2012-04-27 102 views
2

在按照教程向我展示了如何实现应用内邮件后,它似乎在取消按钮和发送按钮关闭视图时失败。MFMailComposeViewController不会解雇

现在我已经看过,说我应该实现此方法,在这里谈几点看法:

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

但是,这并不解决问题,而不是在模拟器或在我的iPhone 4 - 该消息未得到虽然发送,但该观点并没有消除。

这是我到目前为止的代码:

#pragma mark - InApp Mail 
- (IBAction)openMail:(id)sender 
{ 
if ([MFMailComposeViewController canSendMail]) 
{ 
    MFMailComposeViewController *mailer = [[MFMailComposeViewController alloc] init]; 


    [mailer setSubject:@"iOS School - MultipleAlertViews"]; 

    NSArray *toRecipients = [NSArray arrayWithObjects:@"", nil]; 
    [mailer setToRecipients:toRecipients]; 

    // Attach an image to the email 
    NSString *pathFile01 = @"http://dl.dropbox.com/u/61711378/iOS%20School%20-%20Docs/MultipleAlertViewsVCh.pdf"; 
    NSURL *pdfURLFile01 = [NSURL URLWithString:pathFile01]; 
    NSData *pdfDataFile01 = [NSData dataWithContentsOfURL:pdfURLFile01]; 
    [mailer addAttachmentData:pdfDataFile01 mimeType:@"application/pdf" fileName:@"MultipleAlertViewsVCh.pdf"]; 

    NSString *pathFile02 = @"http://dl.dropbox.com/u/61711378/iOS%20School%20-%20Docs/MultipleAlertViewsVCm.pdf"; 
    NSURL *pdfURLFile02 = [NSURL URLWithString:pathFile02]; 
    NSData *pdfDataFile02 = [NSData dataWithContentsOfURL:pdfURLFile02]; 
    [mailer addAttachmentData:pdfDataFile02 mimeType:@"application/pdf" fileName:@"MultipleAlertViewsVCm.pdf"]; 


    NSString *emailBody = 
    @"Hello,<br/><br/>You requested code for this project, which you can now use in XCode <br/><br/> You will find 2 Attachements.<br/>One is the Header file and the other is the Implementation file.<br/><br/>Thank you for using this app, if you find it useful, don't forget to give it a Rating in the App Store.<br/><br/>Kind Regards,<br/>iOS School"; 


    [mailer setMessageBody:emailBody isHTML:YES]; 

    [self presentModalViewController:mailer animated:YES]; 
} 
else 
{ 
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Failure" 
                message:@"Your device doesn't support the composer sheet" 
                delegate:nil 
              cancelButtonTitle:@"OK" 
              otherButtonTitles:nil]; 
    [alert show]; 
} 
} 

- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error 
{ 
switch (result) 
{ 
    case MFMailComposeResultCancelled: 
     NSLog(@"Mail cancelled: you cancelled the operation and no email message was queued."); 
     break; 
    case MFMailComposeResultSaved: 
     NSLog(@"Mail saved: you saved the email message in the drafts folder."); 
     break; 
    case MFMailComposeResultSent: 
     NSLog(@"Mail send: the email message is queued in the outbox. It is ready to send."); 
     break; 
    case MFMailComposeResultFailed: 
     NSLog(@"Mail failed: the email message was not saved or queued, possibly due to an error."); 
     break; 
    default: 
     NSLog(@"Mail not sent."); 
     break; 
} 

// Remove the mail view 
[self dismissModalViewControllerAnimated:YES]; 
} 

正如你所看到的,我已经实施了 - (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error

但它无法正常工作 - 别的我失踪 - 我使用的是iOS 5.1

干杯杰夫

回答

11

你已经实现了委托方法,但都没有设置委托:

mailer.mailComposeDelegate = self; 

在创建mailer后。

+0

谢谢你,就是这样 - 我必须等10分钟才能接受你的答案,但稍后会做:-) – 2012-04-27 06:11:04

+0

奇怪,谢谢你,jrturton。 – Morkrom 2013-09-17 17:55:16