在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中的share sheet扩展启动主应用程序?
发布于 2021-06-07 08:43:37
我想出了一个解决方案,可以让你在启动主应用的同时保持Require Only App-Extension-Safe API
为YES
。
按照苹果的要求,将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)
https://stackoverflow.com/questions/67864793
复制相似问题