telUR在openURL中无法正常工作

时间:2012-04-05 13:46:40

标签: iphone openurl tel

在我的应用中,我想拨打所选号码。为此,我有以下代码

 +(void)makeCallToSelectedContact:(NSString*)phoneNo{

    NSMutableString *phoneNumber = [NSMutableString stringWithString:phoneNo];

    [phoneNumber replaceOccurrencesOfString:@" " 
                                 withString:@"" 
                                    options:NSLiteralSearch 
                                      range:NSMakeRange(0, [phoneNumber length])];
    [phoneNumber replaceOccurrencesOfString:@"(" 
                                 withString:@"" 
                                    options:NSLiteralSearch 
                                      range:NSMakeRange(0, [phoneNumber length])];
    [phoneNumber replaceOccurrencesOfString:@")" 
                                 withString:@"" 
                                    options:NSLiteralSearch 
                                      range:NSMakeRange(0, [phoneNumber length])];

    NSLog(@"phoneNumber => %@",phoneNumber);
    if([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:[NSString stringWithFormat:@"tel:+%@",phoneNumber]]]) {
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"tel:+%@",phoneNumber]]];
    }
    else {
        NSLog(@"Unable to open");
        [self showAlertWithTitle:@"Alert" andMessage:@"This Device Doesn't Support Call Functionality"]; 
    }
}

让我们说如果我的电话号码不是+91 1234567890,那么它会正确拨打号码。但如果我的号码没有+&国家代码如1234567890然后它将其转换为+12 34567890,这是错误的电话号码。这是我的控制台日志

    2012-04-05 19:13:37.960 Search[22453:11003] phoneNumber => +911234567890

2012-04-05 19:13:47.928 Search[22453:11003] phoneNumber => 1234567890

我缺少什么?任何形式的帮助都表示赞赏。

1 个答案:

答案 0 :(得分:1)

您在创建网址时添加“+” - 查看:

if([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:[NSString stringWithFormat:@"tel:+%@",phoneNumber]]]) {
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"tel:+%@",phoneNumber]]];
}