简体   繁体   English

适用于iOS 5的MFMailComposeViewController:教程或示例

[英]MFMailComposeViewController for iOS 5: Tutorial or Example

Does anyone have a decent tutorial on how to implement mail composer for ios 5 either programmatically or with segues? 有没有人有一个不错的教程,介绍如何以编程方式或使用segues为iOS 5实现Mail Composer? Most of the tutorials that I found online are from old iOS versions. 我在网上找到的大多数教程都来自旧的iOS版本。 Thanks! 谢谢!

You could do something like this: 您可以执行以下操作:

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:@"yourrecipent@domain.com"]];

    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.");

At first you have to add "MFMailComposeViewControllerDelegate" to interface section. 首先,您必须在接口部分添加“ MFMailComposeViewControllerDelegate”。

Also you have to add procedure to get response after user taps "Send button" 另外,您还必须添加过程以在用户点击“发送按钮”后获得响应

- (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];

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM