PhoneGap 1.5 ChildBrowser can't show local file

泪湿孤枕 提交于 2019-12-06 11:52:59

问题


I'm building a PhoneGap 1.5 (Cordova) application for iOS and want to use the ChildBrowser plugin to show PDF files. I've been able to get it set up and it works very well when I'm viewing external PDF files (http, https).

The app has limited offline capabilities. I'm using the file system and file transfer API to download PDF files and save them to the local file system. When I try to view these documents using the ChildBrowser plugin, they never load.

I've done some troubleshooting by sticking some breakpoints in the plugin's command and viewcontroller. I find when I try to view a local document, I hit the webViewDidFailLoadWithError method with the following message:

The operation couldn't be completed. 
(WebKitErrorDomain error 101.)

The console shows the file:// URL of the document I tried to load, and if I browse that URL in safari on the simulator, I am able to view the document. The URL is similar to this when run in the simulator:

file://localhost/Users/{username}/Library/Application Support/iPhone Simulator/5.1/Applications/5D8EDAB7-4BB7-409E-989D-250A84B37877/Documents/{filename}

Is what I'm doing possible, and if so, how do I need to configure my application to be able to show PDF files from the local file system in the ChildBrowser?


回答1:


I was able to solve this myself. The PhoneGap fileEntry.toURI() method returns a URL something like what I included in my original post:

file://localhost/Users/{username}/Library/Application Support/iPhone Simulator/5.1/Applications/5D8EDAB7-4BB7-409E-989D-250A84B37877/Documents/{filename}

After jumping through some hoops to ensure the file URL was escaped and relative to the app's documents directory, the resulting URL which loads successfully looks something like this:

file:///Users/{username}/Library/Application%20Support/iPhone%20Simulator/5.1/Applications/B50682DD-AE6C-4015-AE61-3879576A4CB7/Documents/{relativeUri}

Just a little different. To fix this, maybe not for every case but at least for mine, I modified the loadURL method in the ChildBrowserViewController to look for a file URL and strip out the absolute stuff, leaving a URL relative to the app's documents directory. Then I used NSFileManager to help build up a URL that would work. I'm relatively new to iOS development, so maybe there's a better way to do this. Welcome the input. Here's the code:

- (void)loadURL:(NSString*)url {
...
    else if([url hasPrefix:@"file://"]) {
        NSError *error = NULL;

        //Create the regular expression to match against
        NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"file://.*/Documents/" options:NSRegularExpressionCaseInsensitive error:&error];

        // Create the new string by replacing the matching of the regex pattern with the template pattern(empty string)
        NSString *relativeUri = [regex stringByReplacingMatchesInString:url options:0 range:NSMakeRange(0, [url length]) withTemplate:@""];
        NSLog(@"New string: %@", relativeUri);

        NSURL *documentsDirectory = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
        NSURL *url = [documentsDirectory URLByAppendingPathComponent:relativeUri];
        NSLog(@"New string: %@", url);
        NSURLRequest *request = [NSURLRequest requestWithURL:url];
        [webView loadRequest:request];
    }
...
}


来源:https://stackoverflow.com/questions/9998509/phonegap-1-5-childbrowser-cant-show-local-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!