200字范文,内容丰富有趣,生活中的好帮手!
200字范文 > iOS开发之打电话 发短信 发送邮件

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

时间:2022-07-19 01:13:21

相关推荐

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@", nil];[mc setCcRecipients:[NSArray arrayWithObject:@"xxx@"]]; [mc setBccRecipients:[NSArray arrayWithObject:@"xxx@"]]; //设置邮件主体,有两种格式:纯文本,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@"]; // 目标邮箱[mail setFromEmail:@"xxx@"]; // 发送者邮箱[mail setRelayHost:@""]; // 发送邮件代理服务器[mail setRequiresAuth:YES];[mail setLogin:@"xxx@"]; // 发送者邮箱账号[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@"];[mailUrl appendFormat:@"?cc=%@", ccRecipients[0]];//添加密送人NSArray *bccRecipients = @[@"shana_happy@"];[mailUrl appendFormat:@"&bcc=%@", bccRecipients[0]];//添加邮件主题和邮件内容[mailUrl appendString:@"&subject=my email"];[mailUrl appendString:@"&body=<b>Hello</b> World!"];[[UIApplication sharedApplication] openURL:[NSURL URLWithString:mailUrl]];}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。