Question

What is approach should be used while loading files from remote server on QLPreviewController? When should we download files using my server API to load them on QLPreviewController. I am adding QLPreviewController as subview to my current view. I can use datasource method to make call to download file from server.

- (id<QLPreviewItem>)previewController:(QLPreviewController *)controller previewItemAtIndex:(NSInteger)index

But once file is downloaded i need to reload QLPreviewController, where this should be done For images i would like to image gallery view so that swipe to view images downloaded from server. Can anyone point me to any tutorial to load images from remote server URL

Was it helpful?

Solution

To show any file that supports QLPreviewController, the url should be the fileURL.

(id<QLPreviewItem>)previewController:(QLPreviewController *)controller previewItemAtIndex:(NSInteger)index

It always returns fileURL - if you will use any other URL, it will crash. After downloading is over save the file in documents directory and then push to preview.

- (void)saveFileInDocDirectoryWithFileName:(NSString *)title{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *docPath_ = [paths objectAtIndex:0];
    NSString *filePath = [ docPath_ stringByAppendingPathComponent:title];

    self.fileURL = [NSURL fileURLWithPath:filePath];
        [self pushToPreViewWithURL:fileURL];
}


- (void)pushToPreViewWithURL:(NSURL *)filePathURL{     
    QLPreviewController *previewController = [[QLPreviewController alloc] init];
    previewController.dataSource = self;
    previewController.delegate = self;
    // start previewing the document at the current section index

    [[NSOperationQueue mainQueue] addOperationWithBlock:^{

        [self.navigationController pushViewController:previewController animated:YES];

    }]; 
}

Then in delegate method, return fileURL:

 - (id)previewController:(QLPreviewController *)previewController previewItemAtIndex:(NSInteger)idx {
     return self.fileURL;
 }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top