简体   繁体   English

iOS 操作扩展无法打开包含应用程序

[英]iOS action extension cannot open containing app

I need to pass data from A app to B app .我需要将数据从A app传递到B app A app has provided UTI , and I am developing B app . A 应用提供了UTI ,我正在开发B 应用

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.我知道应该使用操作扩展来实现此方法,但是当我单击操作扩展时,我无法打开B 应用程序(contianing 应用程序)。

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必须使用动作类型: Presents User Interface
  2. Based on ActionViewController , recursively find UIApplication基于ActionViewController ,递归查找UIApplication

Maybe you have also seen it, just click on the Action Extension to jump to the Containing App .或许你也见过,直接点击Action Extension就可以跳转到Containing App了。 If you use the type Presents User Interface directly, isn't there a page?如果直接使用Presents User Interface类型,是不是有页面? 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.因为在执行openURL后,直接self.extensionContext?.completeRequest ,这个页面在下面关闭了,看起来好像从来没有出现过一样。 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)
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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