WKWebView的使用

iOS 8 更新的WKWebView,说说项目中怎么用它,其中包括了获取标题,加载进度,和webview跟原生的交互打电话。

#import "BaseViewController.h"
#import 

@interface BaseWebViewController : BaseViewController

- (instancetype)initWithUrl:(NSString *)urlStr;

@property(nonatomic,strong)WKWebView *webView;

@property(nonatomic,copy)NSString *urlString;

@property(nonatomic,strong)UIProgressView *progressView;

@end
#import "BaseWebViewController.h"


@interface BaseWebViewController ()


@end

@implementation BaseWebViewController



- (instancetype)initWithUrl:(NSString *)urlStr{
    
    self = [super init];
    if (self) {
        
        self.urlString = urlStr;
        
    }
    return self;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self loadUI];
    
}

- (void)loadUI{
    

    self.webView = [[WKWebView alloc]initWithFrame:CGRectMake(0, 0, KScreenWidth, KScreenHeight-64)];
    self.webView.backgroundColor = [UIColor blackColor];
    self.webView.navigationDelegate = self;
    [self.view addSubview:self.webView];
    
    self.progressView = [[UIProgressView alloc]initWithFrame:CGRectMake(0, 0, KScreenWidth, 2)];
    //进度条的颜色
    self.progressView.trackTintColor = [UIColor blackColor];
    self.progressView.progressTintColor = KColorFromRGB(0x00c853);
    [self.view addSubview:self.progressView];
    
    [self.webView addObserver:self
                   forKeyPath:@"estimatedProgress"
                      options:NSKeyValueObservingOptionNew
                      context:nil];
    [self.webView addObserver:self forKeyPath:@"title" options:NSKeyValueObservingOptionNew context:NULL];

    
    NSURL *url = [NSURL URLWithString:self.urlString];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [self.webView loadRequest:request];
    
    
}

#pragma mark- 监听进度的变化
- (void)observeValueForKeyPath:(NSString *)keyPath
                      ofObject:(id)object
                        change:(NSDictionary *)change
                       context:(void *)context
{
    if ([keyPath isEqualToString:@"estimatedProgress"]) {
        
        self.progressView.progress = self.webView.estimatedProgress;
        NSLog(@"%f",self.webView.estimatedProgress);
        // 加载完成
        if (self.webView.estimatedProgress  >= 1.0f ) {
            
            [UIView animateWithDuration:0.25f animations:^{
                self.progressView.alpha = 0.0f;
                self.progressView.progress = 0.0f;
            }];
            
        }else{
            self.progressView.alpha = 1.0f;
        }
        
        
    }else if ([keyPath isEqualToString:@"title"])
    {
       
//        self.title = self.webView.title;
    }
    
    
}

#pragma mark- 在发送请求之前,决定是否跳转的代理
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler{
    
    DLog(@"在发送请求之前,决定是否跳转的代理");
    NSString *url =  [navigationAction.request.URL absoluteString];
    if ([url hasPrefix:@"tel"]) {
        
        //打电话
        NSString *telStr = [url substringFromIndex:4];
        
        [self callPhone:telStr];
        
        decisionHandler(WKNavigationActionPolicyCancel);
    }
    
    decisionHandler(WKNavigationActionPolicyAllow);
    
}

- (void)callPhone:(NSString *)telStr
{
    NSMutableString* str=[[NSMutableString alloc] initWithFormat:@"tel:%@",telStr];
    
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];
}



#pragma mark-在收到响应后,决定是否跳转的代理
- (void)webView:(WKWebView *)webView decidePolicyForNavigationResponse:(nonnull WKNavigationResponse *)navigationResponse decisionHandler:(nonnull void (^)(WKNavigationResponsePolicy))decisionHandler{
    
    DLog(@"在收到响应后,决定是否跳转的代理");
    decisionHandler(WKNavigationResponsePolicyAllow);
    
}


#pragma mark-接收到服务器跳转请求的代理
- (void)webView:(WKWebView *)webView didReceiveServerRedirectForProvisionalNavigation:(WKNavigation *)navigation{
    
    DLog(@"接收到服务器跳转请求的代理");
    
}




#pragma mark-准备加载页面
- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(null_unspecified WKNavigation *)navigation{
    
    DLog(@"准备开始加载页面");
    
}

#pragma mark- 内容开始加载(view的过渡动画可在此方法中加载)
- (void)webView:(WKWebView *)webView didCommitNavigation:(null_unspecified WKNavigation *)navigation{
    DLog(@"内容开始加载(view的过渡动画可在此方法中加载)");
}

#pragma mark- 页面加载完成(view的过渡动画的移除可在此方法中进行)
- (void)webView:(WKWebView *)webView didFinishNavigation:(null_unspecified WKNavigation *)navigation{
    
    DLog(@"页面加载完成(view的过渡动画的移除可在此方法中进行))");
    
}

#pragma mark- 页面加载失败
- (void)webView:(WKWebView *)webView didFailNavigation:(null_unspecified WKNavigation *)navigation withError:(NSError *)error{
    
    DLog(@"页面加载失败");
    
    
}

- (void)dealloc {
    [self.webView removeObserver:self forKeyPath:@"estimatedProgress" context:nil];
    [self.webView removeObserver:self forKeyPath:@"title" context:nil];
}




@end

你可能感兴趣的:(WKWebView的使用)