WebView
//加载方法
- (void)loadRequest:(NSURLRequest *)request;
- (void)loadHTMLString:(NSString *)string baseURL:(nullable NSURL *)baseURL;
- (void)loadData:(NSData *)data MIMEType:(NSString *)MIMEType textEncodingName:(NSString *)textEncodingName baseURL:(NSURL *)baseURL;//执行JavaScript脚本【由于是同步的,需在主线程】
- (nullable NSString *)stringByEvaluatingJavaScriptFromString:(NSString *)script;UIWebViewDelegate
//是否可加载指定的网页,常用来和JS交互
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType;//开始加载时调用
- (void)webViewDidStartLoad:(UIWebView *)webView;//加载完成时调用
- (void)webViewDidFinishLoad:(UIWebView *)webView;//加载失败时调用
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error;OC调用JS
1、JavaScriptCore(iOS 7.0引入)
JSContext *context = [self.webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
JSValue *value = [context evaluateScript:@"document.title"];
self.title = value.toString;2、调用stringByEvaluatingJavaScriptFromString
stringByEvaluatingJavaScriptFromStringJS调用OC
1、JavaScriptCore
JavaScriptCore2、shouldStartLoadWithRequest拦截
shouldStartLoadWithRequest拦截Cookie
WKWebView
包含14个类和3个协议
初始化示例
WKNavigationDelegate
WKScriptMessageHandler
Cookie
Last updated
Was this helpful?