iOS SDK: How to invoke E-mail application?

左心房为你撑大大i 提交于 2019-12-03 19:40:25

问题


From within my iPad application I would like to invoke iPad's E-mail app with custom body text. Senders and subject will be empty, the only parameter I would like to set is the text of the e-mail message. How could I do that?

Thanks!


回答1:


Why not just open an email message composer inside your app?

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

[mailController setSubject:@"my subject"];                  
[mailController setMessageBody:@"my message" isHTML:NO];

mailController.mailComposeDelegate = self;

UINavigationController *myNavController = [myViewController navigationController];

if ( mailController != nil ) {
    if ([MFMailComposeViewController canSendMail]){
        [myNavController presentModalViewController:mailController animated:YES];
    }
}

[mailController release];



回答2:


Take a look at MFMailComposeViewController in Apple's documentation. You can use it like this:

MFMailComposeViewController *controller=[[MFMailComposeViewController alloc]init];
controller.delegate = self;
[controller setMessageBody:<#yourBody#> isHTML:<#isHTML#>];
[self presentModalViewController:controller animated:YES];
[controller release];

Don't forget to add #import <MessageUI/MessageUI.h> in your .h file. It will call methods in your delegate to let you know when it was canceled or the email was sent (successfully or not). Let me know if that works for you.




回答3:


NSString *body = @"Hello Mail";
NSString *mailtoURLString = [NSString stringWithFormat:@"mailto:?body=%@", [body stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:mailtoURLString]];

Or, as Mihai suggested, take a look at MFMailComposeViewController which allows you to send mail without leaving your app.




回答4:


Following method is use for sending mail to user.

-(void)sendMail:(UIImage *)image
{
    MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
    picker.mailComposeDelegate = self;

    // Set the subject of email
    [picker setSubject:@"Picture from my iPhone!"];

    // Add email addresses  
    // Notice three sections: "to" "cc" and "bcc"
    [picker setToRecipients:[NSArray arrayWithObjects:@TO mailID1",@TO mailID2", nil]];
    [picker setCcRecipients:[NSArray arrayWithObject:@"CC MailID"]];
    [picker setBccRecipients:[NSArray arrayWithObject:@"BCC Mail ID"]];

    // Fill out the email body text
    NSString *emailBody = @"I just took this picture, check it out.";

    // This is not an HTML formatted email
    [picker setMessageBody:emailBody isHTML:NO];

    // Create NSData object as PNG image data from camera image
    NSData *data = UIImagePNGRepresentation(image);

    // Attach image data to the email   
    // 'CameraImage.png' is the file name that will be attached to the email 
    [picker addAttachmentData:data mimeType:@"image/png" fileName:@"CameraImage"];

    // Show email view
    [self presentModalViewController:picker animated:YES];

    // Release picker   
    [picker release];
}



回答5:


NSString *textToShare = @"http:yourmail.com/";

NSArray *objectsToShare = @[textToShare];

UIActivityViewController *activityVC = [[UIActivityViewController alloc] initWithActivityItems:objectsToShare applicationActivities:nil];

NSArray *excludeActivities = @[UIActivityTypeAirDrop,UIActivityTypeSaveToCameraRoll];

activityVC.excludedActivityTypes = excludeActivities;
[activityVC setValue:@"yourmail" forKey:@"subject"];

[self presentViewController:activityVC animated:YES completion:nil];


来源:https://stackoverflow.com/questions/7120649/ios-sdk-how-to-invoke-e-mail-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!