【问题标题】:WKWebView with local file带有本地文件的 WKWebView
【发布时间】:2015-01-29 18:45:50
【问题描述】:

我正在使用本地文件测试 WKWebView,该文件在模拟器中运行,但在设备中不运行

 @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];
}

我授予的网址来自:

    NSURL *presentationFolder = [self.presentationController url];

没问题,因为我用 UIWebview 测试了相同的代码并且工作正常!

我总是遇到同样的错误:

Could not create a sandbox extension for '/'

这行不通,我想它在 Objective-C 中也可以像在 swift 中一样工作

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

任何想法将不胜感激,谢谢


2014 年 2 月 12 日更新

我发现这可能是 iOS 8.1 中的一个错误,它可能会在 8.2 中修复

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

我已经测试过将文件移动到临时文件夹,但没有收到任何错误,但 webView 只是空的。

我已经用 UIWebView 测试了相同的代码(临时文件夹)并且工作正常!

另外,我试过这个:

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

据我所知,这是因为 css 和 javascript 嵌入在 html 中。

【问题讨论】:

  • 我不认为这是一个错误,它在测试版中工作并停止在最终的 8.0 版本中工作,苹果知道它还没有修复它
  • 嗯,这比错误更糟糕:),尽管它可能在 beta 版中工作,因为嵌入了 css 和 javascript 代码。感谢您的评论
  • 顺便说一句,8.2 beta 好像也不能加载本地文件
  • 正如您在发布的最后一个链接中看到的那样,有些人正在使用 GCDWebServer (github.com/swisspol/GCDWebServer) 来加载文件
  • 是的,我看到了,但我认为这是一个很大的解决方法。

标签: ios objective-c wkwebview


【解决方案1】:

试试XWebView,它有一个非常小的嵌入式 http 服务器。它比 GCDWebServer 小得多。通过扩展添加了loadFileURL:allowingReadAccessToURL方法,所以你不知道服务器。

【讨论】:

    【解决方案2】:

    这就像一个魅力......

    @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];
    

    【讨论】:

    • allowFileAccessFromFileURLs 是私有财产
    猜你喜欢
    • 2016-06-03
    • 2018-07-29
    • 2018-12-26
    • 2017-05-24
    • 2018-07-12
    • 1970-01-01
    • 2016-04-08
    • 2014-09-13
    • 2020-10-25
    相关资源
    最近更新 更多