Вопрос

I have seen a nice feature in the iOS App Scanner Pro. This app allows to send scanned documents as email attachments via the original mail app from Apple but without leaving the Scanner Pro app. I ask me how did they do it? Is there a special API call?

Это было полезно?

Решение

implement MFMailComposeViewControllerDelegate like this:

@interface YourViewController<MFMailComposeViewControllerDelegate >

Then where you want to instantiate this email viewcontroller just do the following:

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];
    }
}

Last implement mailComposerViewController delegate method

-(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
}

Другие советы

You can use UIActivityViewController, for example:

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

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

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

it gives user even more options, than just send email.

Yes, the so called UIActivityViewController. You use it like this:

    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 с атрибуция
Не связан с StackOverflow
scroll top