swift - XCode11 错误 "open(_:options:completionHandler:) is unavailable in application extensions"

标签 swift xcode11 imessage-extension

启动 Touchgram v1.0 后,这是 99% iMessage 应用程序扩展,我尝试更新到 XCode11。
我开始收到错误 open(_:options:completionHandler:) is unavailable in application extensions我确认即使在尝试从 iMessage 应用程序启动 Web URL 的 trivial sample 中也会发生这种情况:
对于 example :

    let openSel = #selector(UIApplication.open(_:options:completionHandler:))
    while (responder != nil){
        if responder?.responds(to: openSel ) == true {
            // cannot package up multiple args to openSel 
            // so we explicitly call it on the iMessage application instance

            // found by iterating up the chain
            (responder as? UIApplication)?.open(url, completionHandler:handler) 
            return
        }
        responder = responder!.next
    }
2020 年更新
下面我自己对这个问题的回答详细说明了解决方法的工作原理。请注意,上面链接的示例已修复为既使用此解决方法,又显示在 iMessage 扩展本身内部的 WKWebView 中打开 Web URL。

最佳答案

正如该样本的(唯一)issue 中所述,这是 DTS 确认的 iOS 13 中的有意更改。我认为这是打击键盘扩展中滥用行为的一部分,该行为将 iMessage 扩展作为副作用。

我已经想出了一个解决方法,这与他们推荐的相同。

  • 使用 self.extensionContext?.open
  • 将 URL 转发到您的父应用程序
  • 让父应用程序启动外部应用程序或 URL
    代表。

  • 这是 Touchgram 的完整工作扩展
    // UIViewController+iMessageContext.swift
    // applied to class MessagesViewController: MSMessagesAppViewController, UrlOpeningInIMessage 
    
    protocol UrlOpeningInIMessage {
        func openFromiMessageContext(url:URL)
    }
    
    
    extension UrlOpeningInIMessage where Self:UIViewController {
        func openFromiMessageContext(url:URL) {
            let handler = { (success:Bool) -> () in
                if success {
                    os_log("Finished opening URL", log:tgEnv.logImUI, type:.debug)
                } else {
                    os_log("Failed to open URL", log:tgEnv.logImUI, type:.debug)
                }
            }
            // logic same as onLaunchMainPressed, since XCode11 unable to compile extension using UIApplication.open
            // so we pass the URL through to the parent app to launch on our behalf
            let appName = Bundle.appName()
            let encodedUrl = url.dataRepresentation.base64EncodedString()
            guard let appUrl: URL = URL(string: "\(appName)://?url=\(encodedUrl)") else { return }
            // can only open our app, not generalised URLs
            self.extensionContext?.open(appUrl, completionHandler: handler)
        }
    }
    

    关于swift - XCode11 错误 "open(_:options:completionHandler:) is unavailable in application extensions",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58153341/

    相关文章:

    ios - 在新模拟器上运行时关闭 iOS 13 键盘教程

    iOS13不支持后台更新通知吗?

    ios - 在 iMessage 扩展中从紧凑模式更改为展开模式、展开模式更改为折叠模式时,顶部和底部约束发生变化

    ios - iMessage 应用程序视频消息音量不起作用

    swift - CloudKit 查询操作仅返回 300 个结果

    ios - 如何在全局swift中创建uialertcontroller

    ios - UILabel 调整其 SuperView 的大小?

    ios - Xcode 11 向后兼容性 : "UIWindowScene is only available in iOS 13 or newer"

    ios - 通过 xcode 中的 iMessage 应用程序发送音频文件 (swift)

    swift - 将数据从自定义 UIView 传递到主 VC