0

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

 +(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 这样的 ph 值,那么它将正确呼叫该号码。但是,如果我有没有 + 和国家代码的号码,例如 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

我错过了什么?任何形式的帮助表示赞赏。

4

1 回答 1

1

创建 URL 时添加“+” - 看:

if([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:[NSString stringWithFormat:@"tel:+%@",phoneNumber]]]) {
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"tel:+%@",phoneNumber]]];
}
于 2012-04-05T13:58:35.133 回答