问题
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