我能够自己解决这个问题。PhoneGap fileEntry.toURI() 方法返回一个类似于我在原始帖子中包含的 URL:
file://localhost/Users/{username}/Library/Application Support/iPhone Simulator/5.1/Applications/5D8EDAB7-4BB7-409E-989D-250A84B37877/Documents/{filename}
在跳过一些环节以确保文件 URL 被转义并相对于应用程序的文档目录后,成功加载的结果 URL 如下所示:
file:///Users/{username}/Library/Application%20Support/iPhone%20Simulator/5.1/Applications/B50682DD-AE6C-4015-AE61-3879576A4CB7/Documents/{relativeUri}
只是有点不同。为了解决这个问题,可能不是针对所有情况,但至少对我而言,我修改了 ChildBrowserViewController 中的 loadURL 方法以查找文件 URL 并去除绝对内容,留下一个相对于应用程序文档目录的 URL。然后我使用 NSFileManager 来帮助建立一个可行的 URL。我对 iOS 开发比较陌生,所以也许有更好的方法来做到这一点。欢迎输入。这是代码:
- (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];
}
...
}