首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >MFMailComposer崩溃

MFMailComposer崩溃
EN

Stack Overflow用户
提问于 2011-09-09 13:17:42
回答 3查看 583关注 0票数 0

在我的应用中,我使用的是MFMailComposer。当我在没有邮件配置的情况下发送邮件(没有在设备上的邮件应用程序中输入邮件ID和密码)时,它会崩溃。

这是导致崩溃的代码行:

代码语言:javascript
复制
[self presentModalViewController:picker animated:YES];
EN

回答 3

Stack Overflow用户

发布于 2011-09-09 13:31:27

代码语言:javascript
复制
-(void) showEmailModalView
{
    NSLog(@"Start method <ExportStatisticsController> : <showEmailModalView>  --");


    MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
    picker.mailComposeDelegate = self; // &lt;- 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>  --");
}
票数 0
EN

Stack Overflow用户

发布于 2011-09-09 13:34:16

你应该打电话给

代码语言:javascript
复制
[MFMailComposeViewController canSendMail]

在呈现视图控制器之前,例如

代码语言:javascript
复制
if ([MFMailComposeViewController canSendMail]) {
    MFMailComposeViewController *composer = [[MFMailComposeViewController alloc] init];
    [self presentModalViewController:composer];
} else {
    // Show error message maybe
}
票数 0
EN

Stack Overflow用户

发布于 2012-01-02 18:23:12

那是真的。你必须配置你的邮件应用程序,并检查设备是否可以发送邮件。因为如果没有邮件应用程序,我们就可以通过模拟器发送邮件(我认为这是不可能的)。你的邮件的收件人必须知道他/她从哪里收到邮件,我认为发件人不能从代码中设置。我可能是错的,但这只是我的观点,因为我也在与同样的情况作斗争。

希望能有所帮助。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7357413

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档