【问题标题】:iOS action extension cannot open containing appiOS 操作扩展无法打开包含应用程序
【发布时间】:2022-01-22 15:20:33
【问题描述】:

我需要将数据从 A 应用 传递到 B 应用A应用提供了UTI,我正在开发B应用

我只能使用如下所示的方式。

我知道这个方法应该使用动作扩展来实现,但是当我点击动作扩展时我无法打开B app(contianing app)。

我找了很多方法,但没有一个有效,这些方法如下:

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

有没有更好的建议来实现如上图所示的数据传输?

【问题讨论】:

    标签: ios


    【解决方案1】:

    我的灵感来自here,虽然直接使用并没有得到我想要的。

    注意两个关键点:

    1. 必须使用动作类型:Presents User Interface
    2. 基于ActionViewController,递归查找UIApplication

    也许你也看过,直接点击Action Extension跳转到Containing App。如果直接使用Presents User Interface这个类型,不是有页面吗?但实际上我没有看到这个页面。因为执行openURL后,直接self.extensionContext?.completeRequest,下面这个页面就关闭了,看起来好像从来没有出现过。代码如下:

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

    【讨论】:

      猜你喜欢
      • 2022-01-17
      • 2019-10-19
      • 2015-02-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多