tom

iOS APP内部调用系统的邮件发送和分享文件

2017-09-10  本文已影响368人  yehkong

序言:app内部调用系统邮件发送和分享文件,其中文件作为邮件附件发送的email目标地址,使用了MessageUI Framework。

##pragma mark - 邮件分享方法
#- (void)shareByEmail{
            MFMailComposeViewController *mailSender = [[MFMailComposeViewController alloc]init];
            mailSender.mailComposeDelegate = self;
            [mailSender setSubject:NKLocalizedString(@"EmailFromMyApp")];
            //以下为具体业务代码
            data = [NSData dataWithContentsOfFile:path];//文件数据
            NSString *extension;//文件类型识别字符串,如文件扩展名
            NSString *mimeType = nil;//这个不能错,错了的话会闪退
                if ([extension isEqualToString:@"MOV"] || [extension isEqualToString:@"MP4"]) {
                    mimeType = @"video/quicktime";
                }else if ([extension isEqualToString:@"MP3"] || [extension isEqualToString:@"M4A"]) {
                    mimeType = @"audio/mpeg3";
                }else if ([extension isEqualToString:@"JPG"] || [extension isEqualToString:@"JPEG"]) {
                    mimeType = @"image/jpeg";
                }else if ([extension isEqualToString:@"PNG"]) {
                    mimeType = @"image/png";
                }else if ([extension isEqualToString:@"TXT"]) {
                    mimeType = @"text/plain";
                }else if ([extension isEqualToString:@"PDF"]) {
                    mimeType = @"application/pdf";
                }else if ([extension isEqualToString:@"DOC"] || [extension isEqualToString:@"DOCX"]) {
                    mimeType = @"application/msword";
                }else if ([extension isEqualToString:@"XLS"] || [extension isEqualToString:@"XLSX"]) {
                    mimeType = @"application/vnd.ms-exceld";
                }else if ([extension isEqualToString:@"PPT"] || [extension isEqualToString:@"PPTX"]) {
                    mimeType = @"application/vnd.ms-powerpoint";
                }else if ([extension isEqualToString:@"ZIP"]) {
                    mimeType = @"application/zip";
                }else{
                    return ;
                }
                [mailSender addAttachmentData:data mimeType:mimeType fileName:name];
            }
            [self presentViewController:mailSender animated:YES completion:nil];
}

##pragma mark - MFMailComposeViewControllerDelegate
#- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
    [controller dismissViewControllerAnimated:YES completion:nil];
    switch (result) {
        case MFMailComposeResultCancelled:
        {
        }
            break;
        case MFMailComposeResultSaved:
        {
        }
            break;
        case MFMailComposeResultSent:
        {
            showAlertMessage(nil, LocalizedString(@"HaveSent"));
        }
            break;
        case MFMailComposeResultFailed:
        {
        }
            break;
    }
}


上一篇下一篇

猜你喜欢

热点阅读