从BOX SDK iOS下载文件

时间:2016-04-01 15:29:29

标签: ios download sdk box

我需要将BOX sdk中的图像下载到我的应用程序中。我已经使用dropbox sdk了 - 似乎比Box sdk更容易。无论如何 - 我有一个委托方法返回文件的名称,但我如何实际下载文件?

    - (void)itemsViewController:(BOXItemsViewController *)itemsViewController didTapFile:(BOXFile *)file inItems:(NSArray *)items {

    NSLog(@"Did tap file: %@", file.name);

    BOXFileDownloadRequest *downloadRequest;
    BOXContentClient *contentClient;

    contentClient = [BOXContentClient defaultClient];
    NSOutputStream *outputStream = [[NSOutputStream alloc] initToMemory];
    downloadRequest = [_contentClient fileDownloadRequestWithID:file.name toOutputStream:outputStream];
    [_downloadRequest performRequestWithProgress:^(long long totalBytesTransferred, long long totalBytesExpectedToTransfer) {
    } completion:^(NSError *error) {
        if (error == nil) {
            NSData *data = [outputStream propertyForKey:NSStreamDataWrittenToMemoryStreamKey];
            UIImage *img = [UIImage imageWithData:data];
            _uiiv_logo.image = img;
        }
        else{
        }
    }];

}

1 个答案:

答案 0 :(得分:0)

我最终选择了BOXItem来获得' id'。

- (void)itemsViewController:(BOXItemsViewController *)itemsViewController didTapFile:(BOXFile *)file inItems:(NSArray *)items
{
    BOXItem *item = file;
    BOXContentClient *contentClient = [BOXContentClient defaultClient];

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsPath = [paths objectAtIndex:0]; //Get the docs directory
    NSString *localFilePath = [documentsPath stringByAppendingPathComponent:@"NSURLSession.png"];

    BOXFileDownloadRequest *boxRequest = [contentClient fileDownloadRequestWithID:[item.JSONData valueForKey:@"id"] toLocalFilePath:localFilePath];
    [boxRequest performRequestWithProgress:^(long long totalBytesTransferred, long long totalBytesExpectedToTransfer) {
        // Update a progress bar, etc.
        NSLog(@"progress %lld",totalBytesTransferred);
    } completion:^(NSError *error) {
        // Download has completed. If it failed, error will contain reason (e.g. network connection)
        if (error) {
            NSLog(@"error %@",[error description]);
            [[NSNotificationCenter defaultCenter] postNotificationName:@"customUpdateBG" object:nil];
        }
    }];
}