2016-04-01 5 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. Я думаю, мне нужно включить обмен. Я попробовал это, и URL-адреса теперь показывают, но все равно ничего не загружают. – malaki1974

ответ

0

Я закончил выпуск команды BOXItem, чтобы получить идентификатор.

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