【发布时间】:2023-03-19 00:43:01
【问题描述】:
我想从我的 iPad 应用程序中调用带有自定义正文文本的 iPad 电子邮件应用程序。发件人和主题将为空,我要设置的唯一参数是电子邮件的文本。我怎么能这样做?
谢谢!
【问题讨论】:
我想从我的 iPad 应用程序中调用带有自定义正文文本的 iPad 电子邮件应用程序。发件人和主题将为空,我要设置的唯一参数是电子邮件的文本。我怎么能这样做?
谢谢!
【问题讨论】:
为什么不直接在您的应用中打开电子邮件消息编辑器?
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];
【讨论】:
查看 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>。它将调用您的委托中的方法,让您知道它何时被取消或电子邮件已发送(成功与否)。让我知道这是否适合你。
【讨论】:
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];
【讨论】: