启动 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
代表。
这是 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/