Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WKWebView with local file

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.

like image 834
xarly Avatar asked Dec 01 '14 17:12

xarly


2 Answers

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.

like image 52
soflare Avatar answered Oct 19 '22 19:10

soflare


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];
like image 33
nullqube Avatar answered Oct 19 '22 17:10

nullqube