How to launch a parent iOS App from its App Extension

后端 未结 7 1380
借酒劲吻你
借酒劲吻你 2021-02-07 07:22

Does any one know how to launch the parent app from the app extension\'s view controller?

I just want to launch the main app from its app extension.

7条回答
  •  广开言路
    2021-02-07 07:48

    Here is working solution (tested on iOS 9.2) at least for Keyboard Extension. This category adds special method for access to hidden sharedApplication object and then call openURL: on it. (Of course then you have to use openURL: method with your app scheme.)

    extension UIInputViewController {
    
        func openURL(url: NSURL) -> Bool {
            do {
                let application = try self.sharedApplication()
                return application.performSelector("openURL:", withObject: url) != nil
            }
            catch {
                return false
            }
        }
    
        func sharedApplication() throws -> UIApplication {
            var responder: UIResponder? = self
            while responder != nil {
                if let application = responder as? UIApplication {
                    return application
                }
    
                responder = responder?.nextResponder()
            }
    
            throw NSError(domain: "UIInputViewController+sharedApplication.swift", code: 1, userInfo: nil)
        }
    
    }
    

提交回复
热议问题