通过mail.app从我在iOS上的应用程序发送电子邮件

时间:2014-05-12 13:02:17

标签: ios email

我在iOS App Scanner Pro中看到了一个不错的功能。此应用程序允许通过Apple的原始邮件应用程序将扫描的文档作为电子邮件附件发送,但不会离开Scanner Pro应用程序。我问他们是怎么做到的?是否有特殊的API调用?

3 个答案:

答案 0 :(得分:1)

您可以使用UIActivityViewController,例如:

UIImage *image = [UIImage imageNamed:@"image_file_name"];

UIActivityViewController *activityViewController = [[UIActivityViewController alloc] initWithActivityItems:@[image] applicationActivities:nil];

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

它为用户提供了更多选项,而不仅仅是发送电子邮件。

答案 1 :(得分:1)

像这样实现MFMailComposeViewControllerDelegate:

@interface YourViewController<MFMailComposeViewControllerDelegate >

然后,您想要实例化此电子邮件viewcontroller,只需执行以下操作:

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 :(得分:0)

是的,所谓的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:^{}];