首页 > 技术文章 > webview重新加载(reload)或者发起 redirect request导致js和objc代码之间的bridge失联解决方案(亲测有效)

linusflow 2018-04-01 21:13 原文

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType { 
//isNotFirstLoad' marks if it is the fisrt load.

//isNotFirstLoad是代表当前这个webview是不是已经加载了一次request if (isNotFirstLoad) { //如果不加上这个判断,后面出现的loadRequest函数的调用就会导致出现死循环 //create an new webview CGRect frame = _myWebView.frame; [_myWebView removeFromSuperview]; _myWebView = [[EasyJSWebView alloc] initWithFrame:frame]; [self.view addSubview:_myWebView]; //init the new webview _webView.delegate = self; javaScriptInterface* interface = [javaScriptInterface new]; [self.webView addJavascriptInterfaces:interface WithName:@"interface"]; [_myWebView loadRequest:request]; //reset the firstload flag to load the new request isNotFirstLoad = NO; //返回No,则不加载本方法传入的的request return NO; } isNotFirstLoad = YES; return YES; //YES if the web view should begin loading content; otherwise, NO .

}

推荐阅读