Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I am a bit confused on how to display mail composer when I click the email option in UIActionSheet. Here's my sample code:

-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
    if(buttonIndex == 0)
    {
        NSString *emailTitle = @"Test Email";
        NSString *messageBody = @"iOS programming is so fun!";
        NSArray *toRecipents = [NSArray arrayWithObject:@"support@email.com"];
        MFMailComposeViewController *mc = [[MFMailComposeViewController alloc] init];
        mc.mailComposeDelegate = self;
        [mc setSubject:emailTitle];
        [mc setMessageBody:messageBody isHTML:NO];
        [mc setToRecipients:toRecipents];
        [self presentViewController:mc animated:YES completion:NULL];
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
179 views
Welcome To Ask or Share your Answers For Others

1 Answer

Make sure you add to your .h file and import the MessageUI.framework

Here is exactly what your looking for. I use this often. By the way UIActionSheets are deprecated in iOS 8. Here you go though:

.h

ViewController : UIViewController <MFMailComposeViewControllerDelegate>

.m

- (IBAction)showActionSheet:(id)sender {

UIActionSheet *moreActions = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"Add To Favorites", @"Search",@"Email", nil];
[moreActions showInView:self.view];
}

-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {

switch (buttonIndex) {
    case 0: [self addToFav:self];
        break;
    case 1: ; [self popSearchBar:self];
        break;
    case 2: { [self sendEmail];
        break;
    }
        break;
}
}
- (void)sendEmail {
NSString *emailTitle = @"This is email title";
// Email Content as for HTML
NSString *messageBody = [NSString stringWithFormat:@"I may have found a missing document in your catalog. I tried opening :<p><strong><font color="red"> %@ </font><br>'%@'</strong></p> with no results. Can I have a dollar for reporting this?", self.title, self.titleString];
// To address
NSArray *toRecipents = [NSArray arrayWithObject:@"youremail@google.com"];

MFMailComposeViewController *mc = [[MFMailComposeViewController alloc] init];
mc.mailComposeDelegate = self;
[mc setSubject:emailTitle];
[mc setMessageBody:messageBody isHTML:YES];
[mc setToRecipients:toRecipents];

// Present mail view controller on screen
[self presentViewController:mc animated:YES completion:NULL];

}
 - (void) mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult: (MFMailComposeResult)result error:(NSError *)error
{
switch (result)
{
    case MFMailComposeResultCancelled:
        NSLog(@"Mail cancelled");
        [self dismissViewControllerAnimated:YES completion:NULL];
        break;
    case MFMailComposeResultSaved:
        NSLog(@"Mail saved");
        [self dismissViewControllerAnimated:YES completion:NULL];
        break;
    case MFMailComposeResultSent:
        NSLog(@"Mail sent");
        [self dismissViewControllerAnimated:YES completion:NULL];
        break;
    case MFMailComposeResultFailed:
        NSLog(@"Mail sent failure: %@", [error localizedDescription]);
        break;
    default:
        break;
}
// Close the Mail Interface
[self dismissViewControllerAnimated:YES completion:NULL];
}

Also note that some people are having issues with simulator emails. try it on a device before you give up.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...