I want to call phone number “#51234” in Xcode use telprompt [duplicate]

倖福魔咒の 提交于 2019-12-13 00:56:55

问题


I want to call phone number "#51234" in Xcode use telprompt.

but telprompt is reject it.

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"telprompt://#5%@", nzoneNum]]];

nzomeNum is "1234"


回答1:


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

// set up the dial sequence
let nzoneNum = "1234"
let prefix = "#5"
let dialSequence = "\(prefix)\(nzoneNum)"

// "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
}

// add the `tel:` url scheme to the front of the encoded string
let dialURLString = "tel:\(encodedDialSequence)"

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

// 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

// set up the dial sequence
NSString *nzoneNum = @"1234";
NSString *prefix = @"#5";
NSString *dialSequence = [NSString stringWithFormat:@"%@%@", prefix, nzoneNum];

// set up the URL Host allowed character set, and "percent encode" the dial sequence
NSCharacterSet *urlHostAllowed = [NSCharacterSet URLHostAllowedCharacterSet];
NSString *encodedDialSequence = [dialSequence stringByAddingPercentEncodingWithAllowedCharacters:urlHostAllowed];

// add the `tel` url scheme to the front of the encoded string
NSString *dialURLString = [NSString stringWithFormat:@"tel:%@", encodedDialSequence];

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

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

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



回答2:


Unfortunately you can't make calls to any number including a hashtag. Apple clearly restricts those calls: iPhoneURLScheme_Reference

To prevent users from maliciously redirecting phone calls or changing the behavior of a phone or account, the Phone app supports most, but not all, of the special characters in the tel scheme. Specifically, if a URL contains the * or # characters, the Phone app does not attempt to dial the corresponding phone number.



来源:https://stackoverflow.com/questions/18648120/i-want-to-call-phone-number-51234-in-xcode-use-telprompt

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!