从swift 3中的邮件扩展名打开应用程序

时间:2017-03-11 15:35:03

标签: ios swift swift3 imessage imessage-extension

我试图通过点击按钮从邮件扩展程序中打开我的母版应用程序。我在我的exstension中使用过这段代码:

    @IBAction func open(_ sender: UIButton) {

    let url = URL(string: "swiftexamples://")

    self.extensionContext?.open(url!, completionHandler: {(succes) in })


}

当我的母亲应用程序正在运行和后台运行时,一切正常,但是当我想打开已关闭的应用程序时,它会崩溃。没有崩溃日志,我对此iOS app crashes when first opened by URL Scheme有类似的情况。 我非常确定我必须向app delegate添加内容。 我找到了功能应用程序(_:open:options :)。问题是,我现在不知道如何实施"选项"部分。我写过:

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.
     let url = launchOptions?[UIApplicationLaunchOptionsKey.url] as? NSURL 
       let sourceApp = launchOptions?[UIApplicationLaunchOptionsKey.sourceApplication] as? String
       let annotation = launchOptions?[UIApplicationLaunchOptionsKey.annotation] as? AnyObject

        self.application(application, open: url, options:[sourceApp: String, annotation:AnyObject] )



    return true
}

我也听说过Universal Links,他们也有类似url方案的能力。是否有可能使用通用链接实现我想要的东西?

1 个答案:

答案 0 :(得分:0)

您可以尝试此解决方案。它对我有用:

func openUrl(url: URL?) {
    let selector = sel_registerName("openURL:")
    var responder = self as UIResponder?
    while let r = responder, !r.responds(to: selector) {
        responder = r.next
    }
    _ = responder?.perform(selector, with: url)
}

func canOpenUrl(url: URL?) -> Bool {
    let selector = sel_registerName("canOpenURL:")
    var responder = self as UIResponder?
    while let r = responder, !r.responds(to: selector) {
        responder = r.next
    }
    return (responder!.perform(selector, with: url) != nil)
}

根据https://stackoverflow.com/a/44694703/2064473的建议