iOS中调用短信和邮箱的方法

//该方法在不退出应用程序的前提下调用短信和邮箱,以下内容请在真机测试

//导入框架MessageUI.framework

#import "ViewController.h"

//首先导入头文件

#import <MessageUI/MFMailComposeViewController.h>

#import <MessageUI/MFMessageComposeViewController.h>

//代理

@interface ViewController ()<MFMailComposeViewControllerDelegate, MFMessageComposeViewControllerDelegate>


@end


@implementation ViewController



//邮件按钮方法实现

- (void)mail:(id)sender {

//判断设备是否支持应用内发送邮件功能

    if ([MFMailComposeViewController canSendMail])  {

        

//在应用内发送邮件

        

        //创建邮件controller

        MFMailComposeViewController *mailPicker = [[MFMailComposeViewController alloc] init];

        

        //设置邮件代理

        mailPicker.mailComposeDelegate = self;

        

        //邮件主题

        [mailPicker setSubject:@"Send WebView ScreenShot"];

        

        //设置发送给谁,参数是NSarray,设置发送给两个邮箱

        [mailPicker setToRecipients:[NSArray arrayWithObjects:@"aaaaa@163.com", @"aaaaaa@qq.com", nil]];

        

        

        //可以添加抄送

        [mailPicker setCcRecipients:[NSArray arrayWithObject:@"aaaaa@qq.com"]];

        

        

        //可以添加暗抄送

        [mailPicker setBccRecipients:[NSArray arrayWithObject:@"aaaaaa@qq.com"]];

        

        

        //邮件正文

        [mailPicker setMessageBody:@"WebShotScreen n in Attachment!" isHTML:NO];

        

        

        //发送图片附件

        //第一个图片名字是本地要选择发送的图片的名字, 第二个图片的名字是邮件里发送时显示的图片名字

        NSString *pathImage = [[NSBundle mainBundle] pathForResource:@"a" ofType:@"jpg"];

        NSData *dataImage = [NSData dataWithContentsOfFile:pathImage];

        [mailPicker addAttachmentData:dataImage mimeType:@"image/jpg" fileName:@"1.jpg"];

        

        //发送txt文本附件

        NSString *pathText = [[NSBundle mainBundle] pathForResource:@"tv" ofType:@"txt"];

        NSData *dataText = [NSData dataWithContentsOfFile:pathText];

        [mailPicker addAttachmentData:dataText mimeType:@"text/txt" fileName:@"aa.txt"];

        

        

        //发送doc文本附件

        NSString *pathDoc = [[NSBundle mainBundle] pathForResource:@"MyText" ofType:@"doc"];

        NSData *dataDoc = [NSData dataWithContentsOfFile:pathDoc];

        [mailPicker addAttachmentData:dataDoc mimeType:@"text/doc" fileName:@"MyText.doc"];

        

        

        //发送pdf文档附件

        

        NSString *pathPdf = [[NSBundle mainBundle] pathForResource:@"CodeSigningGuide"ofType:@"pdf"];

        NSData *dataPdf = [NSData dataWithContentsOfFile:pathPdf];

        [mailPicker addAttachmentData:dataPdf mimeType:@"file/pdf"fileName:@"rainy.pdf"];

        

        //把当前controller变为邮件controller

        [self presentModalViewController:mailPicker animated:YES];

        

        

    }else{

        //如果该设备不支持在不退出程序的前提下调用邮件,则会推出应用程序并调用系统邮件,mailto://为固定写法后面加邮箱地址

        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"mailto://admin@hzlzh.com"]];

    }

    

}


//实现 MFMailComposeViewControllerDelegate

//发送结果

- (void)mailComposeController:(MFMailComposeViewController*)controller

          didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error {

    

    NSString *mes = nil;

    

    switch (result)

    {

        case MFMailComposeResultCancelled:

            mes = @"取消编辑邮件";

            break;

        case MFMailComposeResultSaved:

            mes = @"成功保存邮件";

            break;

        case MFMailComposeResultSent:

            mes = @"点击发送,将邮件放到队列中,还没发送";

            break;

        case MFMailComposeResultFailed:

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

            break;

        default:

            break;

    }

    

    UIAlertView *alter = [[UIAlertView alloc] initWithTitle:@"提醒" message:mes delegate:self cancelButtonTitle:nil otherButtonTitles:@"ok", nil];

    [alter show];

    

    

    [self dismissModalViewControllerAnimated:YES];

}




//短信按钮方法实现

- (IBAction)message:(id)sender {


//判断设备是否支持应用内发送短信功能


    if ([MFMessageComposeViewController canSendText]) {

        

//在应用内发送短信

        {

            //初始化

            MFMessageComposeViewController *picker = [[MFMessageComposeViewController alloc] init];

            //代理

            picker.messageComposeDelegate = self;

            picker.navigationBar.tintColor = [UIColor blackColor];

            //短信内容

            picker.body = @"1111111111111111";

            //设置发送给谁

            picker.recipients = [NSArray arrayWithObject:@"13300000000"];

            //推到发送试图控制器

            [self presentModalViewController:picker animated:YES];

            

        }

        

    }

    else {

        //如果该设备不支持在不退出程序的前提下调用短信,则会推出应用程序并调用系统短信,mailto://为固定写法后面加手机号码

        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"sms://800888"]];

        

    }

    

}




//实现 MFMessageComposeViewControllerDelegate

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

    

    NSString *mes = nil;

    

    switch (result) {

            

        case MessageComposeResultCancelled:

            mes = @"取消编辑短信";

            break;

            

        case MessageComposeResultSent:

            mes = @"点击发送,将短信放到队列中,还没发送";

            break;

            

        case MessageComposeResultFailed:

            mes = @"发送短信失败";

            break;

        default:

            break;

    }

    

    UIAlertView *alter = [[UIAlertView alloc] initWithTitle:@"提醒" message:mes delegate:self cancelButtonTitle:nil otherButtonTitles:@"ok", nil];

    [alter show];

    

    [self dismissModalViewControllerAnimated:YES];

    

}




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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值