2011-09-05 86 views
0

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

这是崩溃的代码

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

    [controller setToRecipients:[NSArray arrayWithObject:@"[email protected]"]]; 
    [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]; 
} 
+0

任何可能帮助我们的崩溃消息? – phi

+0

小心发布错误消息? – Devraj

+0

尝试清除MailController之前的mailComposeDelegate – Victor

回答

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 [email protected]"Sending Mail is cancelled"; 
      break; 
     case MFMailComposeResultSaved: 
      [email protected]"Sending Mail is Saved"; 
      break; 
     case MFMailComposeResultSent: 
      msg1 [email protected]"Your Mail has been sent successfully"; 
      break; 
     case MFMailComposeResultFailed: 
      msg1 [email protected]"Message sending failed"; 
      break; 
     default: 
      msg1 [email protected]"Your Mail is not Sent"; 
      break; 
    } 
    UIAlertView *mailResuletAlert = [[UIAlertView alloc]initWithFrame:CGRectMake(10, 170, 300, 120)]; 
    mailResuletAlert.message=msg1; 
    [email protected]"Message"; 
    [mailResuletAlert addButtonWithTitle:@"OK"]; 
    [mailResuletAlert show]; 
    [mailResuletAlert release]; 
    [self dismissModalViewControllerAnimated:YES]; 
} 
1

试试这个

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


    if(controller && [MFMailComposeViewController canSendMail]){ 

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

     [controller setToRecipients:[NSArray arrayWithObject:@"[email protected]"]]; 
     [controller setMessageBody:@"" isHTML:NO]; 
     [self presentModalViewController:controller animated:YES]; 


    } 

if (controller) { 

    [controller release];   

} 
2

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

相关问题