標籤:
1.打電話
- (IBAction)callPhone1:(id)sender { NSURL *url = [NSURL URLWithString:@"tel://18500441739"]; [[UIApplication sharedApplication] openURL:url];}
- (IBAction)callPhone2:(id)sender { NSURL *url = [NSURL URLWithString:@"telprompt://18500441739"];
[[UIApplication sharedApplication] openURL:url]; }
- (IBAction)callPhone3:(id)sender { if (_webView == nil) { _webView = [[UIWebView alloc] initWithFrame:CGRectZero]; } [_webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"tel://18500441739"]]];}
推薦使用第三種,第一個為私人API,第二個無法回到原來app
2.發簡訊
- (IBAction)sendMsg:(id)sender { NSURL *url = [NSURL URLWithString:@"sms://18500441739"]; [[UIApplication sharedApplication] openURL:url];}
- (IBAction)sendWithMsg:(id)sender { /* 必須要匯入<MessageUI/MessageUI.h> */ MFMessageComposeViewController *vc = [[MFMessageComposeViewController alloc] init]; /* 訊息內容 */ vc.body = @"吃飯了沒?"; /* 收到訊息的人列表 */ vc.recipients = @[@"18500441739",@"15342777049"]; /* MFMessageComposeViewControllerDelegate */ vc.messageComposeDelegate = self; [self presentViewController:vc animated:YES completion:nil];}
建議使用第二種,第二個可以預編輯文字和發送列表發送完了以後會有代理方法調用
#pragma mark - MFMessageComposeViewControllerDelegate-(void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result{ [controller dismissViewControllerAnimated:YES completion:nil]; if (result == MessageComposeResultCancelled) { NSLog(@"取消發送"); }else if (result == MessageComposeResultFailed){ NSLog(@"發送失敗"); }else if(result == MessageComposeResultSent){ NSLog(@"發送成功"); }else{ NSLog(@"未知情況"); }}
3.發送郵件
- (IBAction)sendEmail:(id)sender { NSURL *url = [NSURL URLWithString:@"mailto://[email protected]"]; [[UIApplication sharedApplication] openURL:url];}
- (IBAction)sendEmailWithMsg:(id)sender { NSLog(@"%@",[self class]); if (![MFMailComposeViewController canSendMail]) { NSLog(@"yes"); return; } MFMailComposeViewController *vc = [[MFMailComposeViewController alloc] init]; /* 收件者清單 */ [vc setToRecipients:@[@"[email protected]",@"22422061134"]]; /* 抄送人列表 */ [vc setCcRecipients:@[@"[email protected]"]]; /* 密送人列表 */ [vc setBccRecipients:@[@"[email protected]",@"[email protected]"]]; /* 設定代理 MFMailComposeViewControllerDelegate*/ [vc setMailComposeDelegate:self]; /* 發送主題 */ [vc setSubject:@"會議"]; /* 發送內容,是否為HTML文檔 */ [vc setMessageBody:@"測試發郵件功能" isHTML:NO]; /* 添加附件 */ UIImage *image = [UIImage imageNamed:@"IMG_0993"]; NSData *data = UIImagePNGRepresentation(image); [vc addAttachmentData:data mimeType:@"image/png" fileName:@"首頁.png"]; /* 跳轉頁面 */ [self presentViewController:vc animated:YES completion:nil];}
推薦 第二種方法,第二種方法也有代理方法的調用
#pragma mark - MFMailComposeViewControllerDelegate-(void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error{ [controller dismissViewControllerAnimated:YES completion:nil]; if (result == MFMailComposeResultCancelled) { NSLog(@"取消發送"); }else if (result == MFMailComposeResultSent){ NSLog(@"發送成功"); }else if (result == MFMailComposeResultSaved){ NSLog(@"儲存郵件"); }else if (result == MFMailComposeResultFailed){ NSLog(@"發送失敗"); }else{ NSLog(@"發送異常"); }}
4.跳轉其他app
a.跳轉瀏覽器
/* 跳入瀏覽器 */- (IBAction)intoBorwer:(UIButton *)sender { /* 如果想開啟一些常見檔案,比如html、txt、PDF、PPT等,都可以使用UIWebView開啟 只需要告訴UIWebView檔案的URL即可 至於開啟一個遠端共用資源,比如http協議的,也可以調用系統內建的Safari瀏覽器 */ NSLog(@"%@",sender.titleLabel.text); NSString *urlStr = [NSString stringWithFormat:@"http://%@",sender.titleLabel.text]; NSURL *url = [NSURL URLWithString:urlStr]; [[UIApplication sharedApplication] openURL:url];}
b.跳轉到其他app
/* 跳入到其他的程式 */- (IBAction)intoOtherApp:(id)sender { /* 在要跳入的app中設定 URL Types 1.開啟原檔案 2.選擇Info.Plist檔案 3.添加URL types 4.點開URL types 將URL identifier的值設為 ios.defaults.com 5.在URL types中添加URL Schemes,並將其值設為 who 6.訪問的url為 who://ios.defaults.com */ NSURL *url = [NSURL URLWithString:@"yds://ios.hgl.org"]; [[UIApplication sharedApplication] openURL:url];}
打電話,發簡訊,發郵件,app跳轉