Upload a photo and link to PFObject in ShareExtension

I'm trying to upload an image to Parse and associate it with PFObject in my sharing extension, but I'm not sure how to do it right, given the limitations of writing a sharing extension.

My first attempt is this: using the PFFile and PFObject classes

// Create the UserPhoto PFObject and associate with PFFile PFObject *userPhoto = [PFObject objectWithClassName:@"UserPhoto"]; userPhoto[@"message"] = @"my photo"; NSData *imageData = UIImageJPEGRepresentation(image, 0.2); PFFile *imageFile = [PFFile fileWithName:@"image.jpg" data:imageData]; userPhoto[@"imageFile"] = imageFile; [userPhoto saveInBackground]; 

The problem with this approach is that if saveInBackground did not complete when the application extension was destroyed, the download will stop, so you should use NSURLSession to maintain the connection according to the application extension documentation .

NSURLSession cannot be used in conjunction with Parse classes, so I have to use the Parse REST API to initiate the loading of photos, and then after loading the photo, bind the URL to the object.

Upload Photo → Link Photo URL to Object

This sets up a download task to upload the image to Parse.

 // [self backroundSession] just configures a session and returns it NSURLSession *session = [self backgroundSession]; NSURL *url = [NSURL URLWithString:@"https://api.parse.com/1/files/pic.jpg"]; NSMutableURLRequest *req = [[NSMutableURLRequest alloc] initWithURL:url]; [req addValue:@"12345" forHTTPHeaderField:@"X-Parse-Application-Id"]; [req addValue:@"12345" forHTTPHeaderField:@"X-Parse-REST-API-Key"]; [req addValue:@"image/jpeg" forHTTPHeaderField:@"Content-Type"]; [req setHTTPMethod:@"POST"]; // path is the location of the image to be uploaded NSURLSessionUploadTask *myTask = [session uploadTaskWithRequest:req fromFile:[NSURL fileURLWithPath:path]]; [myTask resume]; 

After the photo upload is complete, we must associate the URL with the object. As above, I create an object called UserPhoto

 - (void)URLSession:(NSURLSession *)mySession task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error { // note: this bg session doesn't set delegate so we only create the UserPhoto object once NSURLSession *session = [self backgroundSessionForUserPhoto]; NSURL *url = [NSURL URLWithString:@"https://api.parse.com/1/classes/UserPhoto"]; NSMutableURLRequest *req = [[NSMutableURLRequest alloc] initWithURL:url]; [req addValue:@"12345" forHTTPHeaderField:@"X-Parse-Application-Id"]; [req addValue:@"12345" forHTTPHeaderField:@"X-Parse-REST-API-Key"]; [req addValue:[PFUser currentUser].sessionToken forHTTPHeaderField: @"X-Parse-Session-Token"]; [req addValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; [req setHTTPMethod:@"POST"]; NSDictionary *jsonDict = @{@"imageFile" : @{@"name" : response[@"url"], @"__type" : @"File"}, @"message" :@"my photo"}; NSError *error; NSData *jsonData = [NSJSONSerialization dataWithJSONObject:jsonDict options:NSJSONWritingPrettyPrinted error:&error]; if (!error) { [req setHTTPBody:jsonData]; NSURLSessionTask *myTask = [session dataTaskWithRequest:req]; [myTask resume]; } } 

Linking REST API calls sometimes works, and sometimes doesn't load the image, but the image URL is not connected to UserPhoto, so I wonder if the OS prevents another NSURLSession from running after the extension has been destroyed.

So here is my question:

1) Does parsing share the abstraction over NSURLSession, which eliminates the need to use the Parse REST API?

Otherwise

2) How can I safely link both NSURLSessions so that both images load and the image URL is successfully associated with the Parse object?

+5
source share

Source: https://habr.com/ru/post/1204075/


All Articles