【问题标题】:Go back to previous app(Swift ios)返回上一个应用程序(Swift ios)
【发布时间】:2016-11-24 13:22:29
【问题描述】:

我的应用程序可以打开 PDF 文件。所以我将它注册为 PDF 文件。当我从邮件应用程序打开 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()

然后我只得到一个黑屏。

【问题讨论】:

  • 这不会帮助您返回邮件应用程序 viewController.view.removeFromSuperview()
  • 这对我的帮助不如你的回答:)。你有解决这个问题的办法吗?

标签: ios swift swift3


【解决方案1】:

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

let email = "foo@bar.com"

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

编辑:这个answer 可能会帮助你

【讨论】:

  • 好吧,Whatsapp 是怎么做到的?
【解决方案2】:

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

它只会从您的视图控制器中删除视图,并显示黑色 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)
}

}

【讨论】:

  • 好吧,Whatsapp 是怎么做到的呢?
  • 仅打开电子邮件编辑器。不会重定向回邮件应用程序。 stackoverflow.com/questions/8821934/…
  • 那不是真的。如果我使用 whatsapp 从邮件应用程序打开文件,我可以将此文件发送给联系人。在我将此文件发送给联系人后,WhatsApp 已关闭,您再次看到我之前打开的邮件(带有文件)。不是电子邮件编辑器
  • 试试看。如果您找到方法,请在您的问题中更新为答案。
【解决方案3】:

我用下面的代码做到了:

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,你的链接帮助了我!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-02
    • 1970-01-01
    • 2023-03-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多