简体   繁体   中英

MFMailComposeViewController not working

I am trying to send an email from my app. When the send email window opens, the recipients and body are not set. Whenever I try to press on any of the fields to edit them my app crashes and gives me this error:

***Assertion failure in -[UIKeyboardTaskQueue waituntilalltasksarefinished], /sourcuecache/UIKIt_Sim/UIKit-3318.16.14/KeyboardTaskQueue.m:374 Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Only run on the main thread!

I have looked at the email tutorials and I can't find what I am doing wrong. Code added for reference:

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];
}

and

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

Your exception says, reason: 'Only run on the main thread

Check if your code runs on a background queue by using :

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

It shoulde be running in the main queue. If not try wrapping your code in :

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

You cannot present UI on background threads. In general, you shouldn't do any UIKit work on background threads. You are on the right track by calling heavy imaging APIs like ( UIImagePNGRepresentation ) on the background thread. After you are done with creating screen shots etc, you need to dispatch to main queue for presenting the mail view controller.

// 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];
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM