2

我正在尝试通过单击按钮从消息扩展中打开我的母亲应用程序。我在我的扩展中使用了这个代码:

    @IBAction func open(_ sender: UIButton) {

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

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


}

当我的母亲应用程序正在运行和后台运行时,一切正常,但是当我想打开关闭的应用程序时,它崩溃了。没有崩溃日志,我有类似的情况,这个iOS 应用程序在首次由 URL Scheme 打开时崩溃。我很确定我必须向应用程序委托添加一些内容。我找到了函数应用程序(_: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
}

我也听说过通用链接,它们具有与 url 方案类似的能力。是否可以使用通用链接来实现我想要的?

4

1 回答 1

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中提出的

于 2019-03-02T02:26:35.233 回答