2012-10-08 35 views
3

我有一种看法,单击时应提示用户拨打电话。我尝试使用下面的代码:iOS:提示用户拨打电话

 NSString *phoneNumber = [@"tel://" stringByAppendingString:locPhone]; 
    NSString *phoneString = [phoneNumber stringByReplacingOccurrencesOfString:@" " withString:@""]; 
    phoneString= [phoneString stringByReplacingOccurrencesOfString:@"(" withString:@""]; 
    phoneString= [phoneString stringByReplacingOccurrencesOfString:@")" withString:@""]; 
    phoneString= [phoneString stringByReplacingOccurrencesOfString:@"-" withString:@""]; 
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:phoneString]]; 

我知道那是达到了代码,而“phoneString”的终极价值是一个10位数的电话号码。没有错误,但没有任何反应。我错过了什么?

编辑:我现在看到,canOpenURL返回NO。这是因为我在模拟器上运行它?

+0

您是如何知道代码已达到的? – aforaudrey

+0

在我看来,这应该工作,没有问题,如果你调试,你确定它得到这些线? –

+0

你应该总是首先使用'canOpenURL'。它是否返回YES? – Felix

回答

2

您的代码似乎正确,但请注意模拟器无法模拟电话呼叫(在模拟器上使用canOpenURL进行测试将返回NO)。

您应该在实际设备上测试您的代码,您可能会发现它可以按预期工作。

顺便说一句,您应该在应用程序中添加canOpenURL测试,并在此情况下向用户显示一些警告,因为用户可能会在iPod设备上或iPad上运行您的应用程序,而且两者都不能执行电话。