I got inspiration from here, although using it directly did not get what I wanted.
Pay attention to two key points:
- Must use Action type:
Presents User Interface
- Based on
ActionViewController
, recursively find UIApplication
Maybe you have also seen it, just click on the Action Extension
to jump to the Containing App
. If you use the type Presents User Interface
directly, isn't there a page? But in fact I haven't seen this page. Because after executing openURL
, directly self.extensionContext?.completeRequest
, this page is closed below, it looks like it has never appeared. Here is the code:
func openApp(url: URL) {
var responder = self as UIResponder?
responder = (responder as? UIViewController)?.parent
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)
}
}
}
override func viewDidLoad() {
super.viewDidLoad()
let scheme = "xxapp://"
let url: URL = URL(string: scheme)!
openApp(url: url)
self.extensionContext?.completeRequest(returningItems: [], completionHandler: nil)
}