WKWebView基本使用

- (void)initWKWebView

{

//创建并配置WKWebView的相关参数

//1.WKWebViewConfiguration:是WKWebView初始化时的配置类,里面存放着初始化WK的一系列属性;

//2.WKUserContentController:为JS提供了一个发送消息的通道并且可以向页面注入JS的类,WKUserContentController对象可以添加多个scriptMessageHandler;

//3.addScriptMessageHandler:name:有两个参数,第一个参数是userContentController的代理对象,第二个参数是JS里发送postMessage的对象。添加一个脚本消息的处理器,同时需要在JS中添加,window.webkit.messageHandlers..postMessage()才能起作用。

、、、

WKWebViewConfiguration*configuration = [[WKWebViewConfigurationalloc]init];

WKUserContentController*userContentController = [[WKUserContentControlleralloc]init];

[userContentControlleraddScriptMessageHandler:selfname:@"getUserid"];

configuration.userContentController= userContentController;

WKPreferences*preferences = [WKPreferencesnew];

preferences.javaScriptCanOpenWindowsAutomatically=YES;

preferences.minimumFontSize=40.0;

configuration.preferences= preferences;

//self.webView = [[WKWebView alloc] initWithFrame:self.view.frame configuration:configuration];

self.webView= [[WKWebViewalloc]initWithFrame:CGRectMake(0,0,self.view.frame.size.width,self.view.frame.size.height-108)configuration:configuration];

//设置访问的URL

NSURL*url = [NSURLURLWithString:@"http://2thewin.com/Home/Comment/index.html?matchid=1"];

//根据URL创建请求

NSURLRequest*request = [NSURLRequestrequestWithURL:url];

// WKWebView加载请求

[self.webViewloadRequest:request];

self.webView.UIDelegate=self;

self.webView.backgroundColor= [UIColorgreenColor];

[self.viewaddSubview:self.webView];

、、、}

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容