我有这个文件,我正在尝试重新加载,但是当我重新加载它时,我得到了应该显示的屏幕,然后是一个空白屏幕,直到我再次点击重新加载。我不确定为什么会出现这个空白屏幕。另外,我没有得到任何东西
- (void)webViewWebContentProcessDidTerminate:(WKWebView *)webView {}
也不
-(void)didReceiveMemoryWarning
这是我的 viewController.m 文件我有 index.html 文件在与它加载一次相同的目录中,但我不确定 webkit 是否崩溃或在哪里
#import "ViewController.h"
#import <WebKit/WebKit.h>
#import <JavaScriptCore/JavaScriptCore.h>
@interface ViewController ()
@end
@implementation ViewController
NSString *localURL;
NSURLRequest *urlRequest;
- (void)viewDidLoad {
[super viewDidLoad];
JSContext *ctx = [[JSContext alloc] init];
ctx[@"console"][@"log"] = ^(NSString *message) {
NSLog(@"Javascript log: %@", message);
};
// Do any additional setup after loading the view, typically from a nib.
// [UIView setAnimationsEnabled:NO];
//_webView = [[WKWebView alloc] initWithFrame:self.view.frame];
// _webView.navigationDelegate = self.webView ;
// _webView.UIDelegate = self;
//[self.view addSubview:_webView ];
_webView= [[WKWebView alloc] initWithFrame:CGRectMake(0, 100, 500, 500)];
localURL = [[NSBundle mainBundle] pathForResource:@"index" ofType:@"html"];
urlRequest = [NSURLRequest requestWithURL:[NSURL fileURLWithPath:localURL]];
[_webView loadRequest:urlRequest];
[self.view addSubview:_webView];
//button
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button addTarget:self action:@selector(reload) forControlEvents:UIControlEventTouchUpInside];
[button setTitle:@"Reload" forState:UIControlStateNormal];
button.frame = CGRectMake(10.0, 20.0, 100.0, 50.0);
button.backgroundColor = UIColor.blackColor;
[self.view addSubview:button];
}
- (void)webViewWebContentProcessDidTerminate:(WKWebView *)webView {
// Reload current page, since we have crashed the WebContent process
// (most likely due to memory pressure)
NSLog(@"most likely due to memory pressure reloaded");
[webView reload];
}
-(void)reload{
NSLog(@"reloading now");
[self.webView loadRequest:urlRequest];
//[self.webView stopLoading];
// [self loadView];
//[_webView loadRequest:urlRequest];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
NSLog(@"memory issue");
}
//- (IBAction)rButton:(UIButton *)sender {
// NSLog(@"Well we didnt crash");
// [self.webView reload];
//}
@end
最佳答案
如果问题是由于网络终止,则将调用委托(delegate)函数 webViewWebContentProcessDidTerminate。
在您的情况下,您似乎没有将 viewController 设置为 webview 的 navigationDelegate:
webview.navigationDelegate = self
关于ios - WKWebView 重新加载,然后为本地 index.html 文件变为空白,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45846533/