2012-09-12 18 views
0

有没有人有一个体面的教程,如何以编程方式或与segues实现邮件编辑器的ios 5?我在网上找到的大多数教程都来自旧的iOS版本。谢谢!用于iOS 5的MFMailComposeViewController:教程或示例

+0

的实施并没有改变多少,如果在所有之后。它只是创建视图控制器并在最简单的情况下以模态方式调用它。基本上就像你会模式地推控制器,而不使用iOS故事板。 –

回答

5

你可以做这样的事情:

if([MFMailComposeViewController canSendMail]) 
{ 
    MFMailComposeViewController *mailController = [[MFMailComposeViewController alloc] init]; 
    [mailController setMailComposeDelegate:self]; 
    [mailController setSubject:@"Mail Subject!"]; 
    [mailController setMessageBody:@"Here is your message body" isHTML:NO]; 
    [mailController setToRecipients:[NSArray arrayWithObject:@"[email protected]"]]; 

    NSData *imageData = UIImageJPEGRepresentation(imageToUpload, 1.0f); 
    if(imageData.length) 
    { 
     [mailController addAttachmentData:imageData mimeType:@"image/jpeg" fileName:@"Your_Photo.jpg"]; 
     [self presentModalViewController:mailController animated:YES]; 
    } 
    else 
    { 
     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Invalid Image" message:@"The image couldn't be converted." delegate:nil cancelButtonTitle:nil otherButtonTitles:@"Okay", nil]; 
     [alert show]; 
    } 
} 
else NSLog(@"Hah. No mail for you."); 
+0

之前做这个东西,你必须检查[MFMailComposeViewController canSendMail]否则你会遇到崩溃的情况下,邮件没有设置 –

2

首先,你必须添加“MFMailComposeViewControllerDelegate”的接口部分。

你也需要添加过程,以获得响应用户点击“发送按钮”

- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error 
{ 
    switch (result) { 
     case MFMailComposeResultSent: 
      NSLog(@"You sent the email."); 
      break; 
     case MFMailComposeResultSaved: 
      NSLog(@"You saved a draft of this email"); 
      break; 
     case MFMailComposeResultCancelled: 
      NSLog(@"You cancelled sending this email."); 
      break; 
     case MFMailComposeResultFailed: 
      NSLog(@"Mail failed: An error occurred when trying to compose this email"); 
      break; 
     default: 
      NSLog(@"An error occurred when trying to compose this email"); 
      break; 
    } 

    [self dismissViewControllerAnimated:YES completion:NULL]; 

}