Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
661 views
in Technique[技术] by (71.8m points)

iphone - Phone call number with hashtag on iOS

How do I make a call to this number *199*123456789# on iOS?

I used the following code but it doesn't work.

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"tel:*199*123456789#"]];
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Reposted and modified from my answer to the now-closed question "iOS - I want to call phone number "#51234" in Xcode useing telprompt":

At least as of iOS 11, one can dial numbers with a hashtag (#) or asterisk (*).

Make calls with these characters by first encoding the phone number, then adding the tel: prefix, and finally turning the resulting string into a URL.

Swift 4, iOS 11

// 1) set up the dial sequence as a String
let dialSequence = "*199*123456789#"

// 2) "percent encode" the dial sequence with the "URL Host Allowed" character set
guard let encodedDialSequence =
    dialSequence.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed) else {
    print("Unable to encode the dial sequence.")
    return
}

// 3) add the `tel:` url scheme to the front of the encoded string
// NOTE: the '//' part is optional: either 'tel:' or 'tel://' will work.
let dialURLString = "tel:(encodedDialSequence)"

// 4) set up the URL with the scheme+encoded number string
guard let dialURL = URL(string: dialURLString) else {
    print("Couldn't convert the dial string into an URL.")
    return
}

// 5) dial the URL
UIApplication.shared.open(dialURL, options: [:]) { success in
    if success { print("SUCCESSFULLY OPENED DIAL URL") }
    else { print("COULDN'T OPEN DIAL URL") }
}

Objective-C, iOS 11

// 1) set up the dial sequence as a String
NSString *dialSequence = @"*199*123456789#";

// 2) "percent encode" the dial sequence with the "URL Host Allowed" character set
NSCharacterSet *urlHostAllowed = [NSCharacterSet URLHostAllowedCharacterSet];
NSString *encodedDialSequence = [dialSequence stringByAddingPercentEncodingWithAllowedCharacters:urlHostAllowed];

// 3) add the 'tel:' url scheme to the front of the encoded string
// NOTE: the '//' part is optional: either 'tel:' or 'tel://' will work.
NSString *dialURLString = [NSString stringWithFormat:@"tel:%@", encodedDialSequence];

// 4) set up the URL with the scheme+encoded number string
NSURL *dialURL = [NSURL URLWithString:dialURLString];

// 5) set up an empty dictionary for the options parameter
NSDictionary *optionsDict = [[NSDictionary alloc] init];

// 6) dial the URL
[[UIApplication sharedApplication] openURL:dialURL
                                   options:optionsDict
                         completionHandler:^(BOOL success) {
                             if (success) { NSLog(@"SUCCESSFULLY OPENED DIAL URL"); }
                             else { NSLog(@"COULDN'T OPEN DIAL URL"); }
                         }];

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...