【问题标题】:How to call a phone from a number entered in a UITextField?如何从 UITextField 中输入的号码拨打电话?
【发布时间】:2011-08-10 12:38:10
【问题描述】:

我想让一个按钮呼叫用户在文本字段中输入的电话号码。我有一个代码,但它不起作用。

NSString * phoneNumber = [NSString stringWithFormat:@"%@%@", @"tel://", phoneNumber.text];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:phoneNumber]];

有人有类似的方法吗?谢谢。

【问题讨论】:

  • 只是一个问题,你是在设备还是模拟器上尝试。
  • @Hadi:哈,好电话。他可能只是在模拟器上使用它。 ;)

标签: iphone cocoa-touch


【解决方案1】:

我认为是tel: 而不是tel://See this Apple document.试试这个:

NSString *pn = [@"tel:" stringByAppendingString:phoneNumber.text];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:pn]];

【讨论】:

  • 嗯......你说得对,文档只显示“tel:”,而不是“tel://”,但如果你使用“tel://”,它肯定可以正常工作。
【解决方案2】:

请参阅my answer to another question 获取一些处理无效输入情况的示例代码。

基本上你这样做:

NSString *cleanedString = [[phoneNumber componentsSeparatedByCharactersInSet:[[NSCharacterSet characterSetWithCharactersInString:@"0123456789-+()"] invertedSet]] componentsJoinedByString:@""];
NSString *escapedPhoneNumber = [cleanedString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *telURL = [NSURL URLWithString:[NSString stringWithFormat:@"tel://%@", escapedPhoneNumber]];

更新:我注意到您创建的字符串共享某个名称(“phoneNumber”)作为您尝试从中获取文本的文本字段。您可能想要重命名这两个中的任何一个。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-08-11
    • 1970-01-01
    • 2016-03-26
    • 1970-01-01
    • 2018-03-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多