简体   繁体   中英

iOS action extension cannot open containing app

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. 在此处输入图像描述

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?

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)
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM