title: swift - dismissing mail view controller from Sprite Kit
body: <p>I am trying to add a send email button to a Sprite Kit game. I can get the email dialog to show up. But if I hit cancel, the app will crash or do nothing. If I hit send, the email will send, but the dialog stays. I cannot get the <code>mailComposeController</code> function to fire...please help!</p>
  
  <p><strong>Code:</strong></p>
  
  <pre><code>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() -&gt; 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)
  }
  </code></pre>