在 iOS App 裡撥打電話,常見的做法有三種。差別主要在於「是否先跳確認提示」以及「撥完之後會不會回到 App」。
特點:直接撥打,不彈出提示;撥完後留在通訊錄,不返回原本的 App。
// 撥打電話
- (void)callPhone:(NSString *)phoneNumber {
NSMutableString *str = [[NSMutableString alloc] initWithFormat:@"tel:%@", phoneNumber];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];
}
特點:撥打前會彈出確認提示;撥完後會回到原本的 App。
// 撥打電話
- (void)callPhone:(NSString *)phoneNumber {
NSMutableString *str = [[NSMutableString alloc] initWithFormat:@"tel:%@", phoneNumber];
UIWebView *callWebview = [[UIWebView alloc] init];
[callWebview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:str]]];
[self.view addSubview:callWebview];
}
特點:撥打前會彈出確認提示;撥完後會回到原本的 App。
注意:Apple 官方文件中並未出現 telprompt 這個 scheme,過去有人使用它而在送審時被拒。
// 撥打電話
- (void)callPhone:(NSString *)phoneNumber {
NSMutableString *str = [[NSMutableString alloc] initWithFormat:@"telprompt://%@", phoneNumber];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];
}
若希望使用者撥完電話後能回到 App 繼續操作,方法二是最穩妥的選擇——它使用的是公開的 tel scheme,行為由系統的通話流程接管,不依賴任何未公開的私有協定。
方法三雖然效果與方法二相同,但因為 telprompt 未列入官方文件,存在被審核退回的風險,不建議在正式產品中使用。
補充兩點實務細節:新版 iOS 中,openURL: 系列 API 已全面改為 completion handler 形式,例如 openURL:options:completionHandler:;另外,若要在 App 內顯示電話號碼而不直接撥出,可以改用 tel: 以外的方案,例如自訂 UI 喚起系統的聯絡人選擇器,讓使用者自行決定要撥給誰。