启动后触摸图 v1.0 https://apps.apple.com/us/app/touchgram-for-imessage/id1447336478?ls=1,这是 99% iMessage 应用程序扩展,我尝试更新到 XCode 11。
我开始收到错误open(_:options:completionHandler:) is unavailable in application extensions
我确认即使在琐碎的样本 https://github.com/AndyDentFree/im-plausibilities/tree/master/webFromIM/webFromIM%20MessagesExtension尝试从 iMessage 应用程序启动 Web URL:
For example https://github.com/AndyDentFree/im-plausibilities/blob/896219d07fa630f8ad960bc05ee0e9fcac76bb22/webFromIM/webFromIM%20MessagesExtension/MessagesViewController.swift#L117:
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年更新
下面是我自己对这个问题的回答,详细介绍了解决方法的工作原理。请注意,上面链接的示例已修复为既使用此解决方法,又显示在WKWebView
在 iMessage 扩展本身内部。
如(唯一)中所述issue https://github.com/AndyDentFree/im-plausibilities/issues/2在该样本上,这是一个iOS 13 中的有意更改由 DTS 确认。我相信这是打击键盘扩展中滥用行为的一部分,该行为的副作用是 iMessage 扩展。
我已经想出了一个解决方法,与他们建议的相同。
- 使用以下命令将 URL 转发到您的父应用程序
self.extensionContext?.open
- 让父应用程序启动您的外部应用程序或 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)
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)