I use this code to upload an image in objective c.
-(void)uploadImageInBackground:(UIImage*)image toPreSignedURL:(NSURL*)preSignedURL withCompletion:(void (^)(id _Nullable response))completion{
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:preSignedURL];
request.HTTPMethod = @"PUT";
[request setValue:@"image/jpeg" forHTTPHeaderField:@"Content-Type"];
NSString* filePath = [self getFilePathForFileName:@"image.jpeg" image:image];
NSFileManager *fileManager = [NSFileManager defaultManager];
if (![fileManager fileExistsAtPath:filePath]){
return;
}
NSURLSessionUploadTask *uploadTask = [manager uploadTaskWithRequest:request fromData:[NSData dataWithContentsOfFile:filePath] progress:^(NSProgress * _Nonnull uploadProgress) {
NSLog(@"\nUpload Progress: %f", uploadProgress.fractionCompleted);
} completionHandler:^(NSURLResponse * _Nonnull response, id _Nullable responseObject, NSError * _Nullable error) {
NSFileManager *fileManager = [NSFileManager new];
[fileManager removeItemAtPath:filePath error:NULL];
if (error) {
completion(nil);
}
else{
NSLog(@"\nhello: Upload Progress COMPLETED: %@", response);
completion(response);
}
}];
[uploadTask resume];
}
How to continue the upload when the app is sent to the background by the user or when the user closes the app?
Any help appreciated.