How to launch a parent iOS App from its App Extension

后端 未结 7 1464
借酒劲吻你
借酒劲吻你 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:49

    it is working in my current working app using action extension

    1- in parent app plist add custom URL like

    2- add both functions in your extension view controller

    func openURL(url: NSURL) -> Bool {
        do {
            let application = try self.sharedApplication()
            return application.performSelector(inBackground: "openURL:", with: 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?.next
        }
    
        throw NSError(domain: "UIInputViewController+sharedApplication.swift", code: 1, userInfo: nil)
    }
    

    3- call this function in your button action or somewhere you want to do

    self.openURL(url: NSURL(string:"openPdf://HomeVC")!)
    

    here homevc is the class name of your View controller which should be presented

    4- in your app delegate implement the method like

      func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
        let urlPath : String = url.absoluteString
        print(urlPath)
        if urlPath.contains("HomeVC"){
            //here go to firstViewController view controller
            self.window = UIWindow(frame: UIScreen.main.bounds)
    
            let storyboard = UIStoryboard(name: "Main", bundle: nil)
    
            let initialViewController = storyboard.instantiateViewController(withIdentifier: "homeVC")
    
            self.window?.rootViewController = initialViewController
            self.window?.makeKeyAndVisible()
        }
    }
    

    I hope will work fine, can also share data between extension app and parent app

提交回复
热议问题