ios Web视图之WKWebView

ios Web视图之WKWebView_第1张图片
图片.png
/**
 WKWebView Demo
 */
- (void) wkWebViewDemo{
    CGRect screen = [[UIScreen mainScreen] bounds];
    
    //按钮栏
    CGFloat buttonBarWidth = 316;
    UIView *buttonBar = [[UIView alloc] initWithFrame:CGRectMake((screen.size.width - buttonBarWidth)/2, 20, buttonBarWidth, 100)];
    buttonBar.backgroundColor = [UIColor orangeColor];
    [self.view addSubview:buttonBar];
    
    //添加LoadHTMLString按钮
    UIButton *buttonLoadHTMLString = [UIButton buttonWithType:UIButtonTypeSystem];
    [buttonLoadHTMLString setTitle:@"Load\nHTML\nString" forState:UIControlStateNormal];
    buttonLoadHTMLString.titleLabel.numberOfLines = 3;
//    buttonLoadHTMLString.backgroundColor = [UIColor grayColor];
//    buttonLoadHTMLString.clipsToBounds = YES;
//    buttonLoadHTMLString.layer.cornerRadius = 30;
    buttonLoadHTMLString.frame = CGRectMake(0, 20, 117, 60);
    [buttonLoadHTMLString addTarget:self action:@selector(testLoadHTMLString:) forControlEvents:UIControlEventTouchUpInside];
    [buttonBar addSubview:buttonLoadHTMLString];
    
    //添加LoadData按钮
    UIButton *buttonLoadData = [UIButton buttonWithType:UIButtonTypeSystem];
    [buttonLoadData setTitle:@"Load\nData" forState:UIControlStateNormal];
    buttonLoadData.titleLabel.numberOfLines = 2;
    buttonLoadData.frame = CGRectMake(137, 20, 67, 60);
    [buttonLoadData addTarget:self action:@selector(testLoadData:) forControlEvents:UIControlEventTouchUpInside];
    [buttonBar addSubview:buttonLoadData];

    //添加LoadRequest按钮
    UIButton *buttonLoadRequest = [UIButton buttonWithType:UIButtonTypeSystem];
    [buttonLoadRequest setTitle:@"Load\nRequest" forState:UIControlStateNormal];
    buttonLoadRequest.titleLabel.numberOfLines = 2;
    buttonLoadRequest.frame = CGRectMake(224, 20, 92, 60);
    [buttonLoadRequest addTarget:self action:@selector(testLoadRequest:) forControlEvents:UIControlEventTouchUpInside];
    [buttonBar addSubview:buttonLoadRequest];
    
    //添加WKWebView
    self.webView = [[WKWebView alloc] initWithFrame:CGRectMake(0, 130, screen.size.width, screen.size.height-150)];
//    self.webView.backgroundColor = [UIColor greenColor];
    [self.view addSubview:self.webView];
    
}

- (void)testLoadHTMLString:(id)sender{
    NSLog(@"%s", __func__);
    NSString *htmlPath = [[NSBundle mainBundle] pathForResource:@"index" ofType:@"html"];
    NSURL *bundleUrl = [NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]];
    NSError *error = nil;
    
    NSString *html = [[NSString alloc] initWithContentsOfFile:htmlPath encoding:NSUTF8StringEncoding error:&error];
    if (error == nil) {
        [self.webView loadHTMLString:html baseURL:bundleUrl];
    }else{
        NSLog(@"加载失败 error: %@", error.description);
    }
}
- (void)testLoadData:(id)sender{
    NSLog(@"%s", __func__);
    NSString *htmlPath = [[NSBundle mainBundle] pathForResource:@"index" ofType:@"html"];
    NSURL *bundleUrl = [NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]];
    NSData *htmlData = [[NSData alloc]  initWithContentsOfFile: htmlPath];
    
    [self.webView loadData:htmlData MIMEType:@"text/html" characterEncodingName:@"UTF-8" baseURL:bundleUrl];
}
- (void)testLoadRequest:(id)sender{
    NSLog(@"%s", __func__);
    NSURL * url = [NSURL URLWithString: @"https://guides.cocoapods.org"];
    NSURLRequest * request = [NSURLRequest requestWithURL:url];
    [self.webView loadRequest:request];
    self.webView.navigationDelegate = self;
     NSLog(@"%s", __func__);
    

}

PS:记得@interface ViewController ()

#pragma mark - WKNavigationDelegate
//开始加载时调用
- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation{
    NSLog(@"开始加载");
}

//内容开始返回时调用
- (void)webView:(WKWebView *)webView didCommitNavigation:(WKNavigation *)navigation{
    NSLog(@"内容开始返回");
}

//加载完成之后调用
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation{
    NSLog(@"加载完成");
}

//加载失败时调用
- (void)webView:(WKWebView *)webView didFailNavigation:(WKNavigation *)navigation withError:(NSError *)error{
    NSLog(@"加载失败 error: %@", error.description);
}

上述代码中使用的index.html,自己随便写个,也可以从网络上载一个网页。还是不过贴上一段简单的






网页内容

你可能感兴趣的:(ios Web视图之WKWebView)