2013-02-01 132 views
1

我想在NSUserDefaults中将电话号码存储为NSString。出于某种原因,下面的代码不起作用。如果我不使用变量,代码将起作用,但我无法使其工作。有没有人有任何想法为什么?iOS从NSUserDefaults拨打电话号码

NSString *savedPhone = [[NSUserDefaults standardUserDefaults] 
         stringForKey:@"savedPhone"]; 

NSString *callNumber = [NSString stringWithFormat:@"%@%@",@"tel://",savedPhone]; 

NSURL *myURL = [NSURL URLWithString:callNumber]; 
[[UIApplication sharedApplication] openURL:myURL]; 

请让我知道,如果任何人有任何想法。谢谢!

回答

0

你可以用下面的代码实现:

NSUserDefaults *objUserDefaults = [NSUserDefaults standardUserDefaults]; 
NSString *savedPhone = [objUserDefaults objectForKey:@"savedPhone"]; 
[self callNumber:savedPhone]; 

然后添加以下功能:

-(void)callNumber:(NSString *)pstrContactNo 
{ 
    NSString *strDeviceModel = [UIDevice currentDevice].model; 
    if(![strDeviceModel isEqualToString:@"iPhone"]) 
    { 
      UIAlertView *objAlertMsg = [[UIAlertView alloc] initWithTitle:@"" 
                 message:@"Calling is not supported in this device." 
                 delegate:nil 
              cancelButtonTitle:@"OK" 
              otherButtonTitles:nil]; 
     [objAlertMsg show]; 
     [objAlertMsg release]; 
    } 
    else 
    { 
     pstrContactNo = [NSString stringWithFormat:@"tel:%@",pstrContactNo]; 
     NSString *strDialedContact = [pstrContactNo stringByReplacingOccurrencesOfString:@" " withString:@""]; 
     [[UIApplication sharedApplication] openURL:[NSURL URLWithString:strDialedContact]]; 
    } 
} 

这将解决您的问题。

干杯!

+1

见@Oleg后,将导航回到您的应用程序回答,你应该删除''//在'电话:'方案。此外,如果设备支持'tel:'方案,然后检查设备型号是否是iPhone,则更好。只需使用'[[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@“tel:”]' – rckoenes

+0

@rckoenes:答案不被接受。那么为什么你有这个答案?这也是测试代码。不知道。 –

+0

我没有downvote你的答案。我只是想告诉你,你的回答有一些错误。 – rckoenes

1

更改您的网址格式(删除//

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"tel:2135554321"]]; 
0

更改callNumber这样的: 的NSString * callNumber = [的NSString stringWithFormat:@ “电话:%@”,savedPhone]。

0
NSString *number = YOUR-NUMBER;  
    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"telprompt:%@",number]]; 

[[UIApplication sharedApplication] openURL:url]; 

尝试使用此代码,呼完