html - wkwebview run local html (in document) Received an unexpected URL from the web 进程

标签 html iframe document wkwebview

两个问题:

1.access files as .css .js 应该像你看到的index.html中的代码一样通过fullPath;

2.当index.html的代码运行到“document.body.appendChild(iFrame)”时,模拟器运行到空白;xcode日志:

从网络进程收到一个意外的 URL:'file:///Users/John_Chen/Library/Developer/CoreSimulator/Devices/20ABEC24-78BD-4577-9AA5-35CB1520F620/data/Containers/Data/Application/30A53A36- 4A05-4491-A400-CEBF5F9E7D21/Documents/test/2.html' 从 Web 进程收到无效消息“WebPageProxy.DecidePolicyForNavigationAction”。

有人可以帮忙吗? TKS!

iOS 代码:

1.解压test.zip到文档:

- (void)copyFloderToSandbox
{
   NSFileManager *fm = [NSFileManager defaultManager];
   NSString *originalPlayerPath = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"zip"];
   NSString *originalMovi_01Path = [[NSBundle mainBundle] pathForResource:@"movi_01" ofType:@"zip"];
   NSString *documentPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
   NSString *PDoc = [documentPath stringByAppendingPathComponent:@"test"];
   NSString *MDoc = [documentPath stringByAppendingPathComponent:@"movi_01"];

  if (![fm fileExistsAtPath:PDoc]) 
 {
    [SSZipArchive unzipFileAtPath:originalPlayerPath toDestination:documentPath];
 }
  if (![fm fileExistsAtPath:MDoc]) 
 {
    [SSZipArchive unzipFileAtPath:originalMovi_01Path toDestination:documentPath];
 }
}

2.WKWebView加载index.html:

- (void)viewDidLoad {
[super viewDidLoad];

[self copyFloderToSandbox];

self.webView = [[WKWebView alloc]initWithFrame:self.view.bounds configuration:[self configWeb]];
_webView.navigationDelegate = self;
_webView.UIDelegate = self;
[self.view addSubview:_webView];


NSString *playerPath = [OMDBManager getLocalDataFilePathWithPathComponent:@"test"];
NSString *html = [playerPath stringByAppendingPathComponent:@"index.html"];
NSString *moviePath = [OMDBManager getLocalDataFilePathWithPathComponent:@"movi_01"];
NSString *moviParameter = [NSString stringWithFormat:@"movieroot=%@",playerPath];
NSString *playParameter = [NSString stringWithFormat:@"playerroot=%@",playerPath];
NSString *baseString = [NSString stringWithFormat:@"%@?%@&%@",html,moviParameter,playParameter];

NSURL *baseUrl = [NSURL fileURLWithPath:baseString isDirectory:YES];
NSString *htmlContent = [NSString stringWithContentsOfFile:html encoding:NSUTF8StringEncoding error:nil];
[_webView loadHTMLString:htmlContent baseURL:baseUrl];
}

- (WKWebViewConfiguration *)configWeb
{
    WKWebViewConfiguration *configuration = [[WKWebViewConfiguration      alloc]init];
configuration.preferences = [[WKPreferences alloc]init];
configuration.preferences.minimumFontSize = 18;
configuration.preferences.javaScriptCanOpenWindowsAutomatically = YES;
configuration.userContentController = [[WKUserContentController alloc]init];
[configuration.userContentController addScriptMessageHandler:self name:@"readJsonFile"];
return configuration;
 }

files in document directory codes in index.html

最佳答案

我认为这是因为您没有使用 loadFileURL:allowingReadAccessToURL: 方法 第二个参数是 html 所在的目录。

关于html - wkwebview run local html (in document) Received an unexpected URL from the web 进程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40123321/

相关文章:

javascript - 无需单击按钮即可计算值的总和

jquery - Datatables jQuery $(document).ready 不工作并被数字取代

javascript - "Access is denied"尝试访问以编程方式创建的 <iframe> 的文档对象时出现 JavaScript 错误(仅限 IE)

javascript - 谁能帮我弄清楚为什么我不能让 Jquery 摇动效果在我的 div 上工作?

jquery - 强制 Bootstrap 3 移动布局

jquery - 使用jQuery突出显示元素,无法获取内部元素

javascript - 是否可以编写一个搜索引擎算法来显示基于搜索关键字的 iframe?

apache - 无法在父jsp中访问不同来源的iframe

objective-c - 自定义 UTI 未在 App 中打开

latex - 如何在 LyX 中显示更多文本?让页面更宽?