iCloud file copy not working...

I'm allowing my users to select multiple iCloud documents to compare side by side. I would like the user to select the file and it copy locally and then display it in the WKWebView. In the simulator it works perfectly. However, on the device it will not display the documents at all. Any ideas?


-(void)documentPicker:(UIDocumentPickerViewController *)controller didPickDocumentsAtURLs:(NSArray *)urls{
    // check
    if(controller.documentPickerMode == UIDocumentPickerModeOpen){
        // vars
        NSURL *url = [urls objectAtIndex:0];
        BOOL isAccess = [url startAccessingSecurityScopedResource];
        
        // check
        if(!isAccess){
            // return
            return;
        }
        
        // vars
        NSString *fileName = [NSString stringWithFormat:@"%@.%@", [appDelegate.myGlobals getGuid], url.pathExtension];
        NSString *path = [url path];
        NSData *data = [[NSFileManager defaultManager] contentsAtPath:path];
        NSString *documentsPath = [[self applicationDocumentsDirectory].path stringByAppendingPathComponent:fileName];
        NSURL *fileURL = [NSURL fileURLWithPath:documentsPath];
        
        // write
        [data writeToFile:documentsPath atomically:YES];
        
        // set
        NSURL *url = [NSURL URLWithString:fileURL];
        NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
        [myWebView loadRequest:urlRequest];
        
        // stop
        [url stopAccessingSecurityScopedResource];
    }
}

Replies

Do you know if the contentsAtPath:path call is succeeding?

You aren't checking in the code above.


I would run it from xcode and breakpoint there to see what you are getting.

Oddly, the file I am saving is located here:

file:///var/mobile/Containers/Data/Application/E28084ED-E935-4D0A-A07C-8A5C7EF588F0/Documents/2C292D01-4AFD-4846-8E38-73B25709B232.pdf


But when checking the directory after a save the files are this:

file:///private/var/mobile/Containers/Data/Application/E28084ED-E935-4D0A-A07C-8A5C7EF588F0/Documents/967FBE1E-1824-49F7-8AAD-4ABD9432BCCC.pdf


Notice the "private" this only occurs on a device and therefore the path is wrong and I believe that is why the file will not load in the WKWebView. Thoughts?


And even more oddly, it works with the exact same code perfectly when loading the content in a UIWebView vs. WKWebView. So there is clearly something wrong with the new WKWebView.