【问题标题】:Phone call number with hashtag on iOSiOS 上带有井号标签的电话号码
【发布时间】:2012-10-27 06:10:18
【问题描述】:

如何在 iOS 上拨打此号码 *199*123456789#?

我使用了以下代码,但它不起作用。

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"tel:*199*123456789#"]];

【问题讨论】:

标签: ios iphone phone-call


【解决方案1】:

根据我对现已结束的问题“iOS - I want to call phone number "#51234" in Xcode useing telprompt”的回答重新发布和修改:

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

通过首先对电话号码进行编码,然后添加tel: 前缀,最后将生成的字符串转换为 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"); }
                         }];

【讨论】:

    【解决方案2】:

    * 替换为%2A,将# 替换为%23

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

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        猜你喜欢
        • 2013-04-14
        • 2017-04-22
        • 1970-01-01
        • 2011-08-28
        • 1970-01-01
        • 1970-01-01
        • 2013-03-22
        • 2014-04-01
        • 2012-10-21
        相关资源
        最近更新 更多