第一種方式是使用 tel 協定,直接開啟通訊錄或撥號流程。這種方式通常不會回到原 App,而是停留在通訊相關畫面。
NSMutableString *str = [NSMutableString stringWithFormat:@"tel:%@", @"186xxxxxxxx"];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];
第二種方式是透過 UIWebView 載入 tel URL。這種方式在撥打後通常會回到原 App,並可能出現確認提示。
NSMutableString *str = [NSMutableString stringWithFormat:@"tel:%@", @"186xxxxxxxx"];
UIWebView *callWebview = [[UIWebView alloc] init];
[callWebview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:str]]];
[self.view addSubview:callWebview];
第三種方式是使用 telprompt 協定。它會先顯示確認提示,撥打後也能回到原 App。
NSMutableString *str = [NSMutableString stringWithFormat:@"telprompt://%@", @"186xxxxxxxx"];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];
三種方式的主要差異在於是否直接撥打、是否顯示提示,以及撥打後是否回到原應用。可依實際需求選擇。