簡體   English   中英

通過mail.app從我在iOS上的應用程序發送電子郵件

[英]Send e-mail via mail.app from my app on iOS

我在iOS App Scanner Pro中看到了一個不錯的功能。 此應用程序允許通過Apple的原始郵件應用程序將掃描的文檔作為電子郵件附件發送,但不會離開Scanner Pro應用程序。 我問他們是怎么做到的? 是否有特殊的API調用?

您可以使用UIActivityViewController ,例如:

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

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

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

它為用戶提供了比發送電子郵件更多的選擇。

像這樣實現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
}

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM