您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 使用技巧 >> iOS 7 技巧

iOS 撥打電話的三種寫法比較:哪一種會回到 App?

編輯:iOS 7 技巧

「點一下就能打電話」是常見需求,但 iOS 上至少有三種寫法,行為差異不小,選錯會讓使用者通話結束後回不來。

寫法一:直接 openURL

NSMutableString *str = [[NSMutableString alloc] initWithFormat:@"tel:%@", @"186xxxx6979"];
NSLog(@"str======%@", str);
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];

這種寫法會直接撥出、不顯示確認提示,但通話結束後停留在通訊錄,回不到你的 App。

寫法二:透過 UIWebView 觸發(推薦)

NSMutableString *str2 = [[NSMutableString alloc] initWithFormat:@"tel:%@", @"186xxxx6979"];
NSLog(@"%@", str2);
UIWebView *callWebview = [[UIWebView alloc] init];
[callWebview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:str2]]];
[self.view addSubview:callWebview];

這種方式會先跳出確認提示,通話結束後自動回到原 App,流程最完整。

寫法三:telprompt 協定

NSMutableString *str3 = [[NSMutableString alloc] initWithFormat:@"telprompt://%@", @"186xxxx6979"];
NSLog(@"str======%@", str3);
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:str3]];

行為與寫法二相同:會跳提示,也會回到 App。

該選哪一個?

使用 telprompt 要小心——它從未出現在 Apple 官方文件中,歷史上確實有因此被退審的案例。若 App 會提交 App Store,建議避開。

至於 UIWebView 那招,在現代 iOS 上已有更乾淨的替代方案,不必為了撥號而多養一個重量級元件:

NSURL *telURL = [NSURL URLWithString:@"tel://186xxxx6979"];
if ([[UIApplication sharedApplication] canOpenURL:telURL]) {
    [[UIApplication sharedApplication] openURL:telURL
                                       options:@{}
                             completionHandler:nil];
}

最後別忘了在 Info.plist 的 LSApplicationQueriesSchemes 加入 tel,否則 canOpenURL: 在 iOS 9 之後一律回傳 NO。另外,模擬器無法真正撥號,測試務必使用實機。

  1. 上一篇文章:
  2. 下一篇文章:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © IOS教程網 All Rights Reserved