来自 macos 共享扩展的 NSURLSession

NSURLSession from macos Sharing Extension

我在 func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?) { URLSessionDataDelegate 方法中遇到此错误:

error=Optional(Error Domain=NSURLErrorDomain Code=-1 “unknown error” UserInfo={NSErrorFailingURLKey=https://www.example.com/, NSLocalizedDescription=unknown error, NSErrorFailingURLStringKey=https://www.example.com/})

非常相似的代码在应用程序中完美运行,但我正在尝试编写一个上传到网络服务器的共享应用程序扩展。

我使用的 URLSessionConfiguration.background(withIdentifier:"...") 与我的桌面应用程序相同。

为什么我会得到 erorr -1?

我在 entitlements 文件中缺少 com.apple.security.network.client 权利。

https://developer.apple.com/library/content/documentation/Miscellaneous/Reference/EntitlementKeyReference/Chapters/EnablingAppSandbox.html#//apple_ref/doc/uid/TP40011195-CH4-SW9