MFMailComposeViewController не будет закрывать

После изучения руководства, которое показало мне, как реализовать In-App Mail, кажется, что при нажатии кнопки «Отмена» и кнопки «Отправить» происходит сбой представления.

Теперь я прочитал несколько комментариев здесь, в которых говорится, что я должен реализовать этот метод:

-(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.

Ура Джефф


person jwknz    schedule 27.04.2012    source источник


Ответы (1)


Вы реализовали метод делегата, но не установили делегат:

mailer.mailComposeDelegate = self;

После создания mailer.

person jrturton    schedule 27.04.2012
comment
Спасибо, это было так - мне нужно подождать 10 минут, прежде чем я смогу принять ваш ответ, но сделаю это позже :-) - person jwknz; 27.04.2012