WKWebView重新加载,然后为本地index.html文件变为空白

时间:2017-08-23 17:59:52

标签: ios objective-c wkwebview

我有这个文件,我试图重新加载,但是当我重新加载它时,我得到的屏幕应该显示,然后是一个空白屏幕,直到我再次重新加载。我不知道为什么我会得到这个空白屏幕。

我没有得到任何东西
- (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

1 个答案:

答案 0 :(得分:2)

如果问题是由于Web终止,将调用委托函数webViewWebContentProcessDidTerminate。

在你的情况下,它看起来你没有将viewController设置为webview的navigationDelegate:

webview.navigationDelegate = self