iOS调用系统的发短信功能可以分为两种:1,程序外调用系统发短信。2,程序内调用系统发短信。第二种的好处是用户发短信之后还可以回到app。这对app来说非常重要。
这个方法其实很简单,直接调用openURL即可:
[[UIApplication sharedApplication]openURL:[NSURL URLWithString:@sms://1333338888]];
2)实现代理方法MFMessageComposeViewControllerDelegate
-(void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result { [self dismissViewControllerAnimated:YES completion:nil]; switch (result) { case MessageComposeResultSent: //信息传送成功 break; case MessageComposeResultFailed: //信息传送失败 break; case MessageComposeResultCancelled: //信息被用户取消传送 break; default: break; } }
-(void)showMessageView:(NSArray *)phones title:(NSString *)title body:(NSString *)body { if( [MFMessageComposeViewController canSendText] ) { MFMessageComposeViewController * controller = [[MFMessageComposeViewController alloc] init]; controller.recipients = phones; controller.navigationBar.tintColor = [UIColor redColor]; controller.body = body; controller.messageComposeDelegate = self; [self presentViewController:controller animated:YES completion:nil]; [[[[controller viewControllers] lastObject] navigationItem] setTitle:title];//修改短信界面标题 } else { UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@提示信息 message:@该设备不支持短信功能 delegate:nil cancelButtonTitle:@确定 otherButtonTitles:nil, nil]; [alert show]; } }
参数phones:发短信的手机号码的数组,数组中是一个即单发,多个即群发。
[self showMessageView:[NSArray arrayWithObjects:@13888888888,@13999999999, nil] title:@test body:@你是土豪么,么么哒];
创建可变的地址字符串对象:
NSMutableString *mailUrl = [[NSMutableString alloc] init];
添加收件人:
NSArray *toRecipients = @[@"写你们自己的邮箱测试@qq.com"]; // 注意:如有多个收件人,可以使用componentsJoinedByString方法连接,连接符为@"," [mailUrl appendFormat:@"mailto:%@", toRecipients[0]];
添加抄送人:
NSArray *ccRecipients = @[@"1229436624@qq.com"]; [mailUrl appendFormat:@"?cc=%@", ccRecipients[0]];View Code
添加密送人:
NSArray *bccRecipients = @[@"shana_happy@126.com"]; [mailUrl appendFormat:@"&bcc=%@", bccRecipients[0]];View Code
添加邮件主题和邮件内容:
[mailUrl appendString:@"&subject=my email"]; [mailUrl appendString:@"&body=<b>Hello</b> World!"];
使用前注意:
1)项目需要导入框架:MessageUI.framework
2)使用的Controlelr里导入头文件:#import <MessageUI/MessageUI.h>
获取用户是否设置了邮件账户:
if ([MFMailComposeViewController canSendMail]) { // 用户已设置邮件账户 [self sendEmailAction]; // 调用发送邮件的代码 }
sendEmailAction方法代码:
- (void)sendEmailAction { // 邮件服务器 MFMailComposeViewController *mailCompose = [[MFMailComposeViewController alloc] init]; // 设置邮件代理 [mailCompose setMailComposeDelegate:self]; // 设置邮件主题 [mailCompose setSubject:@"我是邮件主题"]; // 设置收件人 [mailCompose setToRecipients:@[@"[email protected]"]]; // 设置抄送人 [mailCompose setCcRecipients:@[@"[email protected]"]]; // 设置密抄送 [mailCompose setBccRecipients:@[@"[email protected]"]]; /** * 设置邮件的正文内容 */ NSString *emailContent = @"我是邮件内容"; // 是否为HTML格式 [mailCompose setMessageBody:emailContent isHTML:NO]; // 如使用HTML格式,则为以下代码 // [mailCompose setMessageBody:@"<html><body><p>Hello</p><p>World!</p></body></html>" isHTML:YES]; /** * 添加附件 */ UIImage *image = [UIImage imageNamed:@"image"]; NSData *imageData = UIImagePNGRepresentation(image); [mailCompose addAttachmentData:imageData mimeType:@"" fileName:@"custom.png"]; NSString *file = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"pdf"]; NSData *pdf = [NSData dataWithContentsOfFile:file]; [mailCompose addAttachmentData:pdf mimeType:@"" fileName:@"dddddd"]; // 弹出邮件发送视图 [self presentViewController:mailCompose animated:YES completion:nil]; }
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; } // 关闭邮件发送视图 [self dismissViewControllerAnimated:YES completion:nil]; }
使用前注意:
1)下载第三方库
2)导入类#import "SKPSMTPMessage.h"、#import "NSData+Base64Additions.h"
设置基本参数:
SKPSMTPMessage *mail = [[SKPSMTPMessage alloc] init]; [mail setSubject:@"我是主题"]; // 设置邮件主题 [mail setToEmail:@"填你们自己的@qq.com"]; // 目标邮箱 [mail setFromEmail:@"填你们自己的@qq.com"]; // 发送者邮箱 [mail setRelayHost:@"smtp.qq.com"]; // 发送邮件代理服务器 [mail setRequiresAuth:YES]; [mail setLogin:@"填你们自己的@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"};
添加附件(以下代码可在SKPSMTPMessage库的DMEO里找到):
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 setParts:@[plainPart, vcfPart]]; // 邮件首部字段、邮件内容格式和传输编码 [mail send];
SKPSMTPMessage代理,可以获知成功/失败进行后续步骤处理:
- (void)messageSent:(SKPSMTPMessage *)message { NSLog(@"%@", message); } - (void)messageFailed:(SKPSMTPMessage *)message error:(NSError *)error { NSLog(@"message - %@\nerror - %@", message, error); }