iOS 14.5 或 14.6 及以上版本访问共享扩展中的 UIApplication.shared.open 或 UIApplication.shared.openURL 会给出 "APPLICATION_EXTENSION_API_ONLY"

标签 ios swift objective-c xcode

在 iOS 14.5 或 14.6 之前,我们可以通过将 仅需要 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 中的共享表扩展启动主应用程序?

最佳答案

我想出了一个解决方案,可以让您启动主应用程序,同时将仅需要应用程序扩展安全API设置为YES

按照 Apple 的要求,将仅需要应用程序扩展安全 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)

关于iOS 14.5 或 14.6 及以上版本访问共享扩展中的 UIApplication.shared.open 或 UIApplication.shared.openURL 会给出 "APPLICATION_EXTENSION_API_ONLY",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67864793/

相关文章:

ios - 从ios中的视频中获取所有帧

ios - 如何检测 iPhone 在 Objective-C 或 Swift 中掉落或向上移动?

swift - 协议(protocol)不符合自身?

ios - Xcode 6.3 : 8. 0 或 8.0.2 模拟器

iphone - 使用适用于 iOS 的 ExtAudioFileWrite 将音频样本缓冲区写入 aac 文件

ios - 无法通过 Cocoapods 安装 RealmSwift

ios - 使用 iOS 8 可操作通知启动应用程序

Swift “UDP Read” 代码 - unsaferawbufferpointer 编译错误

iphone - 透明UINavigationBar下的UIWebView

ios - 如何从服务器提取数据、使其易于访问并持久保存而不减慢应用程序的界面速度?