iOS 原生和H5交互总结

1.创建webview

UIWebView *_myWebView = [[UIWebView allow]initWithFrame :bounds];

_myWebView.delegate=self;//代理

[self.view addSubview:_myWebView];

2.网络请求数据加载到webview上

NSString * urlString=[NSString stringWithFormat:@"%@",@"http://www.baidu.com"]; //l链接一定加http或者https不然加载不出来

NSString *encodeStr = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];  //转码防止链接有中文

NSURL *url = [NSURL URLWithString:encodeStr]; 

[_myWebView loadRequest:request];

3.实现webview 代理

#pragma mark - NJKWebViewProgressDelegate

-(void)webViewProgress:(NJKWebViewProgress *)webViewProgress updateProgress:(float)progress

{

self.title = [_myWebView stringByEvaluatingJavaScriptFromString:@"document.title"];

}

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType

{

NSString *host = request.URL.absoluteString;  //获取当前的URL  根据当前的URL判断跳转逻辑

NSLog(@"host == %@",host);

return YES;

}

4 原生点击事件 webview返回上个webview

[_myWebView goBack];

你可能感兴趣的:(iOS 原生和H5交互总结)