Ios 上传照片并与ShareExtension中的PFObject关联
我正试图上传一个图像来解析它,并将其与共享扩展中的PFObject关联,但是考虑到编写共享扩展的限制条件,我不确定如何正确地执行此操作 我的第一次尝试是使用PFFile和PFObject类Ios 上传照片并与ShareExtension中的PFObject关联,ios,parse-platform,nsurlsession,ios-app-extension,ios8-share-extension,Ios,Parse Platform,Nsurlsession,Ios App Extension,Ios8 Share Extension,我正试图上传一个图像来解析它,并将其与共享扩展中的PFObject关联,但是考虑到编写共享扩展的限制条件,我不确定如何正确地执行此操作 我的第一次尝试是使用PFFile和PFObject类 // Create the UserPhoto PFObject and associate with PFFile PFObject *userPhoto = [PFObject objectWithClassName:@"UserPhoto"]; userPhoto[@"message"] = @"my
// 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];
这种方法的问题是,如果在应用程序扩展被销毁时saveInBackground尚未完成,则上载将被终止,因此您必须使用NSURLSession来保持连接处于活动状态,根据
NSURLSession不能与解析类一起使用,因此我必须使用Parse REST API来启动照片上传,然后在上传照片后将url与对象关联
上传照片->将照片URL与对象关联
这将设置上载任务以上载要解析的图像
// [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];
照片上传完成后,我们必须将url与对象关联。就像上面一样,我正在创建一个名为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];
}
}
链接REST API调用有时有效,有时不会导致上载图像,但图像url与UserPhoto不关联。因此,我想知道一旦扩展被破坏,操作系统是否禁止触发另一个NSURLSession
所以我的问题是:
1) parse是否在NSURLSession上提供了一个抽象,从而消除了使用parse REST API的需要
否则
2) 如何安全地链接两个NSURLSession,以便上载图像并成功地将图像URL与解析对象关联