Upload tasks from NSData are not supported in background sessions

前端 未结 1 1250
无人及你
无人及你 2020-12-18 09:43

i am trying to upload image using Share extension but the problem is when i add the background task it is giving me this error, It is saying nsdata is not s

相关标签:
1条回答
  • 2020-12-18 10:06

    It seems Up NSURLSessionUploadTask does not support Large size NSData, But you can give a file path to NSURLSessionUploadTask to upload on server, or write your image temporary on disk firs then give path.

    Here's an example it is uploading both the filepath and NSdata.

    Uploads using backgroundSessionConfiguration and NSURLSessionUploadTask cause app to crash

    Updated :

    //Create a file to upload
    UIImage *image = [UIImage imageNamed:@"onboarding-4@2x.png"];
    NSData *imageData = UIImagePNGRepresentation(image);
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSArray *URLs = [fileManager URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask];
    NSString *documentsDirectory = [[URLs objectAtIndex:0] absoluteString];
    

    // Here's the filePath URL

    NSString *filePath = [documentsDirectory stringByAppendingString:@"testfile.png"];
    [imageData writeToFile:filePath atomically:YES];
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"https://file.upload/destination"]];
    [request setHTTPMethod:@"PUT"];
    

    // Here it is using it filePath.

    NSURLSessionUploadTask *uploadTask = [upLoadSession uploadTaskWithRequest:request fromFile:[NSURL URLWithString:filePath] completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
        //code
    }];
    
    0 讨论(0)
提交回复
热议问题