從我的iPad應用程序中,我想調用iPad的電子郵件應用程序與自定義正文文本。發件人和主題將爲空,我想設置的唯一參數是電子郵件的文本。我怎麼能這樣做?iOS SDK:如何調用電子郵件應用程序?
謝謝!
從我的iPad應用程序中,我想調用iPad的電子郵件應用程序與自定義正文文本。發件人和主題將爲空,我想設置的唯一參數是電子郵件的文本。我怎麼能這樣做?iOS SDK:如何調用電子郵件應用程序?
謝謝!
在Apple的文檔中查看MFMailComposeViewController
。您可以使用它像這樣:
MFMailComposeViewController *controller=[[MFMailComposeViewController alloc]init];
controller.delegate = self;
[controller setMessageBody:<#yourBody#> isHTML:<#isHTML#>];
[self presentModalViewController:controller animated:YES];
[controller release];
不要忘了在你的.h
文件中添加#import <MessageUI/MessageUI.h>
。它會調用委託中的方法告訴您何時取消或發送電子郵件(成功與否)。讓我知道這是否適合你。
爲什麼不直接在應用程序中打開電子郵件編輯器?
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];
NSString *body = @"Hello Mail";
NSString *mailtoURLString = [NSString stringWithFormat:@"mailto:?body=%@", [body stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:mailtoURLString]];
或者,如Mihai建議,看看MFMailComposeViewController
它允許您發送電子郵件而無需離開你的應用程序。
以下方法用於向用戶發送郵件。
-(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];
}
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];