从我在 iOS 上的应用程序通过 mail.app 发送电子邮件
Posted
技术标签:
【中文标题】从我在 iOS 上的应用程序通过 mail.app 发送电子邮件【英文标题】:Send e-mail via mail.app from my app on iOS 【发布时间】:2014-05-12 13:02:17 【问题描述】:我在 ios App Scanner Pro 中看到了一个不错的功能。此应用程序允许通过 Apple 的原始邮件应用程序将扫描的文档作为电子邮件附件发送,但无需离开 Scanner Pro 应用程序。我问我他们是怎么做到的?是否有特殊的 API 调用?
【问题讨论】:
MFMailComposeViewController
是您要找的。span>
【参考方案1】:
像这样实现 MFMailComposeViewControllerDelegate:
@interface YourViewController<MFMailComposeViewControllerDelegate >
然后你想实例化这个电子邮件视图控制器只需执行以下操作:
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];
最后实现mailComposerViewController委托方法
-(void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
[self dismissViewControllerAnimated:YES completion:nil];
// or you can check for the status first and implement different task if you wish
【讨论】:
【参考方案2】:您可以使用UIActivityViewController,例如:
UIImage *image = [UIImage imageNamed:@"image_file_name"];
UIActivityViewController *activityViewController = [[UIActivityViewController alloc] initWithActivityItems:@[image] applicationActivities:nil];
[self presentViewController:activityViewController animated:YES completion:nil];
它为用户提供了更多的选择,而不仅仅是发送电子邮件。
【讨论】:
【参考方案3】:没错,就是所谓的UIActivityViewController。你可以这样使用它:
NSArray *itemsToShare = @[[NSString stringWithFormat:@"This is a string that is sent via mail as well."], NSURLtoTheFileToSend];
UIActivityViewController *activityVC = [[UIActivityViewController alloc] initWithActivityItems:itemsToShare applicationActivities:nil];
activityVC.excludedActivityTypes = @[UIActivityTypeAssignToContact]; // Here you can say what you dont want to give the opportunity to share.
activityVC.completionHandler = ^(NSString *activityType, BOOL completed)
if (completed)
UIAlertView *alert = [[UIAlertView alloc] init];
alert.title = @"Export successfull";
[alert show];
[alert performSelector:@selector(dismissWithClickedButtonIndex:animated:) withObject:nil afterDelay:1];
;
[self presentViewController:activityVC animated:YES completion:^];
【讨论】:
以上是关于从我在 iOS 上的应用程序通过 mail.app 发送电子邮件的主要内容,如果未能解决你的问题,请参考以下文章
AppleScript从Mail.app上的Imap文件夹中选择消息
复制 iOS Mail App 的 Compose Function 的风格