2012-04-05 61 views
0

在我的應用程序中,我想調用選定的號碼。爲此,我有如下代碼在openURL中電話無法正常工作

+(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"]; 
    } 
} 

如果我讓說的pH沒有像+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

您要添加的 '+' 當您創建網址 - 看:

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