Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
554 views
in Technique[技术] by (71.8m points)

ios - WKWebView loadFileURL:allowingReadAccessToURL: does not allow access to bundle for file in documents or temp

This is sort of a result of this, which is about trying to fix goBack behaviour by writing a string first to a file and then navigating to it.

So, I have a few html files and images in my bundle. Also some html pages generated programmatically. They work fine, using images from my bundle and following links to html files on my bundle, with the right baseURL:. But there is a glitch with goBack which makes navigation annoying at times. As a solution, I tried writing the generated html strings in a temp file first:

NSString *file = [NSTemporaryDirectory() stringByAppendingPathComponent:@"temp.html"];
[html writeToFile:file atomically:YES encoding:NSUTF8StringEncoding error:nil];

I also tried as a file in my documents directory, same effect, so temporary dir is of no importance. According to the documentation, loadFileURL:allowingReadAccessToURL: should give me read access to the directory I specify, so I try:

NSURL *dir = [NSURL fileURLWithPath:NSBundle.mainBundle.resourcePath];
NSURL *URL = [NSURL fileURLWithPath:file];
[webView loadFileURL:URL allowingReadAccessToURL:dir];

But while the Simulator is fine, on the devices I get: WebPageProxy::Ignoring request to load this main resource because it is outside the sandbox.

I tried to go the opposite way, use simple [webView loadRequest:file], and then in the actual html use full paths, i.e. prefix with NSBundle.mainBundle.resourcePath all the src and href, and it actually shows images, so you sort of do have read access, yet if you make the images (or html) a link and try to click the link you get the same message. There might not be a solution, I found references that this was added in some iOS 13 beta and some people thought it was a bug, but it seems to have stayed that way, but in case someone has an idea...


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...