天天看點

原生和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];

位址 http://www.jianshu.com/p/ee1aeb1f72f3