5

我正在尝试将图像上传到 Parse 并将其与共享扩展中的 PFObject 相关联,但是鉴于编写共享扩展的限制,我不确定如何正确执行此操作。

我的第一次尝试如下使用 PFFile 和 PFObject 类

// 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 类一起使用,因此我必须使用 Parse REST API 来启动照片上传,然后在上传照片后将 url 与对象相关联。

上传照片 -> 将照片 URL 与对象关联

这将设置上传任务以将图像上传到 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];

照片上传完成后,我们必须将 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)如何安全地链接两个 NSURLSessions,以便上传图像并且图像 URL 成功与 Parse 对象关联?

4

0 回答 0