8

We need to create a shared link for a file and then retrieve that link so that we can display it inside our application. We are able to create a shared link for a specific file (we can see it inside Box Account on the Web) but we are not able to retrive sharedLink via the API. It is always nil, although isShared method returns YES.

From the header file of BoxObject.h we find that these two methods provide required information about shared state of the item.

@protocol BoxObject
// ...


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

//...
@end

This is how we create shared link.

  1. Find BoxFile that we would like to share, lets call that object photo Prior calling method shareWithPassword:message:emails:callbacks:, [photo isShared] returns NO.
  2. we call [photo shareWithPassword:@"" message:@"" emails:[NSArray arrayWithObject:@""] callbacks:^(id<BoxOperationCallbacks> on1){...}];
  3. inside on1.after we check if response == BoxCallbackResponseSuccessful and then we call [photo updateWithCallbacks:^(id on2){..}]
  4. inside on2.after we check if response == BoxCallbackResponseSuccessful
  5. on successful response [photo isShared] returns YES but [photo sharedLink] returns nil

And if we check on the Web, we can see that file is actually shared but we cannot retrive sharedLink from the Box SDK.

Anyone has the same problem?

dtrsan
  • 158
  • 5
  • It'd be helpful if you could post the HTTP traffic during this process. This information will help to isolate the problem to either the data that Box is sending you, or the way that the iOS SDK is interpreting it. If you're working on a Mac, you might use a tool like HTTPScoop to capture the traffic. – John Hoerr Mar 12 '13 at 14:05
  • Already tried that with Wireshark. Requests go through HTTPS and I am not aware if there is a way to force them via HTTP. – dtrsan Mar 12 '13 at 16:49
  • HTTPScoop [provides some information](http://www.tuffcode.com/support.html#support7) on how you can get around that. – John Hoerr Mar 12 '13 at 16:57

3 Answers3

1

This is working for me, based off of the code already posted and the information found on github here

- (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];
}
ohhh
  • 972
  • 9
  • 24
0

I was able to get the share link by refreshing the folder itself. This is the code I came up with:

[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];
        }
    });
}];

This is with the old v1 API. Not sure if it has changed with the newer v2.

Tim Ritchey
  • 171
  • 1
  • 9
0

You can create a shared link by edit its info with 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];
}
Hoàng Toản
  • 861
  • 9
  • 11