查看: 176|回复: 0

[IOS开发教程] iOS开发之邮件发送代码

发表于 2018-5-8 08:00:06

邮件发送功能是由MessageUI Framework提供的,这个框架是iPhone sdk中最简单的框。由一个类、一个视图控制器,一个protocol组成。

一、创建视图控制器:

?

MFMailComposeViewController *mc = [[MFMailComposeViewController alloc] init]; mc.mailComposeDelegate = self;

二、设置邮件主题:

[code][mc setToRecipients:[NSArray arrayWithObjects:@"zhuqi0@126.com", "@dave@iphonedevbook.com", nil];

2、设置cc

[code][mc setMessageBody:@"Hello, Joe!
What do you know?"
isHTML:YES];

五、添加附件

添加附件需要三个参数,一个是NSData类型的附件,一个是mime type,一个附件的名称。

  NSString *path = [[NSBundle mainBundle] pathForResource:@"blood_orange"      
      ofType:@"png"]; 
  NSData *data = [NSData dataWithContentsOfFile:path]; 
  [mc addAttachmentData:data mimeType:@"image/png" fileName:@"blood_orange"];

六、视图呈现

    [self presentModalViewController:mc animated:YES]; 
    [mc release];

七、视图控制器的委托方法

邮件视图控制器的委托方法包含在MFMailComposeViewControllerDelegate中,无论用户是否发送或取消发送,不论系统是否能够发送邮件,

方法 mailComposeController:didFinishWithResult:error: gets called都会被调用。

复制代码

- (void)mailComposeController:(MFMailComposeViewController*)controller  
        didFinishWithResult:(MFMailComposeResult)result  
        error:(NSError*)error {  
    switch (result) 
    { 
        case MFMailComposeResultCancelled: 
            NSLog(@"Mail send canceled..."); 
            break; 
        case MFMailComposeResultSaved: 
            NSLog(@"Mail saved..."); 
            break; 
        case MFMailComposeResultSent: 
            NSLog(@"Mail sent..."); 
            break; 
        case MFMailComposeResultFailed: 
            NSLog(@"Mail send errored: %@...", [error localizedDescription]); 
            break; 
        default: 
            break; 
    } 
    [self dismissModalViewControllerAnimated:YES]; 
}


相关帖子

回复

使用道具 举报