我的应用程序可以打开PDF文件。所以我注册了PDF文件。当我从mail-app打开PDF文件时,我的应用程序被调用,并调用以下函数:
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
}
这个有效!如果用户点击确定按钮我想回到邮件应用程序。我怎样才能做到这一点?我读过,苹果不允许你在应用之间切换。但是whatsApp正在这样做。
我尝试删除viewController:
viewController.view.removeFromSuperview()
然后我只得到一个黑屏。
答案 0 :(得分:1)
答案 1 :(得分:-1)
这不会帮助您返回邮件应用viewController.view.removeFromSuperview()
它只会从您的viewcontroler中移除视图,并显示黑色window
。
即使您使用openURL,也无法返回邮件应用程序。
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"mailto:"]];
这将打开电子邮件作曲家,但实际上你将无法返回邮件应用程序。
<强>夫特强>
let url = URL(string: "mailto:\abc@xyz.com")
UIApplication.sharedApplication().openURL(url)
这将打开发件人的电子邮件作曲家为abc@xyz.com。
如果您想附加文件并通过电子邮件撰写器发送,请按照以下代码进行制作。
导入UIKit 导入MessageUI
class ViewController: UIViewController, MFMailComposeViewControllerDelegate {
@IBAction func launchEmail(sender: AnyObject) {
var emailTitle = "EmailTitle"
var messageBody = "Email BodyFeature request or bug report?"
var toRecipents = ["abc@xyz.com"]
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)
}
}
答案 2 :(得分:-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你的链接帮助了我!