I am trying to upload an image using backgroundSessionConfiguration and NSURLSessionUploadTask in order to maintain live broadcast in the background of the application.
But when I use backgroundSessionConfiguration , NSURLSessionUploadTask does not call the delegation method when using defaultSessionConfiguration , it calls the delegation method.
Here is the code.
//Create a file to upload UIImage *image = [UIImage imageNamed:@"1.jpg"]; NSData *imageData = UIImagePNGRepresentation(image); NSFileManager *fileManager = [NSFileManager defaultManager]; NSArray *URLs = [fileManager URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask]; NSString *documentsDirectory = [[URLs objectAtIndex:0] absoluteString]; NSString *filePath = [documentsDirectory stringByAppendingString:@"testfile.jpg"]; [imageData writeToFile:filePath atomically:YES]; NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"https://file.upload/destination"]]; [request setHTTPMethod:@"PUT"]; NSURLSessionConfiguration *config = [NSURLSessionConfiguration backgroundSessionConfiguration: [NSString stringWithFormat:@"testSession.foo.com"]]; config.HTTPMaximumConnectionsPerHost = 1; session = [NSURLSession sessionWithConfiguration:config delegate:self delegateQueue:nil]; NSURLSessionUploadTask *uploadTask = [self.session uploadTaskWithRequest:request fromFile:[NSURL URLWithString:filePath]]; [uploadTask resume]; NSLog(@"==%ld",uploadTask.state);
if I change backgroundSessionConfiguration to defaultSessionConfiguration , it will call the delegate method.
Please help me understand what I am missing.
Thanks Nitin
ios objective-c nsurlsession nsurlsessionuploadtask
Nit Sep 22 '15 at 12:49 2015-09-22 12:49
source share