我想在Xcode中使用telprompt拨打电话号码“#51234”

时间:2013-09-06 00:41:05

标签: ios phone-call hashtag telprompt

我想在Xcode中使用telprompt拨打电话号码“#51234”。

但telprompt拒绝它。

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

nzomeNum是“1234”

2 个答案:

答案 0 :(得分:2)

很遗憾,您无法拨打任何号码,包括主题标签。 Apple明确限制这些电话:iPhoneURLScheme_Reference

  

为防止用户恶意重定向电话或更改手机或帐户的行为,Phone应用程序支持tel方案中的大多数但不是全部特殊字符。具体来说,如果URL包含*或#字符,则Phone应用程序不会尝试拨打相应的电话号码。

答案 1 :(得分:2)

至少从iOS 11开始,一个可以拨打带有#标签(#)或星号(*)的号码。

首先使用这些字符进行调用编码电话号码,然后添加tel:前缀,最后将结果字符串转换为网址。

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