【问题标题】:MFMailComposeViewController not workingMFMailComposeViewController 不工作
【发布时间】:2016-06-06 17:47:12
【问题描述】:

我正在尝试从我的应用程序发送电子邮件。当发送电子邮件窗口打开时,收件人和正文未设置。每当我尝试按下任何字段来编辑它们时,我的应用程序都会崩溃并给我这个错误:

***-[UIKeyboardTaskQueue waituntilalltasksarefinished] 中的断言失败,/sourcuecache/UIKIt_Sim/UIKit-3318.16.14/KeyboardTaskQueue.m:374 由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“仅在主线程上运行!

我查看了电子邮件教程,但找不到我做错了什么。添加代码供参考:

if ([MFMailComposeViewController canSendMail]) {
    // Get current date/time
    NSDate *date = picker.date;
    MFMailComposeViewController *mailer = [[MFMailComposeViewController alloc]init];
    mailer.mailComposeDelegate = self;
    mailer.modalPresentationStyle = UIModalPresentationCurrentContext;
    // Set title
    NSDateFormatter *dateFmt = [[NSDateFormatter alloc] init];
    [dateFmt setDateStyle:NSDateFormatterMediumStyle];
    [mailer setSubject:[NSString stringWithFormat:@"test email: sent on %@", [dateFmt stringFromDate:date]]];
    // Set recipient
    [mailer setToRecipients:@[@"testingEmail@example.com"]];
    // Set attachment
    CGRect r = CGRectMake(0, 0, 200, 200);
    if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)])
        UIGraphicsBeginImageContextWithOptions(r.size, NO, [UIScreen mainScreen].scale);
    else
        UIGraphicsBeginImageContext(r.size);

    CGContextRef c = UIGraphicsGetCurrentContext();
    CGContextTranslateCTM(UIGraphicsGetCurrentContext(), - mapview.center.x + r.size.width / 2.f, - mapview.center.y + r.size.height / 2.f);
    [mapholder.layer renderInContext:c];
    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    NSData *imageData = UIImagePNGRepresentation(img);
    [mailer addAttachmentData:imageData mimeType:@"image/png" fileName:@"map"];
    // Set email body
    [dateFmt setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    CLLocationCoordinate2D loc = mapview.centerCoordinate;
    NSString *emailBody = [NSString stringWithFormat:@"BRO it is Time: %@\n Location: %f,%f", [dateFmt stringFromDate:date], loc.latitude, loc.longitude];
    [mailer setMessageBody:emailBody isHTML:NO];
    [self presentViewController:mailer animated:YES completion:nil];
}

-(void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error {
    [self dismissModalViewControllerAnimated:YES];
}

【问题讨论】:

    标签: ios objective-c email mfmailcomposeviewcontroller


    【解决方案1】:

    你的例外说,原因:'只在主线程上运行

    使用以下命令检查您的代码是否在后台队列中运行:

    NSLog(@"%@",[NSOperationQueue currentQueue]);
    

    它应该在主队列中运行。如果不尝试将代码包装在:

    dispatch_async(dispatch_get_main_queue(), ^{
       ...
    });    
    

    【讨论】:

    • 它确实在 main 中运行
    【解决方案2】:

    您不能在后台线程上呈现 UI。一般来说,你不应该在后台线程上做任何UIKit 工作。通过在后台线程上调用 (UIImagePNGRepresentation) 等重度成像 API,您走在了正确的轨道上。在您完成创建屏幕截图等之后,您需要调度到主队列以呈现邮件视图控制器。

    // Create mail composer with screenshots etc
    // ...
    
    // Now present mail composer on "MAIN" thread
    dispatch_async(dispatch_get_main_queue(), ^{
        [self presentViewController:mailer animated:YES completion:nil];
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-05-05
      • 2014-11-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-10
      • 1970-01-01
      相关资源
      最近更新 更多