在我的应用中,我使用的是MFMailComposer。当我在没有邮件配置的情况下发送邮件(没有在设备上的邮件应用程序中输入邮件ID和密码)时,它会崩溃。
这是导致崩溃的代码行:
[self presentModalViewController:picker animated:YES];发布于 2011-09-09 13:31:27
-(void) showEmailModalView
{
NSLog(@"Start method <ExportStatisticsController> : <showEmailModalView> --");
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self; // <- very important step if you want feedbacks on what the
//user did with your email sheet
[picker setSubject:SendEmailSubject];
NSArray *torec=[[NSArray alloc] initWithObjects:SendEmailToEmailID, nil];
[picker setToRecipients:torec];
[torec release];
//------ message body ---
NSString *body =@"";
body=[NSString stringWithFormat:@"%@ From : %@\n",body, emailTextField.text];
body=[NSString stringWithFormat:@"%@ Subject : %@\n",body,subjectTextField.text];
//email contents
body = [NSString stringWithFormat:@"%@ Message : \n %@", body,messageBodyTextView.text];
[picker setMessageBody:body isHTML:NO]; // depends. Mostly YES, unless you want to send it as plain text (boring)
picker.navigationBar.barStyle = UIBarStyleBlack; // choose your style, unfortunately, Translucent colors behave quirky.
[self presentModalViewController:picker animated:YES];
[picker release];
NSLog(@"End method <ExportStatisticsController> : <showEmailModalView> --");
}
// Dismisses the email composition interface when users tap Cancel or Send. Proceeds to update the message field with the result of the operation.
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
{
NSLog(@"Start method <ExportStatisticsController> : <didFinishWithResult> --");
// Notifies users about errors associated with the interface
switch (result)
{
case MFMailComposeResultCancelled:
NSLog(@"Message MFMailComposeResultCancelled");
break;
case MFMailComposeResultSaved:
NSLog(@"Message MFMailComposeResultSaved");
break;
case MFMailComposeResultSent:
NSLog(@"Message sent Successfuly");
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Email" message:@"Mail Sent Successfully!"
delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
[alert show];
[alert release];
break;
case MFMailComposeResultFailed:
NSLog(@"Message MFMailComposeResultFailed");
break;
default:
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Email" message:@"Sending Failed - Unknown Error :-("
delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
[alert show];
[alert release];
}
break;
}
[self dismissModalViewControllerAnimated:YES];
NSLog(@"End method <ExportStatisticsController> : <didFinishWithResult> --");
}发布于 2011-09-09 13:34:16
你应该打电话给
[MFMailComposeViewController canSendMail]在呈现视图控制器之前,例如
if ([MFMailComposeViewController canSendMail]) {
MFMailComposeViewController *composer = [[MFMailComposeViewController alloc] init];
[self presentModalViewController:composer];
} else {
// Show error message maybe
}发布于 2012-01-02 18:23:12
那是真的。你必须配置你的邮件应用程序,并检查设备是否可以发送邮件。因为如果没有邮件应用程序,我们就可以通过模拟器发送邮件(我认为这是不可能的)。你的邮件的收件人必须知道他/她从哪里收到邮件,我认为发件人不能从代码中设置。我可能是错的,但这只是我的观点,因为我也在与同样的情况作斗争。
希望能有所帮助。
https://stackoverflow.com/questions/7357413
复制相似问题