这些功能比较实用,而且实现的代码也比较简单
此方式打完电话,不会返回应用界面,而是停留在打电话界面
NSURL *url = [NSURL URLWithString:@"tel://10086"];[[UIapplication sharedApplication] openURL:url];此方法拨号之前会提示是否拨号,打完电话后会回到应用界面,但是因为是私有API,苹果不建议使用
NSURL *url = [NSURL URLWithString:@"telPRompt://10086"];[[UIApplication sharedApplication] openURL:url];此方法是最推荐的,但是需要注意的是,不能给webView设置frame,因为其目的只是调用电话功能,不需要显示
UIWebView *webView = [[UIWebView alloc]init];[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"tel://10086"]]];与打电话方式1相似,URL的协议换成sms,且不能指定短信内容,其结果也是在发完短信后不会返回应用界面
NSURL *url = [NSURL URLWithString:@"sms://10086"]; [[UIApplication sharedApplication] openURL:url];使用MessageUI框架,可以指定短信内容,也可以在发完短信后返回应用界面 首先需要导入头文件#import <MessageUI/MessageUI.h> 然后:
MFMessageComposeViewController *messageComposeVC = [[MFMessageComposeViewController alloc]init]; messageComposeVC.body = @"Hello World"; messageComposeVC.recipients = @[@"10086",@"10010"]; messageComposeVC.messageComposeDelegate = self; //显示短信界面[self presentViewController:messageComposeVC animated:YES completion:nil];最后实现代理方法:
-(void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result{[controller dismissViewControllerAnimated:YES completion:nil];if (result == MessageComposeResultCancelled) { //TODO}else if (result == MessageComposeResultFailed){//TODO}else{ //result == MessageComposeResultSent//TODO}与前面的类似,不能返回到应用界面
NSURL *url = [NSURL URLWithString:@"mailto://email@QQ.com"];[[UIApplication sharedApplication] openURL:url];与发短信类似,使用的是MessageUI框架的MFMailComposeViewController类 首先需要导入头文件#import <MessageUI/MessageUI.h> 然后:
MFMailComposeViewController *mailComposeVC = [[MFMailComposeViewController alloc]init];[mailComposeVC setSubject:@"subject"];[mailComposeVC setToRecipients:@[@"eamil@qq.com"]];[mailComposeVC setMessageBody:@"Hello World" isHTML:NO];//其他需要填写的内容可以查看头文件定义mailComposeVC.mailComposeDelegate = self;//显示邮箱页面[self presentViewController:mailComposeVC animated:NO completion:nil];最后实现代理方法:
-(void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error{[controller dismissViewControllerAnimated:YES completion:nil];if (result == MessageComposeResultCancelled) {//TODO}else if (result == MessageComposeResultFailed){//TODO}else{ //result == MessageComposeResultSent//TODO}打开其他文件,例如PPT,PDF,TXT,HTML都可以用UIWebView实现,只要定义好URL就可以了
新闻热点
疑难解答