WKWebViewは簡単に使えます.
3718 ワード
WKWebViewの紹介:1、JavaScriptのNitroライブラリのロードを許可し、呼び出し(UICbViewで制限)2、より多くのH 5特性をサポートする3、UICWebViewとUID WebViewの重さを14種類と3つのプロトコルで構成しています.
:
WKWebViewConfiguration *config = [[WKWebViewConfiguration alloc] init];
// :preferences
config.preferences = [WKPreferences new];
//The minimum font size in points default is 0;
config.preferences.minimumFontSize = 10;
// JavaScript
config.preferences.javaScriptEnabled = YES;
// ,
config.preferences.javaScriptCanOpenWindowsAutomatically = YES;
// JS webView
config.userContentController = [WKUserContentController new];
//
[config.userContentController addScriptMessageHandler:self name:@"js "];
// WKWebView
:
WkWebView * wkwebView = [[WKWebView alloc] initWithFrame:frame configuration:config];
self.wkwebView.navigationDelegate = self;
self.wkwebView.UIDelegate = self;
NSURLRequest * request = [NSURLRequest requestWithURL:[NSURL URLWithString:str]];
[self.wkwebView loadRequest:request];
WKNavigationDelegate
// , 。 ,web
-(void)webView:(WKWebView *)webView decidePolicyForNavigationResponse:(WKNavigationResponse *)navigationResponse decisionHandler:(void (^)(WKNavigationResponsePolicy))decisionHandler{
decisionHandler(WKNavigationResponsePolicyAllow);
}
// ,
-(void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler{
decisionHandler(WKNavigationActionPolicyAllow);
}
//
- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation;
//
- (void)webView:(WKWebView *)webView didCommitNavigation:(WKNavigation *)navigation;
OC JS
//
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation;
OC JS
//javaScriptString JS ,completionHandler block
[self.webView evaluateJavaScript:javaScriptString completionHandler:completionHandler];
//
- (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(WKNavigation *)navigation;
ScriptMessageHandler
JS OC
JS , WKWebView , JS
-(void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message{
NSLog(@"%@",message.name);
NSLog(@"%@",message.body);
}
WKWebView
window.webkit.messageHandlers..postMessage()
//
@interface WeakScriptMessageDelegate : NSObject
@property (nonatomic, weak) id scriptDelegate;
- (instancetype)initWithDelegate:(id)scriptDelegate;
@end
@implementation WeakScriptMessageDelegate
- (instancetype)initWithDelegate:(id)scriptDelegate
{
self = [super init];
if (self) {
_scriptDelegate = scriptDelegate;
}
return self;
}
- (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message
{
[self.scriptDelegate userContentController:userContentController didReceiveScriptMessage:message];
}
@end
WKUserContentController *userContentController = [[WKUserContentController alloc] init];
[userContentController addScriptMessageHandler:[[WeakScriptMessageDelegate alloc] initWithDelegate:self] name:@"closeMe"];
self dealloc
[[_webView configuration].userContentController removeScriptMessageHandlerForName:@"closeMe"];