10

I'm testing the WKWebView with local file, which is working in the simulator but it is not working in the device

 @interface EDPresentationViewController ()<WKNavigationDelegate,WKScriptMessageHandler>


     @property(nonatomic,strong)WKWebView *webView;

     @property(nonatomic,strong)EDPresentationController *presentationController;

 @end


@implementation EDPresentationViewController

-(void)viewDidLoad
{
    [super viewDidLoad];

    self.presentationController = [[EDPresentationController alloc]init];

    WKWebViewConfiguration *webConfiguration = [[WKWebViewConfiguration alloc]init];
    self.webView = [[WKWebView alloc]initWithFrame:self.view.frame configuration:webConfiguration];

    NSURL *presentationFolder = [self.presentationController url];
    NSURLRequest *request = [NSURLRequest requestWithURL:presentationFolder];

    [self.webView loadRequest:request];
}

I grant the url from:

    NSURL *presentationFolder = [self.presentationController url];

is ok, because I tested the same code with a UIWebview and works!

I always get the same error:

Could not create a sandbox extension for '/'

This wasn't work, I guess it would work in Objective-C as in swift

iOS Webkit not working on device, but works on simulator at swift

Any idea will be appreciated, thanks


Update 2-12-2014

I've discovered this could be a bug in iOS 8.1 and it may be fixed in 8.2

https://devforums.apple.com/thread/247777?start=25&tstart=0

I've tested moving the files to the temporary folder and I didn't get any error but the webView is just empty.

I've tested the same code (temporary folder) with a UIWebView and works fine!

Also, I've tried this:

https://stackoverflow.com/a/26054170/426180

As I could find out, this works because the css and the javascript is embebed in the html.

Community
  • 1
  • 1
xarly
  • 2,054
  • 4
  • 24
  • 40
  • I don't think it's a bug, it was working on the betas and stopped working on the final 8.0 release, apple knows it and hasn't fix it yet – jcesarmobile Dec 02 '14 at 11:13
  • Well, that worse than a bug :), although maybe it was working in the betas because the css and javascript code is embebed. Thanks for your comment – xarly Dec 02 '14 at 11:35
  • BTW, it seems that the 8.2 beta can't load local files either – jcesarmobile Dec 02 '14 at 11:53
  • As you might have seen on the last link you posted, some people is using the GCDWebServer (https://github.com/swisspol/GCDWebServer) to load the files – jcesarmobile Dec 02 '14 at 11:56
  • Yes, I saw that, but I consider it's a big workaround. – xarly Dec 02 '14 at 12:01
  • You should not use `loadRequest` for a local file. Use either `loadHTMLString:baseURL:` or `loadFileURL:allowingReadAccessToURL:`. Cf WKWebView documentation: [link]https://developer.apple.com/documentation/webkit/wkwebview/1414973-loadfileurl?language=objc `Use the loadHTMLString:baseURL: method to begin loading local HTML files or the loadRequest: method to begin loading web content.` – Pierre Jul 01 '20 at 09:16

2 Answers2

4

Try XWebView which has a very tiny embedded http server. It's much smaller than the GCDWebServer. A loadFileURL:allowingReadAccessToURL method is added through extension, so you are not aware of the server.

soflare
  • 751
  • 5
  • 16
2

This worked like a charm ...

@interface ViewController () <WKScriptMessageHandler, WKNavigationDelegate>

...

WKWebViewConfiguration *configuration = [[WKWebViewConfiguration alloc] init];
WKUserContentController *controller = [[WKUserContentController alloc] init];

configuration.userContentController = controller;
[configuration.preferences setValue:@"TRUE" forKey:@"allowFileAccessFromFileURLs"];
self.webView = [[WKWebView alloc] initWithFrame:[UIScreen mainScreen].bounds configuration:configuration];
self.webView.navigationDelegate = self;
// Also if you'd have bouncing problem
self.webView.scrollView.bounces = false;
self.webView.scrollView.alwaysBounceVertical = false;
[self.view addSubview:self.webView];

NSString* productURL = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"htmlapp/home.html"];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL fileURLWithPath:productURL]];
[self.webView loadRequest:request];
nullqube
  • 2,959
  • 19
  • 18