0

I need to pass data from A app to B app. A app has provided UTI, and I am developing B app.

I can only use the way as shown below. enter image description here

I understand that this method should be implemented using the action extension, but I cannot open the B app (contianing app) when I click the action extension.

I have looked for many ways, but none of them work,these are as follows:

responder?.perform(Selector("openURL:"), with: url)
[self.extensionContext openURL:YOUR_NSURL completionHandler:nil];

Is there any better suggestion to realize the transfer of data as shown in the figure above?

Nullable
  • 761
  • 5
  • 17

1 Answers1

0

I got inspiration from here, although using it directly did not get what I wanted.

Pay attention to two key points:

  1. Must use Action type: Presents User Interface
  2. 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)
}

Nullable
  • 761
  • 5
  • 17