简体   繁体   English

从iOS Safari共享扩展打开主应用

[英]Open the main app from a iOS Safari Share Extension

I have a Safari share extension where I want the ability to open the main app from within the extension. 我有一个Safari共享扩展程序,希望从该扩展程序中打开主应用程序。 The user is presented with an alert where they have the option to open the app. 系统会向用户显示警报,让他们可以选择打开应用。

func openAppHandler() {
    self.extensionContext?.completeRequest(returningItems: []) { (success) in
        if let url = URL(string: "myapp://...") {
            self.extensionContext?.open(url, completionHandler: nil)
        }
    }
}

The alert appears after the method didSelectPost() is called, and as you can see it occurs in the background priority completion block for the extension. 警报将在调用didSelectPost()方法之后出现,并且您可以看到该警报发生在扩展的后台优先级完成块中。 The open method says in it's docs "In iOS 8, only the Today extension point (used for creating widgets) supports this method." open方法在其文档中说:“在iOS 8中,只有Today扩展点(用于创建窗口小部件)支持此方法。” I'm guessing it's still the case that it's still not supported in the Safari Share Extension. 我猜测Safari Share Extension仍然不支持它。

Does anyone know of a way to open my main app from a share extension? 有谁知道通过共享扩展程序打开我的主应用程序的方法吗?

I found a solution here . 我在这里找到了解决方案。 I'm not sure if this is technically ok with Apple, but it works just as I need it to. 我不确定苹果在技术上是否可以,但是它可以按我的需要工作。

@objc func openURL(_ url: URL) {
    return
}

func openContainerApp() {
    var responder: UIResponder? = self as UIResponder
    let selector = #selector(MyViewController.openURL(_:))

    while responder != nil {
        if responder!.responds(to: selector) && responder != self {
            responder!.perform(selector, with: URL(string: "myapp://url")!)
            return
        }
        responder = responder?.next
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM