swift - 从Sprite Kit解雇邮件视图控制器

时间:2016-04-05 01:25:21

标签: swift email sprite-kit

我正在尝试向Sprite Kit游戏添加发送电子邮件按钮。我可以显示电子邮件对话框。但如果我点击取消,应用程序将崩溃或什么都不做。如果我点击发送,电子邮件将发送,但对话框保持不变。我无法启动mailComposeController功能......请帮忙!

代码:

import Foundation
import UIKit
import MessageUI

class MailViewController: UIViewController, MFMailComposeViewControllerDelegate {

let systemVersion = UIDevice.currentDevice().systemVersion
let devicemodel = UIDevice.currentDevice().model
let appVersion = NSBundle.mainBundle().infoDictionary?["CFBundleShortVersionString"] as! String
let appBuild = NSBundle.mainBundle().infoDictionary?["CFBundleVersion"] as! String
let myrootview2 = UIApplication.sharedApplication().keyWindow?.rootViewController
let mailComposerVC = MFMailComposeViewController()


override func viewDidLoad() {
    super.viewDidLoad()
}

func sendEmailButtonTapped(sender: AnyObject) {
    let mailComposeViewController = configuredMailComposeViewController()
    if MFMailComposeViewController.canSendMail() {
        self.view.window?.rootViewController = mailComposerVC
        print("This is the rootview2: \(myrootview2)")
        myrootview2!.presentViewController(mailComposeViewController, animated: true, completion: nil)
    } else {
        self.showSendMailErrorAlert()
    }
}

func configuredMailComposeViewController() -> MFMailComposeViewController {
    var msgbody: String

    mailComposerVC.mailComposeDelegate = self
    msgbody = "\n\nDevice: \(devicemodel)\niOS Version: \(systemVersion)\nApp Version: \(appVersion)\nApp Build Number: \(appBuild)\n"
    mailComposerVC.setToRecipients(["test1@test.com"])
    mailComposerVC.setSubject("test subject")
    mailComposerVC.setMessageBody(msgbody, isHTML: false)
    //print(mailComposerVC)
    return mailComposerVC
}

func showSendMailErrorAlert() {
    let sendMailErrorAlert = UIAlertView(title: "Could Not Send Email", message: "Your device could not send e-mail.  Please check e-mail configuration and try again.", delegate: self, cancelButtonTitle: "OK")
    sendMailErrorAlert.show()
}





// THIS DOESN'T GET CALLED WHEN SENDING OR CANCELLING EMAIL!
func mailComposeController(controller: MFMailComposeViewController, didFinishWithResult result: MFMailComposeResult, error: NSError?) {

    let test1 = result.rawValue
    print(test1)
    print(controller)
    print(self)
    print(myrootview2)
}

1 个答案:

答案 0 :(得分:0)

问题是你将mailVC作为根视图,你必须在你的视图上呈现它,如下所示

@IBAction func sendEmailButtonTapped(sender: AnyObject) {
    let mailComposeViewController = configuredMailComposeViewController()

if MFMailComposeViewController.canSendMail() {

    self.presentViewController(mailComposeViewController, animated: true, completion: nil)
} else {
    self.showSendMailErrorAlert()
}
}


 func mailComposeController(controller: MFMailComposeViewController!, didFinishWithResult result: MFMailComposeResult, error: NSError!) {
      controller.dismissViewControllerAnimated(true, completion: nil)
}