应用程序崩溃在设备中的电子邮件

时间:2011-09-05 12:34:01

标签: iphone email-integration mfmailcomposeviewcontroller

我添加了"电子邮件"在我的iPhone应用程序中,它在iPhone上崩溃,但在模拟器上工作。请指导我为什么它会在设备上崩溃

这是代码

-(IBAction)sendMail{
    MFMailComposeViewController *controller = [[MFMailComposeViewController alloc] init];
    controller.mailComposeDelegate = self;
    [controller setSubject:@"Contact"];

    [controller setToRecipients:[NSArray arrayWithObject:@"Contact@BettorsSidekick.com"]];
    [controller setMessageBody:@"" isHTML:NO];
    [self presentModalViewController:controller animated:YES];
    [controller release];
}

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

3 个答案:

答案 0 :(得分:2)

使用此代码可以正常工作。

-(IBAction)Btn_EmailPressed:(id)sender{
    if (![MFMailComposeViewController canSendMail]) {
        UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"Alert" message:@"Email cannot be configure." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alert show];
        [alert release];
        return;
    }else {
        picker = [[MFMailComposeViewController alloc] init];
        picker.mailComposeDelegate=self;
        [picker setToRecipients:nil];
        [picker setSubject:@"Email"];
        [picker setMessageBody:nil isHTML:NO];
        NSArray *toRecipients = [[NSArray alloc] initWithObjects:lblSite.text,nil];
        [picker setToRecipients:toRecipients];
        [self presentModalViewController:picker animated:YES];
    }
}


- (void)mailComposeController:(MFMailComposeViewController*)mailController didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error {
    NSString *msg1;
    switch (result)
    {
        case MFMailComposeResultCancelled:
            msg1 =@"Sending Mail is cancelled";
            break;
        case MFMailComposeResultSaved:
            msg1=@"Sending Mail is Saved";
            break;
        case MFMailComposeResultSent:
            msg1 =@"Your Mail has been sent successfully";
            break;
        case MFMailComposeResultFailed:
            msg1 =@"Message sending failed";
            break;
        default:
            msg1 =@"Your Mail is not Sent";
            break;
    }
    UIAlertView *mailResuletAlert = [[UIAlertView alloc]initWithFrame:CGRectMake(10, 170, 300, 120)];
    mailResuletAlert.message=msg1;
    mailResuletAlert.title=@"Message";
    [mailResuletAlert addButtonWithTitle:@"OK"];
    [mailResuletAlert show];
    [mailResuletAlert release];
    [self dismissModalViewControllerAnimated:YES];  
}

答案 1 :(得分:2)

如果你没有在你的iphone设备中配置任何电子邮件帐户可能是这可能是崩溃的原因,因为当你调用mfmailcomposer它将在模拟器上工作但它将在设备上失败并且结果将是崩溃因此配置邮件然后设备尝试上面的代码。

答案 2 :(得分:1)

试试这个

MFMailComposeViewController *controller = [[MFMailComposeViewController alloc] init];


    if(controller && [MFMailComposeViewController canSendMail]){

        controller.mailComposeDelegate = self;
        [controller setSubject:@"Contact"];

        [controller setToRecipients:[NSArray arrayWithObject:@"Contact@BettorsSidekick.com"]];
        [controller setMessageBody:@"" isHTML:NO];
        [self presentModalViewController:controller animated:YES];


    }

if (controller) {

    [controller release];        

}