NSURLSessionUploadTask is not calling delegate method with NSURLSessionConfiguration backgroundSessionConfiguration

*爱你&永不变心* 提交于 2019-12-12 04:40:01

问题


I am trying upload image using backgroundSessionConfiguration and NSURLSessionUploadTask to keep live upload process in application background mode.

But when i use backgroundSessionConfiguration, NSURLSessionUploadTask is not calling delegate method while using defaultSessionConfiguration it call delegate 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, delegate method will call.

Please help me to understand what am i missing.

Thanks, Nitin

来源:https://stackoverflow.com/questions/32717350/nsurlsessionuploadtask-is-not-calling-delegate-method-with-nsurlsessionconfigura

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!