WKWebView使用
程序员文章站
2022-03-20 17:01:39
...
@interface BaiduViewController () <WKNavigationDelegate, WKUIDelegate>
@property (strong, nonatomic) WKWebView *webView;
@end
@implementation BaiduViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
// 图片缩放的js代码
NSString *js = @"var count = document.images.length;for (var i = 0; i < count; i++) {var image = document.images[i];image.style.width=320;};window.alert('加载' + count + '的图');";
// 根据JS字符串初始化WKUserScript对象
WKUserScript *script = [[WKUserScript alloc] initWithSource:js injectionTime:WKUserScriptInjectionTimeAtDocumentEnd forMainFrameOnly:YES];
// 根据生成的WKUserScript对象,初始化WKWebViewConfiguration
WKWebViewConfiguration *config = [[WKWebViewConfiguration alloc] init];
[config.userContentController addUserScript:script];
_webView = [[WKWebView alloc] initWithFrame:self.view.bounds configuration:config];
//_webView = [[WKWebView alloc] initWithFrame:self.view.bounds];
_webView.UIDelegate = self;
_webView.navigationDelegate = self;
[self.view addSubview:_webView];
[_webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"https://www.baidu.com"]]];
}
#pragma mark - WKNavigationDelegate
/**
* 在发送请求之前,决定是否跳转 01
*
* @param webView 实现该代理的webview
* @param navigationAction 当前navigation
* @param decisionHandler 是否调转block
*/
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler {
// 是否允许跳转
decisionHandler(WKNavigationActionPolicyAllow);
}
/**
* 页面开始加载时调用 02
*
* @param webView 实现该代理的webview
* @param navigation 当前navigation
*/
- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation {
}
/**
* 在收到响应后,决定是否跳转 03
*
* @param webView 实现该代理的webview
* @param navigationResponse 当前navigation
* @param decisionHandler 是否跳转block
*/
- (void)webView:(WKWebView *)webView decidePolicyForNavigationResponse:(WKNavigationResponse *)navigationResponse decisionHandler:(void (^)(WKNavigationResponsePolicy))decisionHandler {
NSString *lowercaseString = navigationResponse.response.URL.host.lowercaseString ;
// 是否允许跳转
decisionHandler(WKNavigationResponsePolicyAllow);
}
/**
* 当内容开始返回时调用 04
*
* @param webView 实现该代理的webview
* @param navigation 当前navigation
*/
- (void)webView:(WKWebView *)webView didCommitNavigation:(WKNavigation *)navigation {
}
/**
* 页面加载完成之后调用 05
*
* @param webView 实现该代理的webview
* @param navigation 当前navigation
*/
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation {
}
/**
* 加载失败时调用
*
* @param webView 实现该代理的webview
* @param navigation 当前navigation
* @param error 错误
*/
- (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(WKNavigation *)navigation withError:(NSError *)error {
}
/**
* 接收到服务器跳转请求之后调用
*
* @param webView 实现该代理的webview
* @param navigation 当前navigation
*/
- (void)webView:(WKWebView *)webView didReceiveServerRedirectForProvisionalNavigation:(WKNavigation *)navigation {
}
#pragma mark - WKUIDelegate
/**
* web界面中有弹出警告框时调用
*
* @param webView 实现该代理的webview
* @param message 警告框中的内容
* @param frame 主窗口
* @param completionHandler 警告框消失调用
*/
- (void)webView:(WKWebView *)webView runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(void))completionHandler {
[[[UIAlertView alloc] initWithTitle:@"温馨提示" message:message delegate:nil cancelButtonTitle:@"确认" otherButtonTitles: nil] show];
completionHandler();
}
//运行JavaScript确认面板与消息
- (void)webView:(WKWebView *)webView runJavaScriptConfirmPanelWithMessage:(NSString *)message initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(BOOL))completionHandler {
NSLog(@"message%@",message);
}
//运行JavaScript确认面板与消息
- (void)webView:(WKWebView *)webView runJavaScriptTextInputPanelWithPrompt:(NSString *)prompt defaultText:(NSString *)defaultText initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(NSString *))completionHandler {
NSLog(@"defaultText%@",defaultText);
}
// 从web界面中接收到一个脚本时调用
- (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message {
NSLog(@"%@", message);
}
@end
上一篇: ‘’‘安装PyMouse,个人日志'''
下一篇: HTML标签:img标签的用法总结
推荐阅读
-
省点花锦鲤卡app怎么激活 省点花锦鲤卡激活后怎么使用
-
Android自定义View 使用PathMeasure简单模仿系统ProgressBar(四)
-
solidworks零件模型怎么使用剖面命令?
-
Android studio怎么使用git获取最新内容然后合并?
-
省点花锦鲤卡可以在美团上使用吗 省点花锦鲤卡app怎么在美团上用
-
Wing FTP Server FTP服务器端中文版安装使用教程
-
eclipse格式化代码快捷键无法使用怎么办?
-
android使用DataBinding来设置空状态
-
IDPhotoStudio证件照打印使用教程
-
百中搜优化软件怎么样?百中搜优化软件使用教程(附视频教程)