Sending Email using mailto: URLs

陌路散爱 提交于 2019-12-22 18:43:52

问题


Can someone help me with the following code? For sending email in iOS, is the below code a good one or should I use the MFMailComposeViewController than this?:

NSString *url = [NSString stringWithString: @"mailto:foo@example.com?cc=bar@example.com&subject=Greetings%20from%20Cupertino!&body=Wish%20you%20were%20here!"];
[[UIApplication sharedApplication] openURL: [NSURL URLWithString: url]];NSString *url = [NSString stringWithString: @"mailto:foo@example.com?cc=bar@example.com&subject=Greetings%20from%20Cupertino!&body=Wish%20you%20were%20here!"];
[[UIApplication sharedApplication] openURL: [NSURL URLWithString: url]];

Is it a reliable code for sending mail?


回答1:


If this is targeted for IOS 3.0+ then MFMailCompseViewController

    #import <MessageUI/MFMailComposeViewController.h>
  //  ....

        MFMailComposeViewController* controller = [[MFMailComposeViewController alloc] init];
        controller.mailComposeDelegate = self;
        [controller setSubject:@"My Subject"];
        [controller setMessageBody:@"Hello there." isHTML:NO]; 
        if (controller) [self presentModalViewController:controller animated:YES];
        [controller release];

Then the user does the work and you get the delegate callback in time:

 - (void)mailComposeController:(MFMailComposeViewController*)controller  
              didFinishWithResult:(MFMailComposeResult)result 
                            error:(NSError*)error;
 {
      if (result == MFMailComposeResultSent) {
          NSLog(@"sent");
        }
    [self dismissModalViewControllerAnimated:YES];
 }



回答2:


You really should use MFMailComposeViewController. It keeps you in the app and makes your code more readable.




回答3:


To expand on the answers provided, I'd like to add that there is one benefit to the mailto approach, and that is that You don't really have to check if the user is able to send emails. If the user is not able to, it will prompt the user with the email wizard that will allow him/her to set up an email account with the default apple mail app.

In case of the MFMailComposeViewController, you should always check if the user can send emails with the canSendMail method, and act accordingly.

I'd like to also note that the mailto approach does not allow you to set a delegate in a straight forward way, making the error handling a little more tricky.



来源:https://stackoverflow.com/questions/9012957/sending-email-using-mailto-urls

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!