background

It is necessary to jump to the main App when clicking Extension. Data transfer is not considered for the time being, but only how to pull up the main App.

After all, we still need to use UIApplication’s openURL: method to open the main App, but this API is limited to use in Extension

To solve

Whether Extension requires an interface

I started with Action Extension, which was created with two types, interface and no interface. Which one to choose? Further analysis is needed.

So there is only one idea, which is to call openURL with Runtime:

The first thing you need to do is find the UIApplication object, which is also forbidden to use directly in Extension.

So we can’t use it directly, so we’ll just go through the responder chain and find the UIApplication object, and then we’ll get the object Perform openURL. So in order to find a UIApplication object in the responder chain, you definitely need to select a type that has 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)
        }
    }
}
Copy the code

In fact, the problem of opening the App has been solved here. You only need to use the App Group to transfer data.