2016-04-01 7 views
0

Ich muss ein Bild von der BOX SDK in meine App herunterladen. Ich habe dies bereits funktioniert mit der Dropbox SDK - scheint einfacher als Box SDK. Wie auch immer - ich habe eine Delegate-Methode, die den Namen der Datei zurückgibt, aber wie lade ich die Datei tatsächlich herunter?Laden Sie eine Datei von 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

Welches Problem haben Sie mit dem gebuchten Code? – rmaddy

+0

Nichts Downloads. Ich habe versucht, zu BoxItem zu wechseln, um das Jsondict die URL zu sehen, aber sie eine Reblank. Ich denke, ich muss das Teilen ermöglichen. Ich habe das ausprobiert und die URLs zeigen jetzt aber immernoch nichts downloads. – malaki1974

Antwort

0

Ich endete Casting zu BOXItem, um die "ID" zu bekommen.

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