美文网首页
iOS-调用系统的发送短信和发送邮件功能,实现短信分享和邮件分享

iOS-调用系统的发送短信和发送邮件功能,实现短信分享和邮件分享

作者: MJBaby | 来源:发表于2016-11-25 18:24 被阅读1836次

    一、邮件分享

    1、iOS系统自带邮件设置邮箱(此处以QQ邮箱为例)

    a.打开电脑,登录邮箱(以QQ邮箱为例),进入设置-账户-找到“POP3/IMAP/SMTP/Exchange/CardDAV/CalDAV服务”将“IMAP/SMTP服务”打开后并生成授权码。

    b、生成授权码由16位随机字母组成

    c、设置手机端。打开设置-邮件-添加账户

    d、此处以QQ邮箱为例,其他邮箱也可。

    e、按要求填写后点击下一步。

    f、按图中所示进行设置后,最后点击“发件服务器”SMTP设置

    g、继续点击。“主要服务器”设置

    h、按图中设置,最后完成。

    i、选择“邮件”app,即可使用。

    注意:由于QQ邮箱激活时间不满14天,暂时无法设置POP3/IMAP/SMTP/Exchange/CardDAV/CalDAV服务

    会造成只能发送邮件,但是邮件接收不到的情况。

    2、实现功能

    - (void)displayMailComposerSheet

    {

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

    // 设置picker的委托方法,完成之后会自动调用成功或失败的方法

    picker.mailComposeDelegate = self;

    // 添加主题

    [picker setSubject:@"文件分享"];

    // 添加收件人

    //NSArray *toRecipients = [NSArray arrayWithObject:@"收件人@qq.com"];

    // 说明:也可以添加多个收件人,代码如下所示:

    //NSArray *toRecipients = [NSArray arrayWithObjects:@"one@qq.com",@"two@qq.com",nil];

    // 添加抄送

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

    // 添加密送

    //NSArray *bccRecipients = [NSArray arrayWithObject:@"fourth@qq.com"];

    [picker setToRecipients:toRecipients];

    //[picker setCcRecipients:ccRecipients];

    //[picker setBccRecipients:bccRecipients];

    // 直接在HTML代码中写入图片的地址

    NSString *emailBody = [NSString stringWithFormat:@"

    我分享了图片

    "];

    [picker setMessageBody:emailBody isHTML:YES];

    [self presentModalViewController:picker animated:YES];

    [picker release];

    }

    这样实现的效果是,在邮件中会显示一个图片和一段文本,关键是第25行代码,一定要设置isHTML为YES。但实际应用中,图片一般我们没有上传到服务器上,而是在客户端,这时候我们可以用下面的方式来发送图片(将第23行代码替换成下面的代码):

    // 发送图片附件(其他格式的附件,可以都先转化称NSData类型,然后设置相应的mimeType即可,如txt类型为@"text/txt",doc类型为@"text/doc",pdf类型为@"file/pdf"等等)

    NSData *myData = [NSData dataWithContentsOfFile:[[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:@"new.png"]];

    [picker addAttachmentData:myData mimeType:@"image/jpeg" fileName:@"new.png"];

    NSString *emailBody = [NSString stringWithFormat:@"

    我分享了图片

    "];

    使用上面这种方式,将isHTML设置为YES的话,图片会显示在正文中;isHTML设置为NO的话,图片会显示在附件中。

    》添加邮箱附件方法:

    //发送图片附件

    NSString *path = [[NSBundle mainBundle] pathForResource:@"rainy" ofType:@"jpg"];

    NSData *myData = [NSData dataWithContentsOfFile:path];

    [picker addAttachmentData:myData mimeType:@"image/jpeg" fileName:@"rainy.jpg"];

    //发送txt文本附件

    NSString *path = [[NSBundle mainBundle] pathForResource:@"MyText" ofType:@"txt"];

    NSData *myData = [NSData dataWithContentsOfFile:path];

    [picker addAttachmentData:myData mimeType:@"text/txt" fileName:@"MyText.txt"];

    //发送doc文本附件

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

    NSData *myData = [NSData dataWithContentsOfFile:path];

    [picker addAttachmentData:myData mimeType:@"text/doc" fileName:@"MyText.doc"];

    //发送pdf文档附件

    NSString *path = [[NSBundlemainBundle]pathForResource:@"CodeSigningGuide"ofType:@"pdf"];

    NSData *myData = [NSDatadataWithContentsOfFile:path];

    [pickeraddAttachmentData:myDatamimeType:@"file/pdf"fileName:@"rainy.pdf"];

    邮件发送完成之后,可以在如下的委托方法中进行相应的处理,委托方法如下所示:

    - (void)mailComposeController:(MFMailComposeViewController*)controller

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

    {

    switch (result)

    {

    case MFMailComposeResultCancelled:

    NSLog(@"Result: Mail sending canceled");// 邮件发送取消

    break;

    case MFMailComposeResultSaved:

    NSLog(@"Result: Mail saved");// 邮件保存成功

    break;

    case MFMailComposeResultSent:

    NSLog(@"Result: Mail sent");// 邮件发送成功

    break;

    case MFMailComposeResultFailed:

    NSLog(@"Result: Mail sending failed");// 邮件发送失败

    break;

    default:

    NSLog(@"Result: Mail not sent");

    break;

    }

    [self dismissModalViewControllerAnimated:YES];

    }

    导入MessageUI.framework

    .h文件中#import

    #import

    实现 MFMailComposeViewControllerDelegate, MFMessageComposeViewControllerDelegate

    二、发送短信

    //短信

    -(void)showSMSPicker{

    Class messageClass = (NSClassFromString(@"MFMessageComposeViewController"));

    if (messageClass != nil) {

    // Check whether the current device is configured for sending SMS messages

    if ([messageClass canSendText]) {

    [selfdisplaySMSComposerSheet];

    }

    else {

    UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@""message:@"设备不支持短信功能" delegate:selfcancelButtonTitle:@"确定" otherButtonTitles:nil];

    [alert show];

    [alert release];

    }

    }

    else {

    }

    }

    -(void)displaySMSComposerSheet

    {

    MFMessageComposeViewController *picker = [[MFMessageComposeViewControlleralloc]init];

    picker.messageComposeDelegate =self;

    NSString *smsBody =[NSStringstringWithFormat:@"我分享了文件给您,地址是%@",address] ;

    picker.body=smsBody;

    [selfpresentModalViewController:pickeranimated:YES];

    [pickerrelease];

    }

    相关文章

      网友评论

          本文标题:iOS-调用系统的发送短信和发送邮件功能,实现短信分享和邮件分享

          本文链接:https://www.haomeiwen.com/subject/frrwpttx.html