1

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

但 telprompt 是拒绝它。

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

nzomeNum 是“1234”

4

2 回答 2

3

至少从 iOS 11 开始,人们可以拨打带有井号 (#) 或星号 (*) 的号码。

通过首先对电话号码进行编码,然后添加tel:前缀,最后将生成的字符串转换为 URL来使用这些字符进行呼叫。

斯威夫特 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") }
}

目标-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"); }
                         }];
于 2017-12-30T21:58:45.167 回答
2

不幸的是,您不能拨打任何号码,包括标签。Apple 明确限制了这些调用:iPhoneURLScheme_Reference

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

于 2013-10-30T12:55:15.837 回答