您的位置:首页 > 其它

调用系统的短信和邮件功能,实现发送指定内容等

2016-10-20 21:17 836 查看
1.首先导入 #import<MessageUI/MessageUI.h>,有的人说还要导入MessageUI.framework,但是我这里面没导入也好用,还是导入吧,也不费什么事!

2.<MessageUI/MessageUI.h>中包含MFMailComposeViewController和MFMessageComposeViewController,也就是邮件和短信,实现两个代理,

MFMailComposeViewControllerDelegate,MFMessageComposeViewControllerDelegate

3.在.m文件中实现方法:

(1)邮件,只需要在你点击邮件按钮的方法中调用sendMail即可!!

- (void)sendMail

{

MFMailComposeViewController *mailPicker = [[MFMailComposeViewControlleralloc]init];

if (!mailPicker) {

//在设备还没有添加邮件账户的时候mailPicker为空,下面的present
view controller会导致程序崩溃,这里要作出判断

NSLog(@"设备还没有添加邮件账户");

return;

}

mailPicker.mailComposeDelegate =self;

(一)//设置主题

[mailPicker setSubject:@"这里是主题"];

(二)//设置内容

[mailPicker setMessageBody:@"这里是邮件的内容"];
//还有很多方法,比如

(三)//添加收件人

NSArray *toRecipients = [NSArray arrayWithObject: @"first@example.com"];

[mailPicker setToRecipients: toRecipients];

(四)//添加抄送

NSArray *ccRecipients = [NSArray arrayWithObjects:@"second@example.com", @"third@example.com", nil];

[mailPicker setCcRecipients:ccRecipients];

(五)//添加密送

NSArray *bccRecipients = [NSArray arrayWithObjects:@"fourth@example.com", nil];

[mailPicker setBccRecipients:bccRecipients];

(六)// 添加一张图片

UIImage *addPic = [UIImage imageNamed: @"Icon@2x.png"];

NSData *imageData = UIImagePNGRepresentation(addPic);

//关于mimeType:http://www.iana.org/assignments/media-types/index.html

[mailPicker addAttachmentData: imageData mimeType: @"" fileName: @"Icon.png"];

(七)//添加一个pdf附件

NSString *file = [self fullBundlePathFromRelativePath:@"高质量C++编程指南.pdf"];

NSData *pdf = [NSData dataWithContentsOfFile:file];

[mailPicker addAttachmentData: pdf mimeType: @"" fileName: @"高质量C++编程指南.pdf"];

(八)//添加一个视频

NSString *path=[NSString stringWithFormat:@"%@/Documents/%@",NSHomeDirectory(),@"20121219.avi"];

NSData *video = [NSData dataWithContentsOfFile:path];

[mailPicker addAttachmentData:video mimeType: @"" fileName:@"20121219.avi"];

[selfpresentViewController:mailPickeranimated:YEScompletion:nil];

}
//实现代理方法

- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result
error:(NSError *)error

{

//关闭邮件发送窗口

[controller dismissViewControllerAnimated:YEScompletion:nil];

NSString *msg;

if (result ==MFMailComposeResultCancelled) {

msg = @"用户取消编辑邮件";

}

elseif (result ==MFMailComposeResultSaved){

msg = @"用户成功保存邮件";

}

elseif (result ==MFMailComposeResultSent){

msg = @"发送成功";

}

else

{

msg = @"用户试图保存或者发送邮件失败";

}

[selfalertWithMessage:msg];

}
(2)短信,只需要在你点击短信按钮的方法中调用sendMessage即可!!

-(void)sendMessage

{

//显示发短信的控制器

MFMessageComposeViewController *messageViewController =[[MFMessageComposeViewControlleralloc]init];

if (!messageViewController) {

//在设备还没有添加邮件账户的时候messageViewController为空,下面的present
view controller会导致程序崩溃,这里要作出判断

return;

}

//设置短信内容

messageViewController.body = [InvateURLstringByAppendingString:self.invitationURL];

//设置收件人列表

messageViewController.recipients=@[@"13888888888",@"13666666666"];

//设置代理

messageViewController.messageComposeDelegate =self;

//显示控制器

[selfpresentViewController:messageViewControlleranimated:YEScompletion:^{

}];

}

#pragma mark 调取手机短信

-(void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result

{

[controller dismissViewControllerAnimated:YEScompletion:nil];

//关闭短信界面

NSString *msg;

if (result ==MessageComposeResultCancelled) {

msg = @"取消发送";

}

elseif (result ==MessageComposeResultSent){

msg = @"发送成功";

}

else

{

msg = @"发送失败";

}

[selfalertWithMessage:msg];

}

//弹出alert方法

-(void)alertWithMessage:(NSString *)msg

{

UIAlertController *alert = [UIAlertControlleralertControllerWithTitle:@"提示"message:msgpreferredStyle:
UIAlertControllerStyleAlert];

[alert addAction:[UIAlertActionactionWithTitle:@"确定"style:UIAlertActionStyleDefaulthandler:^(UIAlertAction
*_Nonnull action) {

SZQLog(@"确定");

}]];

[selfpresentViewController:alertanimated:YEScompletion:^{

}];

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  短信 邮件 系统
相关文章推荐