NSURLSessionUploadTask is not calling delegate met

2019-09-11 11:48发布

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

0条回答
登录 后发表回答