2016-06-06 5 views
0

내 응용 프로그램에서 전자 메일을 보내려고합니다. 이메일 보내기 창이 열리면 수신자와 본문이 설정되지 않습니다. 나는 모든 필드에 누르려고 할 때마다 그들에게 내 응용 프로그램 충돌을 편집하고 나에게이 오류 제공합니다 :MFMailComposeViewController가 작동하지 않습니다.

***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!

내가 이메일 자습서를 살펴 보았습니다을 나는 내가 뭘 잘못 찾을 수 없습니다.

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:@[@"[email protected]"]]; 
    // 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]; 
} 

답변

1

귀하의 예외가 말한다 이유 : 코드 참조 추가 '코드를 사용하여 배경 큐에서 실행되는 경우에만 메인 스레드

확인에서 실행 :

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

대기열에 있습니다. 시도하지 않으려면 코드를 다음과 같이 포장하십시오.

+0

주 – pudm

1

배경 스레드에 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]; 
}