13

I try to upload an image using NSURLSession in shared extension in iOS 8 but have this error instantly after calling

[task resume]
Error Domain=NSURLErrorDomain Code=-995 "The operation couldn’t be completed. (NSURLErrorDomain error -995.)"

here is my code:

NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:DEFAULT_SHARE_SESSION_ID];
NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:self.queue];
NSURLRequest *request = [self getMultipartUploadRequest:data url:url albumId:albumId];
// ... saving file here to Documents folder
NSURLSessionUploadTask *uploadTask = [session uploadTaskWithRequest:request fromFile:url1];
[uploadTask resume];

and also I have this message in console:

Attempted to create a task in a session that has been invalidated

This code absolutely works in the app, but doesn't work in the sharing extension. I couldn't find what does code -995 mean.

Any ideas?

Nilesh
  • 701
  • 5
  • 14
sasha_nec
  • 532
  • 5
  • 12
  • 2
    Look at this question: http://stackoverflow.com/questions/25438709/afnetworking-background-session-configuration-for-ios-8-extension – Wisors Oct 09 '14 at 09:54

1 Answers1

16

@Wisors great! It helped, just need to set

sessionConfiguration.sharedContainerIdentifier = @“com.me.myapp.containerIdentifier”;

In my case @“com.me.myapp.containerIdentifier” was @"group.mycompany.appname" which was crucial, another identifier didn't work. Thank you!

sasha_nec
  • 532
  • 5
  • 12
  • i tried both "com.company.app.sharedContainer" and "group.com.company.app" which is the registered app group identifier on the developer portal but both failed with the same error! – static0886 Feb 15 '16 at 14:51
  • Thanks, it helped. I was kinda stuck – Raheel Sadiq Mar 16 '16 at 06:59
  • I've a question for what we have to use this? "sessionConfiguration.sharedContainerIdentifier = @“com.me.myapp.containerIdentifier”;" I have already created NSURLSessionConfiguration.backgroundSessionConfigurationWithIdentifier(identifier). Now when i am requesting through it (manager class) it is executing but when i try to invalidate that it throws an error. What should i do ? – MQ. Aug 24 '16 at 08:36