MFMailComposeViewController - iPad

前端 未结 5 1662
失恋的感觉
失恋的感觉 2020-12-28 13:46

I\'ve setup a MFMailComposeViewController and it works just fine on the iPhone, but on the iPad it crashes, saying:

*** Terminating app due to uncaught excep         


        
相关标签:
5条回答
  • 2020-12-28 13:56

    Loos like MFMailComposeViewController was not created for some reason and thus has nil value. Check if it is nil before presenting it (although this workaround does not answer what went wrong here...).

    You should also perform the check if mail composer can send mail before trying to create and present it using +canSendMail method (it will return NO for example if no mail account set up on device):

     if ([MFMailComposeViewController canSendMail]){
        // Create and show composer
     }
     else{
       // Show some error message here
     }
    
    0 讨论(0)
  • 2020-12-28 14:01

    You must have to put check canSendMail, before you create the MFMailComposerViewController object, see the following comments from MFMailComposeViewController.h class:

    /*!
    @method     canSendMail
    @abstract   Returns <tt>YES</tt> if the user has set up the device for sending email.
    @discussion The client may continue to set the recipients and content if the return value was <tt>YES</tt>.  If <tt>NO</tt>
                was the result, the client has a couple options.  It may choose to simply notify the user of the inability to
                send mail, or it may issue a "mailto" URL via <tt>-[UIApplication openURL:]</tt>.
    */
    
    + (BOOL)canSendMail __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_3_0);
    

    Your object won't be initialized until your device is setup for sending mails.

    0 讨论(0)
  • 2020-12-28 14:01

    It's happen b'cause of your iOS default mail app did't configured yet with any mail id. so configure with any of your mail id and try.

    like this

    if ([MFMailComposeViewController canSendMail]){
        MFMailComposeViewController *controller = [[MFMailComposeViewController alloc] init];
        controller.mailComposeDelegate = self;
        [controller setToRecipients:[NSArray arrayWithObject:eMail]];
        [self presentViewController:controller animated:YES completion:nil];
    }
    else{
        UIAlertView *anAlert = [[UIAlertView alloc] initWithTitle:@"error" message:@"No mail account setup on device" delegate:self cancelButtonTitle:nil otherButtonTitles:nil];
        [anAlert addButtonWithTitle:@"Cancel"];
        [anAlert show];
    }
    

    hope its help you.

    0 讨论(0)
  • 2020-12-28 14:03
    if ([MFMailComposeViewController   canSendMail]){
        //execute  your    code 
    else{
        UIAlertView *anAlert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"No mail account setup on device" delegate:self cancelButtonTitle:nil otherButtonTitles:nil];     
        [anAlert addButtonWithTitle:@"OK"];
        [anAlert show];
    }
    

    If you are getting an alert, configure your mail account on your phone.

    0 讨论(0)
  • 2020-12-28 14:20

    No mail account set up on your testing device.

    if ([MFMailComposeViewController canSendMail]){
    
    //execute your code else{
    UIAlertView *anAlert = [[UIAlertView alloc] initWithTitle:@"error" message:@"No mail account setup on device" delegate:self cancelButtonTitle:nil otherButtonTitles:nil];
        [anAlert addButtonWithTitle:@"Cancel"];
        [anAlert show];
    }
    
    0 讨论(0)
提交回复
热议问题