[英]Accessing `shared` variable of `UIApplication` from inside `share extension` in Swift
[英]iOS 14.5 or 14.6 onwards accessing UIApplication.shared.open or UIApplication.shared.openURL in Share Extension gives "APPLICATION_EXTENSION_API_ONLY"
在 iOS 14.5 或 14.6 之前,我们可以通过将Require Only App-Extension-Safe API
为NO
并使用:
if #available(iOS 10.0, *) {
UIApplication.shared.open(urlToOpen, options: [:], completionHandler: nil)
} else {
UIApplication.shared.openURL(urlToOpen)
}
但是 iOS 14.5 或 14.6 以后,我们在构建时收到此错误:
Application extensions and any libraries they link to must be built with the `APPLICATION_EXTENSION_API_ONLY` build setting set to YES.
有没有办法从 iOS 14.5/14.6 中的共享表扩展启动主应用程序?
我想出了一个解决方案,它可以让您启动主应用程序,同时保持Require Only App-Extension-Safe API
为YES
。
按照 Apple 的要求,将Require Only App-Extension-Safe API
设置为YES
。
然后在扩展中使用它:
let sharedSelector = NSSelectorFromString("sharedApplication")
let openSelector = NSSelectorFromString("openURL:")
if let urlToOpen = URL(string: "YOURAPPURLSCHEME://whatever_you_need_to_pass"), UIApplication.responds(to: sharedSelector), let shared = UIApplication.perform(sharedSelector)?.takeRetainedValue() as? UIApplication, shared.responds(to: openSelector) {
shared.perform(openSelector, with: urlToOpen)
}
//do the rest of extension completion stuff....
self.extensionContext?.completeRequest(returningItems: [], completionHandler:nil)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.