WKWebView 獲取標題+進度條+常用代理方法以及native相容處理
1.屬性宣告
@property (copy,nonatomic) NSString *url;
@property (copy,nonatomic) NSString *pageTitle;
@property (nonatomic, strong) WKWebView *webView;
@property (nonatomic , strong) UIProgressView *processView;
@property (strong,nonatomic) UIBarButtonItem *backItem;//goBack
@property (strong,nonatomic) UIBarButtonItem *closeItem;//pop
@property (strong,nonatomic) UILabel *errorLabel;
@property (copy,nonatomic) NSString *currentUrl;//當前載入頁面的url
2.遵守協議
<WKNavigationDelegate,WKUIDelegate>
3. viewDidLoad 建立UI
- (void)viewDidLoad {
[super viewDidLoad];
self.title = self.pageTitle;
[self loadWebView];
}
-(void)loadWebView {
_webView = [[WKWebView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight-64)];
[self.view addSubview: self.webView];
_webView.allowsBackForwardNavigationGestures = YES;
_webView.navigationDelegate = self;
_webView.UIDelegate = self;
_webView.contentMode = UIViewContentModeScaleAspectFit;
_webView.scrollView.showsVerticalScrollIndicator = NO;
_webView.scrollView.showsHorizontalScrollIndicator = NO;
[_webView addObserver:self
forKeyPath:@"estimatedProgress"
options:NSKeyValueObservingOptionNew
context:nil];
[_webView addObserver:self
forKeyPath:@"title"
options:NSKeyValueObservingOptionNew
context:NULL];
// process
_processView = [[UIProgressView alloc] initWithFrame:CGRectMake(0, 1, kScreenWidth, 1)];
_processView.progressTintColor = MAINCOLOR;
_processView.trackTintColor = [UIColor whiteColor];
[self.view addSubview:self.processView];
NSURL *url = [NSURL URLWithString:self.url];
NSURLRequest *request = [[NSURLRequest alloc]initWithURL:url];
[self.webView loadRequest:request];
}
4. viewWillAppear 建立導航按鈕
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self setNav];
}
//建立導航條右側 返回 和 關閉 的按鈕
- (void)setNav {
if (self.navigationController.viewControllers.count > 1) {
UIButton *backBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
backBtn.frame = CGRectMake(0, 0, 50, 40);
[backBtn setTitle:@" 返回" forState:UIControlStateNormal];
[backBtn.titleLabel setFont:[UIFont systemFontOfSize:16]];
[backBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[backBtn setImage:[UIImage imageNamed:@"return"] forState:UIControlStateNormal];
[backBtn setImage:[UIImage imageNamed:@"return"] forState:UIControlStateHighlighted];
[backBtn setContentHorizontalAlignment:(UIControlContentHorizontalAlignmentLeft)];
[backBtn addTarget:self action:@selector(backClick) forControlEvents:UIControlEventTouchUpInside];
[backBtn.imageView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.mas_equalTo(backBtn);
make.height.mas_equalTo(@18);
make.centerY.mas_equalTo(backBtn);
}];
self.backItem = [[UIBarButtonItem alloc] initWithCustomView:backBtn];
UIButton *closeBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
[closeBtn setContentHorizontalAlignment:(UIControlContentHorizontalAlignmentLeft)];
closeBtn.frame = CGRectMake(0, 0, 40, 40);
[closeBtn setTitle:@"關閉" forState:UIControlStateNormal];
[closeBtn.titleLabel setFont:backBtn.titleLabel.font];
[closeBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[closeBtn addTarget:self action:@selector(closeClick) forControlEvents:UIControlEventTouchUpInside];
self.closeItem = [[UIBarButtonItem alloc] initWithCustomView:closeBtn];
self.navigationItem.leftBarButtonItem = self.backItem;
}
}
-(void)viewDidDisappear:(BOOL)animated{
[self.webView stopLoading];
[self.webView removeObserver:self forKeyPath:@"estimatedProgress"];
[self.webView removeObserver:self forKeyPath:@"title"];
self.webView = nil;
NSURLCache * cache = [NSURLCache sharedURLCache];
[cache removeAllCachedResponses];
[cache setDiskCapacity:0];
[cache setMemoryCapacity:0];
[super viewDidDisappear:animated];
}
6. KVO 監聽進度變化和標題變化
#pragma mark ----- KVO ----------
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
if ([keyPath isEqualToString:@"estimatedProgress"]) {
if (object == self.webView) {
[self.processView setAlpha:1.0f];
[self.processView setProgress:self.webView.estimatedProgress animated:YES];
if(self.webView.estimatedProgress >= 1.0f) {
[UIView animateWithDuration:0.3 delay:0.5 options:UIViewAnimationOptionCurveEaseOut animations:^{
[self.processView setAlpha:0.0f];
} completion:^(BOOL finished) {
[self.processView setProgress:0.0f animated:NO];
}];
}
}
else
{
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
}
}
else if ([keyPath isEqualToString:@"title"])
{
if (object == self.webView) {
if (self.webView.title.length != 0) {
self.title = self.webView.title;
}
}
else
{
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
}
}
else {
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
}
}
#pragma mark ------- WKNavigationDelegate -----
//網頁開始載入
- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation {
self.currentUrl = webView.URL.absoluteString;
NSLog(@"currentUrl====%@",self.currentUrl);
}
//網頁載入完成
- (void)webView:(WKWebView *)webView didFinishNavigation:(null_unspecified WKNavigation *)navigation {
if (webView.backForwardList.backList.count > 0) {
self.navigationItem.leftBarButtonItems = @[self.backItem,self.closeItem];
}
}
//網頁載入失敗
- (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(null_unspecified WKNavigation *)navigation withError:(NSError *)error {
[self.view addSubview:self.errorLabel];
}
8. 回退和關閉按鈕點選方法
#pragma mark -------- action ----------
//回退goback
- (void)backClick {
HtmlModel *model = [LocalStoreTools getHtmlModel];
if ([self.url rangeOfString:model.alarm4Dosing].location != NSNotFound &&
[self.currentUrl rangeOfString:@"index.html"].location != NSNotFound) {//用藥提醒
[self closeClick];
} else {
if ([self.webView canGoBack]) {
[self.webView goBack];
} else {
[self closeClick];
}
}
}
//關閉pop
- (void)closeClick {
if (self.presentingViewController != nil) {
[self dismissViewControllerAnimated:NO completion:nil];
} else {
[self.navigationController popViewControllerAnimated:YES];
}
}
9. native相容處理
//提示框
- (void)webView:(WKWebView *)webView runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(void))completionHandler{
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:message?:@"" preferredStyle:UIAlertControllerStyleAlert];
[alertController addAction:([UIAlertAction actionWithTitle:@"確認" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
completionHandler();
}])];
[self presentViewController:alertController animated:YES completion:nil];
}
//彈出框
- (void)webView:(WKWebView *)webView runJavaScriptConfirmPanelWithMessage:(NSString *)message initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(BOOL))completionHandler{
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:message?:@"" preferredStyle:UIAlertControllerStyleAlert];
[alertController addAction:([UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
completionHandler(NO);
}])];
[alertController addAction:([UIAlertAction actionWithTitle:@"確認" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
completionHandler(YES);
}])];
[self presentViewController:alertController animated:YES completion:nil];
}
//輸入框
- (void)webView:(WKWebView *)webView runJavaScriptTextInputPanelWithPrompt:(NSString *)prompt defaultText:(NSString *)defaultText initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(NSString * _Nullable))completionHandler{
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:prompt message:@"" preferredStyle:UIAlertControllerStyleAlert];
[alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
textField.text = defaultText;
}];
[alertController addAction:([UIAlertAction actionWithTitle:@"完成" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
completionHandler(alertController.textFields[0].text?:@"");
}])];
[self presentViewController:alertController animated:YES completion:nil];
}
//處理 target = "__blank"
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler
{
if (!navigationAction.targetFrame.isMainFrame) {
[webView evaluateJavaScript:@"var a = document.getElementsByTagName('a');for(var i=0;i<a.length;i++){a[i].setAttribute('target','');}" completionHandler:nil];
}
decisionHandler(WKNavigationActionPolicyAllow);
}
相關文章
- wkwebView 新增 進度條WebView
- iOS WKWebView新增進度條02iOSWebView
- Ajax 處理時進度條使用
- NSURLProtocol對WKWebView的處理ProtocolWebView
- 【新特性速遞】進度條,進度條,進度條
- React Native 之 獲取螢幕解析度React Native
- 移動端頁面寬度相容處理
- ios 如何獲取WKWebview錯誤資訊iOSWebView
- 神奇的meta標籤處理相容性
- javascript如何獲取滾動條的寬度JavaScript
- 讀取檔案大小-列印進度條
- Win7相容性問題的處理方法Win7
- ES系列(五):獲取單條資料get處理過程實現
- HttpServletRequest常用獲取URL的方法HTTPServlet
- 事件相容處理事件
- JavaScript常用的字串處理方法JavaScript字串
- ajax進度條 非同步下載進度條非同步
- iOS WKWebView UI增強(上拉重新整理,JS互動,載入進度條)iOSWebViewUIJS
- React-Native ‘WKWebView` has no propType for native propReactWebView
- Xamarin XAML語言教程使用方法設定進度條進度
- iOS獲取裝置資訊的常用方法iOS
- 瀏覽器相容問題處理總結瀏覽器
- React Native 橋接原生 iOS 以及 Android 獲取 APP 版本號React Native橋接iOSAndroidAPP
- 獲取字串長度的幾個常用方法字串
- java正規表示式四種常用的處理方式(匹配、分割、替代、獲取)Java
- Qt 進度條QT
- 處理問題的方法
- js如何獲取網頁的標題JS網頁
- Python Cookie HTTP獲取cookie並處理PythonCookieHTTP
- 百度地圖:根據位置獲取座標地圖
- sklearn中常用資料預處理方法
- (轉)Shell常用處理字串方法(備查)字串
- C# 下載帶進度條程式碼(普通進度條)C#
- 部落格園美化包括模板、目錄、頂部、進度條滑鼠特效、標題等特效
- jquery defered的progress方法實現進度條jQuery
- Oracle RAC 錯誤記錄以及處理方法Oracle
- HTML <progress> 進度條HTML
- canvas 畫進度條Canvas