2016-04-01 58 views
0

我需要從BOX sdk下載圖像到我的應用程序。我已經在使用Dropbox sdk工作 - 似乎比Box sdk更容易。無論如何 - 我有一個委託方法返回文件的名稱,但我怎麼實際下載文件?從BOX SDK下載文件iOS

- (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{ 
     } 
    }]; 

} 
+0

您對發佈的代碼有什麼問題? – rmaddy

+0

沒有下載。我試過切換到BoxItem來讓jsondict看到url,但他們是一個reblank。我想我需要啓用共享。我試過了,現在顯示的網址仍然沒有下載。 – malaki1974

回答

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