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

1.首先导入 #import<MessageUI/MessageUI.h>,有的人说还要导入MessageUI.framework,但是我这里面没导入也好用,还是导入吧,也不费什么事!

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

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:@"这里是邮件的内容"];

    //还有很多方法,比如 

  1.        (三)//添加收件人   
  2.         NSArray *toRecipients [NSArray arrayWithObject: @"first@example.com"];   
  3.         [mailPicker setToRecipients: toRecipients];     

  4.         (四)//添加抄送   
  5.         NSArray *ccRecipients [NSArray arrayWithObjects:@"second@example.com"@"third@example.com"nil];     
  6.         [mailPicker setCcRecipients:ccRecipients];   
  7.       
  8.         (五)//添加密送   
  9.         NSArray *bccRecipients [NSArray arrayWithObjects:@"fourth@example.com"nil];     
  10.         [mailPicker setBccRecipients:bccRecipients]; 
  11.       
  12.         (六)// 添加一张图片     
  13.         UIImage *addPic [UIImage imageNamed: @"Icon@2x.png"];     
  14.         NSData *imageData UIImagePNGRepresentation(addPic);  
  15.         //关于mimeType:http://www.iana.org/assignments/media-types/index.html   
  16.         [mailPicker addAttachmentData: imageData mimeType: @"" fileName: @"Icon.png"];
  17.      
  18.         (七)//添加一个pdf附件   
  19.         NSString *file [self fullBundlePathFromRelativePath:@"高质量C++编程指南.pdf"];   
  20.         NSData *pdf [NSData dataWithContentsOfFile:file];   
  21.         [mailPicker addAttachmentData: pdf mimeType: @"" fileName: @"高质量C++编程指南.pdf"];  
  22.    
  23.         (八)//添加一个视频
  24.         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:^{

    }];

}











  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值