3

When trying to create a NSURLSessionUploadTask using AFNetworkings AFURLSessionManager in a share extension I keep getting an error. The delegate for the session keeps getting called for

- (void)URLSession:(NSURLSession *)session didBecomeInvalidWithError:(NSError *)error

With the error

Error Domain=NSURLErrorDomain Code=-996 "Could not communicate with background transfer service" UserInfo=0x60800007a6c0 {NSLocalizedDescription=Could not communicate with background transfer service}

This happens if the app has a session and then the share extension tries to make a session. To init the session I do the following in both the app and the share extension.

NSURLSessionConfiguration* config = [NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:kSHARE_UPLOAD_SESSION];
config.sharedContainerIdentifier = kAPP_GROUP;
self.sessionManager = [[AFURLSessionManager alloc] initWithSessionConfiguration:config];
self.sessionManager.attemptsToRecreateUploadTasksForBackgroundSessions = YES;
datinc
  • 3,404
  • 3
  • 24
  • 33

1 Answers1

4

You must use a different "Identifier" (kSHARE_UPLOAD_SESSION) for the main app & the extension. You apparently cannot have 2 NSURLSession with the same identifier running at the same time. You should keep the same group though (kAPP_GROUP)

user2962499
  • 486
  • 6
  • 10