2011-09-05 84 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它將在模擬器工作,但它會失敗在設備和結果將是一個崩潰做的原因在設備上配置郵件,然後嘗試上面的代碼。

相關問題