【发布时间】: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