The simulator does not perform the call method and must be a real machine to make a call. The method is tested under the IOS10 system
Method One, Requestwithurl (recommended use)
Features: Pop-up prompt before dialing. And, you will return to the original application when you finish dialing.
OC Code:
NSMutableString * str=[[NSMutableString alloc] initWithFormat:@"tel:%@",@"10086"];UIWebView * callWebview = [[UIWebView alloc] init];[callWebview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:str]]];[self.view addSubview:callWebview];
Swift code:
let callWebview = UIWebView()callWebview.loadRequest(NSURLRequest(url: URL(string: "tel:10086")!) as URLRequest)self.view.addSubview(callWebview)
Method Two, OpenURL (telprompt)
Features: Pop-up prompt before dialing. And, you will return to the original application when you finish dialing. The internet says the method may not be legal and cannot be audited.
OC Code:
NSMutableString * str=[[NSMutableString alloc] initWithFormat:@"telprompt:%@",@"10086"];[[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];
Swift code:
UIApplication.shared.openURL(URL(string: "telprompt:10086")!)
Method three, using OpenURL (tel)
Features: No popup prompts before dialing. And, you will return to the original application when you finish dialing. This method online has a post said, after dialing the phone to stay in the address book, will not go back to the original application, may be iOS10 before the system, I used iOS10 system test really back to the original application.
OC Code:
NSMutableString * str=[[NSMutableString alloc] initWithFormat:@"tel:%@",@"10086"];[[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];
Swift code:
UIApplication.shared.openURL(URL(string: "tel:10086")!)
The following warning will be used under the Xcode8 development tool - (BOOL)openURL:(NSURL*)url !
Please use openURL:options:completionHandler: instead
IOS10 API Changes:
- (BOOL)openURL:(NSURL*)url;//不提倡使用
After iOS10, we recommend replacing the following API instead
- (void)openURL:(NSURL*)url options:(NSDictionary<NSString *, id> *)options completionHandler:(void (^ __nullable)(BOOL success))completion;
Remarks:
1. After iOS10, use OpenURL: to make a call with a 1-2 second delay, iOS10 after the openurl:options: Completionhandler: The API for can resolve latency issues.
2. This OpenURL:options:completionHandler: Method API tests the situation under iOS11: Before dialing the prompt, and, after the call will return to the original application.
For example :
OC Code:
nsstring *callPhone = [nsstring stringwithformat:@ "telprompt://%@", @ "10086"]; cgfloat version = [[[uidevice currentdevice]systemversion] Floatvalue]; if (version >= 10.0) {/// Greater than or equal to 10.0 systems use this OpenURL method [[uiapplication sharedapplication] Openurl:[nsurl Urlwithstring:callphone] options:@{} completionhandler:nil]; } else {[[uiapplication Sharedapplication] OpenURL:[nsurl Urlwithstring:callphone]; }
In OC code, the judgment system method can be replaced by the following statement (in fact, Xcode9.1 automatically prompts to add), so that you do not have to take the system version number, but the system method @available(iOS 10.0, *) , the code is as follows:
NSString *callPhone = [NSString stringWithFormat:@"telprompt://%@", @"10086"]; if (@available(iOS 10.0, *)) { [[UIApplication sharedApplication] openURL:[NSURL URLWithString:callPhone] options:@{} completionHandler:nil]; } else { [[UIApplication sharedApplication] openURL:[NSURL URLWithString:callPhone]]; }
SWIFT3 Code:
let "10086"if #available(iOS 10.0, *) { UIApplication.shared.open(URL(string: "tel:" + tel!)!, options: [:], completionHandler: nil) } else { UIApplication.shared.openURL(URL(string: "tel:" + tel!)!) }
IOS--Make a phone call