日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

iOS开发之打电话,发短信,发送邮件

發布時間:2024/5/21 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 iOS开发之打电话,发短信,发送邮件 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

iOS開發中,撥打電話的實現主要有三種方式:

  • 直接撥號:撥打完電話之后回不到原來的應用,會停留在通訊錄里面,而且是直接撥打,不彈出提示
- (void)makePhoneCall {NSMutableString *string = [[NSMutableString alloc] initWithFormat:@"tel:%@",@“10086"];[[UIApplication sharedApplication] openURL:[NSURL URLWithString:string]]; }
  • 跳出應用打完電話之后回到應用
//telprompt協議屬于蘋果的私有協議,一旦程序中使用了此協議,程序無法上架。針對越獄的機器開發的系統,可以使用此協議。- (void)makePhoneCall {NSMutableString *string = [[NSMutableString alloc] initWithFormat:@“telprompt://%@",@“10086"];[[UIApplication sharedApplication] openURL:[NSURL URLWithString:string]]; }
  • 借助UIWebView打電話
- (void)makePhoneCall {NSMutableString *string = [[NSMutableString alloc] initWithFormat:@"tel:%@",@“10086"];UIWebView *callWebview = [[UIWebView alloc] init];[callWebview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:string]]];[self.view addSubview:callWebview]; }

iOS調用系統的發短信功能主要有兩種:

  • 程序外調用系統發短信(不能指定短信內容,且不能自動回到原應用)
NSURL *url = [NSURL URLWithString:@"sms://10010"]; [[UIApplication sharedApplication] openURL:url];
  • 程序內調用系統發短信(發完短信之后可以回到App)

    1.導入MessageUI.framework,并實現代理方法MFMessageComposeViewControllerDelegate

#import<MessageUI/MessageUI.h>- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result {[self dismissViewControllerAnimated:YES completion:nil];switch(result){caseMessageComposeResultSent://信息傳送成功break;caseMessageComposeResultFailed://信息傳送失敗break;caseMessageComposeResultCancelled://信息被用戶取消傳送break;default:break;} }

2.發送短信

- (void)showMessageView:(NSArray*)phones title:(NSString*)title body:(NSString*)body {// 判斷用戶設備能否發送短信if([MFMessageComposeViewController canSendText]) {MFMessageComposeViewController *controller = [[MFMessageComposeViewController alloc] init];//收件人:phones是發短信的手機號碼的數組,數組中是一個即單發,多個即群發controller.recipients = phones;//短信內容controller.body = body;controller.messageComposeDelegate = self;[self presentViewController:controller animated:YES completion:nil];//修改短信界面標題[[[[controllerviewControllers] lastObject] navigationItem] setTitle:title];}else{UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示信息"message:@"該設備不支持短信功能"delegate:nilcancelButtonTitle:@"確定"otherButtonTitles:nil,nil];[alertshow];} }

3.調用發短信的方法

[self showMessageView:[NSArrayarrayWithObjects:@"10086",@"10010",nil] title:@"恭喜" body:@"你們中五百萬大獎啦!!"];

iOS上可以使用三種方法實現郵件的發送:

  • 使用內置的MFMailComposeViewController發送郵件
    1、導入MessageUI,實現MFMailComposeViewControllerDelegate協議
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error { switch (result) { case MFMailComposeResultCancelled: NSLog(@"Mail send canceled..."); break; case MFMailComposeResultSaved: NSLog(@"Mail saved..."); break; case MFMailComposeResultSent: NSLog(@"Mail sent..."); break; case MFMailComposeResultFailed: NSLog(@"Mail send errored: %@...", [error localizedDescription]); break; default: break; } [self dismissModalViewControllerAnimated:YES]; }

2、檢測設備是否支持郵件發送功能

//檢測設備是否支持郵件發送功能 Class mailClass = (NSClassFromString(@"MFMailComposeViewController")); if (mailClass != nil) { if ([mailClass canSendMail]) { [self displayComposerSheet];//調用發送郵件的方法 } }

3.郵件發送方法:

- (void)sendE-mail {//創建視圖控制器MFMailComposeViewController *mc = [[MFMailComposeViewController alloc] init]; mc.mailComposeDelegate = self; //設置郵件主題[mc setSubject:@"Hello, World!"];//設置收件人,收件人有三種:主收件人,cc,bcc[mc setToRecipients:[NSArray arrayWithObjects:@"xxx@126.com", nil];[mc setCcRecipients:[NSArray arrayWithObject:@"xxx@163.com"]]; [mc setBccRecipients:[NSArray arrayWithObject:@"xxx@gmail.com"]]; //設置郵件主體,有兩種格式:純文本,html格式[mc setMessageBody:@"Watson!!!\n\nCome here, I need you!" isHTML:NO]; [mc setMessageBody:@"<HTML><B>Hello, Joe!</B><BR/>What do you know?</HTML>" isHTML:YES]; //添加附件:需要三個參數,一個是NSData類型的附件,一個是mime type,一個附件的名稱NSString *path = [[NSBundle mainBundle] pathForResource:@"blood_orange" ofType:@"png"]; NSData *data = [NSData dataWithContentsOfFile:path]; [mc addAttachmentData:data mimeType:@"image/png" fileName:@"blood_orange"];//視圖呈現[self presentModalViewController:mc animated:YES]; }
  • 通過第三方類庫SKPSMTPMessage發送郵件
    1、下載三方庫,導入類#import “SKPSMTPMessage.h”、#import “NSData+Base64Additions.h”,實現SKPSMTPMessage代理
//成功 - (void)messageSent:(SKPSMTPMessage *)message {NSLog(@"%@", message); }//失敗 - (void)messageFailed:(SKPSMTPMessage *)message error:(NSError *)error {NSLog(@"message - %@\nerror - %@", message, error); }

2、郵件發送方法

- (void)sendE-mail {//設置基本參數SKPSMTPMessage *mail = [[SKPSMTPMessage alloc] init];[mail setSubject:@"主題"]; // 設置郵件主題[mail setToEmail:@"xxx@qq.com"]; // 目標郵箱[mail setFromEmail:@"xxx@qq.com"]; // 發送者郵箱[mail setRelayHost:@"smtp.qq.com"]; // 發送郵件代理服務器[mail setRequiresAuth:YES];[mail setLogin:@"xxx@qq.com"]; // 發送者郵箱賬號[mail setPass:@"填你們自己的"]; // 發送者郵箱密碼[mail setWantsSecure:YES]; // 需要加密[mail setDelegate:self];//設置郵件正文內容 NSString *content = [NSString stringWithCString:"郵件內容" encoding:NSUTF8StringEncoding];NSDictionary *plainPart = @{kSKPSMTPPartContentTypeKey : @"text/plain", kSKPSMTPPartMessageKey : content, kSKPSMTPPartContentTransferEncodingKey : @"8bit"};//添加附件 NSString *vcfPath = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"vcf"];NSData *vcfData = [NSData dataWithContentsOfFile:vcfPath];NSDictionary *vcfPart = [NSDictionary dictionaryWithObjectsAndKeys:@"text/directory;\r\n\tx-unix-mode=0644;\r\n\tname=\"test.vcf\"",kSKPSMTPPartContentTypeKey,@"attachment;\r\n\tfilename=\"test.vcf\"",kSKPSMTPPartContentDispositionKey,[vcfData encodeBase64ForData],kSKPSMTPPartMessageKey,@"base64",kSKPSMTPPartContentTransferEncodingKey,nil];mail.parts = [NSArray arrayWithObjects:plainPart,vcfPart,nil];[mail send]; }
  • 使用openURL發送郵件(用戶體驗較差,程序會進入后臺,跳轉至郵件發送界面)
- (void)sendE-mail {//創建可變的地址字符串對象NSMutableString *mailUrl = [[NSMutableString alloc] init];//添加收件人NSArray *ccRecipients = @[@"1229436624@qq.com"]; [mailUrl appendFormat:@"?cc=%@", ccRecipients[0]];//添加密送人NSArray *bccRecipients = @[@"shana_happy@126.com"]; [mailUrl appendFormat:@"&bcc=%@", bccRecipients[0]];//添加郵件主題和郵件內容[mailUrl appendString:@"&subject=my email"]; [mailUrl appendString:@"&body=<b>Hello</b> World!"];[[UIApplication sharedApplication] openURL:[NSURL URLWithString:mailUrl]]; }

總結

以上是生活随笔為你收集整理的iOS开发之打电话,发短信,发送邮件的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。