8

我们需要为文件创建一个共享链接,然后检索该链接,以便我们可以在我们的应用程序中显示它。我们能够为特定文件创建共享链接(我们可以在 Web 上的 Box Account 中看到它),但我们无法通过 API 检索 sharedLink。它总是 nil,尽管isShared方法返回 YES。

从头文件中BoxObject.h我们发现这两个方法提供了项目共享状态所需的信息。

@protocol BoxObject
// ...


// Information about the shared state of the item
@property (readonly, getter = isShared) BOOL shared;
@property (readonly) NSString *sharedLink;

//...
@end

这就是我们创建共享链接的方式。

  1. 找到我们想要共享的 BoxFile,让我们调用该对象photo 之前调用方法 shareWithPassword:message:emails:callbacks:,[photo isShared]返回 NO。
  2. 我们称之为[photo shareWithPassword:@"" message:@"" emails:[NSArray arrayWithObject:@""] callbacks:^(id<BoxOperationCallbacks> on1){...}];
  3. 在 on1.after 我们检查是否响应 == BoxCallbackResponseSuccessful 然后我们调用 [photo updateWithCallbacks:^(id on2){..}]
  4. 在 on2.after 我们检查是否响应 == BoxCallbackResponseSuccessful
  5. 成功响应[photo isShared]返回 YES 但 [photo sharedLink] 返回 nil

如果我们在 Web 上查看,我们可以看到该文件实际上是共享的,但我们无法从 Box SDK 中检索 sharedLink。

有人有同样的问题吗?

4

3 回答 3

1

这对我有用,基于已经发布的代码和在 github 上找到的信息

- (void) getShareableLinkForFileId:(NSString *)fileId 
{
    BoxFileBlock fileSuccess = ^(BoxFile *file) {
            NSDictionary *fileInfo = file.rawResponseJSON;
            if (![fileInfo[@"shared_link"] isEqual:[NSNull null]]) { 
            NSDictionary *linkData = fileInfo[@"shared_link"];
            //Do something with the link
        } else {
            // failure
        }
    };
    BoxAPIJSONFailureBlock failure = ^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, NSDictionary *JSONDictionary) {
         //Handle the failure 
    };

    BoxFilesRequestBuilder *builder = [[BoxFilesRequestBuilder alloc] init];
    BoxSharedObjectBuilder *sharedBuilder = [[BoxSharedObjectBuilder alloc] init];
    sharedBuilder.access = BoxAPISharedObjectAccessOpen;
    builder.sharedLink = sharedBuilder;
    [[BoxSDK sharedSDK].filesManager editFileWithID:fileId requestBuilder:builder success:fileSuccess failure:failure];
}
于 2014-06-12T17:57:34.850 回答
0

您可以通过使用 Box V2 编辑其信息来创建共享链接:

 Box2FolderBlock folderSuccess = ^(Box2Folder *folder) {
    if (![[folder sharedLink] isEqual:[NSNull null]]) {
        NSString *sharedUrl = [[folder sharedLink] objectForKey:Box2APIObjectKeyURL];
    } else {
        // failure
    }
 };

 Box2FileBlock fileSuccess = ^(Box2File *file) {
    if (![[file sharedLink] isEqual:[NSNull null]]) {
       NSString *sharedUrl = [[file sharedLink] objectForKey:Box2APIObjectKeyURL];
    } else {
       // failure
    }
 };

 Box2APIJSONFailureBlock failure = ^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, NSDictionary *JSONDictionary) {
 };

BoxSharedObjectBuilder *sharedLinkObject = [[BoxSharedObjectBuilder alloc] init];
sharedLinkObject.access = BoxAPISharedObjectAccessOpen;

BoxAPIJSONOperation *operation;

if (isFile == NO) {

    sharedLinkObject.canPreview = BoxAPISharedObjectPermissionStateEnabled;

    BoxFoldersRequestBuilder *requestBuilder = [[BoxFoldersRequestBuilder alloc] init];
    requestBuilder.sharedLink = sharedLinkObject;

    operation = [boxSDK.foldersManager editFolderWithID:fileOrFolderId requestBuilder:requestBuilder success:folderSuccess failure:failure];

} else {

    sharedLinkObject.canDownload = BoxAPISharedObjectPermissionStateEnabled;

    BoxFilesRequestBuilder *requestBuilder = [[BoxFilesRequestBuilder alloc] init];
    requestBuilder.sharedLink = sharedLinkObject;

    operation = [boxSDK.filesManager editFileWithID:fileOrFolderId requestBuilder:requestBuilder success:fileSuccess failure:failure];
}
于 2013-10-11T21:07:18.703 回答
0

我能够通过刷新文件夹本身来获取共享链接。这是我想出的代码:

[boxFile shareWithPassword:@"" message:@"" emails:@[ @"" ] callbacks:^(id<BoxOperationCallbacks> on) {
    on.after(^(BoxCallbackResponse response) {
        if (response == BoxCallbackResponseSuccessful) {
            [self.rootFolder updateWithCallbacks:^(id<BoxOperationCallbacks> on) {
                on.after(^(BoxCallbackResponse response) {
                    BoxFile *updatedBoxFile = (BoxFile*)[self.rootFolder.children objectAtIndex:self.selectedIndexPath.row];
                    NSString *fileName = updatedBoxFile.name;
                    NSString *shareLink = updatedBoxFile.sharedLink;

                    NSLog(@"%@ [%@]: %@", fileName, updatedBoxFile.isShared ? @"YES" : @"NO", shareLink);
                });
            }];
        } else {
            [BoxErrorHandler presentErrorAlertViewForResponse:response];
        }
    });
}];

这是旧的 v1 API。不确定它是否随着较新的 ​​v2 发生了变化。

于 2013-06-19T14:51:20.520 回答