简体   繁体   中英

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#"]];

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"); }
                         }];

Replace * with %2A and # with %23 :

NSURL *tel = [NSURL URLWithString:@"tel:%2A199%2A123456789%23"];
[[UIApplication sharedApplication] openURL:tel];

您需要使用tel://,而不仅仅是tel:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM