2016-11-24 20 views
2

我的应用程序可以打开PDF文件。所以我将它注册为PDF文件。当我从邮件应用程序打开PDF文件时,我的应用程序会被调用,并且会调用以下功能:回到上一个应用程序(Swift ios)

func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool { 
     let rect = app.keyWindow?.rootViewController?.view.bounds 
     viewController.view.backgroundColor = UIColor.red 
     let picker = UIPickerView(frame: CGRect(x: 0, y: 100, width: (rect?.width)!, height: (rect?.height)!/3)) 
     picker.delegate = self 
     picker.dataSource = self 
     viewController.view.addSubview(picker) 
     let okButton = UIButton(frame: CGRect(x: 0, y: 100 + ((rect?.height)!/3), width: ((rect?.width)!/2) - 20, height: 30)) 
     okButton.setTitle("Ok", for: .normal) 
     okButton.addTarget(self, action: #selector(AppDelegate.endApp), for: .touchUpInside) 
     viewController.view.addSubview(okButton) 
     app.keyWindow?.rootViewController?.present(viewController, animated: true, completion: nil) 

     return true 
    } 

This Works!如果用户点击ok按钮,我想回到邮件应用程序。我怎样才能做到这一点?我读过,那个苹果不允许你在应用程序之间切换。但是WhatsApp正在这样做。

我取出的viewController试了一下:

viewController.view.removeFromSuperview() 

但后来我只得到一个黑色的屏幕。

+0

这不会帮助你回到邮件应用程序viewController.view.removeFromSuperview() – Hasya

+0

这不像你的答案帮助我:)。你有解决这个问题的办法吗? – ReasyEasyPeasy

回答

-1

我用下面的代码做它:

viewController.dismiss(animated: false, completion: nil) 
let mailURL = NSURL(string: "message://")! 
     if UIApplication.shared.canOpenURL(mailURL as URL) { 
      UIApplication.shared.openURL(mailURL as URL) 
     } 

那是不够好,对我来说。

谢谢@Jeremy你的链接帮助了我!

1

启动其他应用程序的唯一方法是使用它们的URL方案,打开邮件的唯一方法是使用mailto:方案,该方案将始终打开撰写视图。

let email = "[email protected]" 

let url = URL(string: "mailto:\(email)") 
UIApplication.sharedApplication().openURL(url) 

编辑:此answer可以帮助你

+0

那么Whatsapp是如何做的呢? – ReasyEasyPeasy

+0

我编辑我的答案,检查链接 – Jeremy

-1

这不会帮助你回去邮件应用viewController.view.removeFromSuperview()

它只会删除您viewcontroler和黑色window是显示了意见。

即使您使用openURL,也无法回到邮件应用程序。

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"mailto:"]]; 

这将打开电子邮件作曲家,但实际上你将无法回到邮件应用程序。

斯威夫特

let url = URL(string: "mailto:\[email protected]") 
UIApplication.sharedApplication().openURL(url) 

与发件人为[email protected]这将打开邮件的作曲家。

如果你想附加文件并通过电子邮件作曲家发送,然后像下面的代码。

进口的UIKit 进口MessageUI

class ViewController: UIViewController, MFMailComposeViewControllerDelegate { 

@IBAction func launchEmail(sender: AnyObject) { 

var emailTitle = "EmailTitle" 
var messageBody = "Email BodyFeature request or bug report?" 
var toRecipents = ["[email protected]"] 
var mc: MFMailComposeViewController = MFMailComposeViewController() 
mc.mailComposeDelegate = self 
mc.setSubject(emailTitle) 
mc.setMessageBody(messageBody, isHTML: false) 
mc.setToRecipients(toRecipents) 

self.presentViewController(mc, animated: true, completion: nil) 
} 

func mailComposeController(controller:MFMailComposeViewController, didFinishWithResult result:MFMailComposeResult, error:NSError) { 
    switch result { 
    case MFMailComposeResultCancelled: 
     print("Mail cancelled") 
    case MFMailComposeResultSaved: 
     print("Mail saved") 
    case MFMailComposeResultSent: 
     print("Mail sent") 
    case MFMailComposeResultFailed: 
     print("Mail sent failure: \(error?.localizedDescription)") 
    default: 
     break 
    } 
    self.dismissViewControllerAnimated(true, completion: nil) 
} 

} 
+1

嗯,但WhatsApp是如何做到这一点? – ReasyEasyPeasy

+0

仅打开电子邮件编辑器。不会重定向回邮件应用程序。 http://stackoverflow.com/questions/8821934/launch-apple-mail-app-from-within-my-own-app – Hasya

+0

那不是真的。如果我使用WhatsApp从邮件应用程序打开文件,我可以将此文件发送给联系人。将此文件发送给WhatsApp关闭的联系人后,您会再次看到我之前打开的邮件(使用该文件)。不是电子邮件作曲家 – ReasyEasyPeasy

相关问题