iOS jumps from Extension to App

background

You need to Extensionjump to the main when you click App, the data transfer is not considered for the time being, only if the main is pulled up App.

At the end of the day we still need to use UIApplicationthe openURL:method to open the main App, but this APIlimitation Extensionis used in

wecom-temp-02eb4ff4bffc7de70373eed58989a8d4.png

solve

Does the Extension need an interface

What I did initially was Action Extensionthat there were two types when I created it, with an interface and without an interface. Which one to choose to create? Further analysis is required.

image.png

Then we have only one idea, which is Runtimeto call openURL:.

Then you need to find UIApplicationthe object first, and Extensionit is also forbidden to use it directly in it.

image.png

UIApplicationSince it cannot be used directly, we can find the object through the responder chain and get the object perform openURL. Then in order to find the object in the responder chain, you UIApplicationmust choose a type with an interface to create it.

how to findUIApplication

func openApp(url: URL) {
    var responder = self as UIResponder?
    while (responder != nil && !(responder is UIApplication)) {
        responder = responder?.next
    }
    if responder != nil {
        let selectorOpenURL = sel_registerName("openURL:")
        if responder!.responds(to: selectorOpenURL) {
            responder?.perform(selectorOpenURL, with: url)
        }
    }
}
复制代码

At this point, the problem of opening the App has been solved, and you only need to use App Groupit to transmit data.

Guess you like

Origin juejin.im/post/7078927981766246407