macosnsurlsessionmacos-app-extension

NSURLSession from macos Sharing Extension


I'm getting this error in the func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?) { URLSessionDataDelegate method:

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

Very similar code works perfectly from an application, but I'm trying to write an Sharing App Extension which uploads to a webserver.

I'm using URLSessionConfiguration.background(withIdentifier:"...") same as my desktop application.

Why do I get erorr -1?


Solution

  • I was missing the com.apple.security.network.client entitlement in the entitlements file.

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