自iOS 9运行时以来,MFMailComposeViewController在旧的iOS代码中无效

时间:2016-06-23 12:43:19

标签: ios4 ios9

这是非常古老的iOS4代码,并尝试在iOS9中邮件组合控制器崩溃时找到错误

#pragma mark - Export song
- (IBAction)tapExportButton:(id)sender
{
    [self dismissAllPopovers];

    if ([MFMailComposeViewController canSendMail])
    {
        MFMailComposeViewController *mailer = [[MFMailComposeViewController alloc] init];
        mailer.mailComposeDelegate = self;
        NSString *songDescription = self.songViewController.song.getSongDescription;
        [mailer setSubject:[NSString stringWithFormat:@"Chords+Lyrics %@: %@",NSLocalizedString(@"Song", nil),songDescription]];

        // Build the attachment. The current song in import/export format.
        FileExporter  *fileExporter = [[FileExporter alloc]init];

        NSData *attachData = [[NSData alloc]init];
        if ([fileExporter encodeExportFormat:
             [fileExporter songIntoExportFormat:self.songViewController.song]
                      intoMailAttachment:&attachData]) {
            [mailer addAttachmentData:attachData mimeType:@"text/plain" fileName:[NSString stringWithFormat:@"%@.song",songDescription]];
    }

        NSString *emailBody = [NSString stringWithFormat: NSLocalizedString(@"This song is send to you from the Chords+Lyrics App.", nil) ];
        [mailer setMessageBody:emailBody isHTML:NO];
        mailer.modalPresentationStyle = UIModalTransitionStyleCoverVertical;


        [self presentViewController:mailer animated:YES completion: NULL];

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

有一些解决方案MFMailComposeViewController throws an error only in iOS 9 关于UINavigationBar中的字体问题,但这对我不起作用......

日志:

2016-06-23 14:23:59.883  +[NSManagedObjectContext(ActiveRecord) contextWithStoreCoordinator:](5d67a58)     Creating MOContext  *** On Main Thread ***
2016-06-23 14:23:59.901  -[SongViewController viewDidLoad]: called
2016-06-23 14:24:02.034  viewServiceDidTerminateWithError: Error Domain=_UIViewServiceInterfaceErrorDomain Code=3 "(null)" UserInfo={Message=Service Connection Interrupted}
2016-06-23 14:24:02.537  -[RootViewController mailComposeController:didFinishWithResult:error:]: Mail cancelled: you cancelled the operation and no email message was queued.
2016-06-23 14:24:02.537  Trying to dismiss the presentation controller while transitioning already. (<_UIFullscreenPresentationController: 0x7fed2a8f4a20>)
2016-06-23 14:24:02.540  transitionViewForCurrentTransition is not set, presentation controller was dismissed during the presentation? (<_UIFullscreenPresentationController: 0x7fed2a8f4a20>)

此致 jr00n

1 个答案:

答案 0 :(得分:0)

// 1.将此代码写入send button func。

@IBAction func sendEmail(_ sender: Any) {
    let mailComposer = MFMailComposeViewController()
    if  MFMailComposeViewController.canSendMail() {
        mailComposer.mailComposeDelegate = self
        mailComposer.setToRecipients([emailText.text!])
        mailComposer.setSubject(subjectText.text!)
        mailComposer.setMessageBody(bodyText.text, isHTML: true)
    } else {
        print("Mail composer is not able to send Email")
    }
}

// 2.实施 MFMailComposeViewControllerDelegate 协议

func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
    dismiss(animated: true, completion: nil)
}