WKWebView

 1.1 加载某个网页
 1.2 MK自带加载进度条

1.加载某个URL

需导入 #import 

@property (nonatomic , strong)WKWebView *mainWebView;

self.mainWebView = [[WKWebView 
alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];

NSURL *url = [NSURL 
URLWithString:@"http://www.jianshu.com/users/d10b02ea2d91/latest_articles"];

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];

[self.mainWebView loadRequest:request];
[self.view addSubview:self.mainWebView];

1.2 MK自带加载进度条

#import "ViewController.h"
#import 

#define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width //屏幕宽度
#define SCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height //屏幕高度

@interface ViewController ()

@property (nonatomic, strong) WKWebView *mainWebView;
@property (nonatomic, strong) UIProgressView *progressView;

@end

@implementation ViewController

#pragma mark Life Cycle
- (void)viewDidLoad {
    
    [super viewDidLoad];
    [self.view addSubview:self.mainWebView];
    [self.view addSubview:self.progressView];
    [self.self.mainWebView loadRequest:[NSURLRequest 
    requestWithURL:[NSURL URLWithString:@"http://www.jianshu.com/users
    /d10b02ea2d91/latest_articles"]]];
    
    [self.mainWebView addObserver:self
                       forKeyPath:@"estimatedProgress"
                          options:NSKeyValueObservingOptionNew
                          context:nil];
}  

#pragma mark KVO
- (void)observeValueForKeyPath:(NSString *)keyPath
                      ofObject:(id)object
                        change:(NSDictionary *)change
                       context:(void *)context
{
    if ([keyPath isEqualToString:@"estimatedProgress"]) {
        
        self.progressView.progress = self.mainWebView.estimatedProgress;
        // 加载完成
        if (self.mainWebView.estimatedProgress  >= 1.0f ) {
            
            [UIView animateWithDuration:0.5f animations:^{
                self.progressView.alpha = 0.0f;
                self.progressView.progress = 0.0f;
            }];
            
        }else{
            self.progressView.alpha = 1.0f;
        }
        
    }
    
}

- (WKWebView *)mainWebView {
    
    if (!_mainWebView) {
        _mainWebView = [[WKWebView alloc] init];
        _mainWebView.autoresizingMask = 
        UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
        _mainWebView.backgroundColor = [UIColor whiteColor];
        _mainWebView.scrollView.showsVerticalScrollIndicator = NO;
        _mainWebView.scrollView.showsHorizontalScrollIndicator = NO;
        _mainWebView.frame = CGRectMake(0, 64, SCREEN_WIDTH, 
        SCREEN_HEIGHT - 64);
    }
    return _mainWebView;
}

- (UIProgressView *)progressView {
    
    if (!_progressView) {
        _progressView = [[UIProgressView alloc] init];
        _progressView.frame = CGRectMake(0, 64, SCREEN_WIDTH, 2);
    }
    return _progressView;

}

//移除通知
- (void)dealloc {
    
    [self.mainWebView removeObserver:self forKeyPath:@"estimatedProgress" 
    context:nil];
    
}

@end

你可能感兴趣的:(WKWebView)