ios WKWebView混合开发案例

说明

加载本地的html页面。

  • loadHTMLString:baseURL: 设置主页的基本路径,通过一个html字符串加载主页数据
  • loadData:MIMEType:characterEncodingName:baseURL: 指定mime类型、 编码集、和nsdata对象加载一个主页数据,并设置主页文件的基本路径。

加载网络的页面 (注意需要增加网络权限。)

  • loadRequest : 使用详情见下面的demo。
    权限增加:
    ios WKWebView混合开发案例_第1张图片
运行效果

ios WKWebView混合开发案例_第2张图片

项目结果

ios WKWebView混合开发案例_第3张图片

示例代码
#import "ViewController.h"
#import 

@interface ViewController ()<WKNavigationDelegate>

@property(nonatomic, strong) WKWebView *webView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    CGRect screen = [[UIScreen mainScreen] bounds];
    
    // 按纽栏  按钮栏宽
    CGFloat buttonBarWidth = 316;
    UIView *buttonBar  = [[UIView alloc] initWithFrame:CGRectMake((screen.size.width - buttonBarWidth) / 2, 40, buttonBarWidth, 30)];
    
    [self.view addSubview: buttonBar];
    
    // 添加loadHtmlstring按钮
    UIButton *buttonLoadHtmlString = [UIButton buttonWithType:UIButtonTypeSystem];
    [buttonLoadHtmlString setTitle:@"loadHTMLString" forState:UIControlStateNormal];
    buttonLoadHtmlString.frame = CGRectMake(0, 0, 117, 30);
    //指定事件的处理方法
    [buttonLoadHtmlString addTarget:self action:@selector(testLiadHTMLString:) forControlEvents:UIControlEventTouchUpInside];
    //添加到buttonbarf视图
    [buttonBar addSubview:buttonLoadHtmlString];
    
    //2 添加loaddata按钮
    UIButton *buttonLoadData = [UIButton buttonWithType:UIButtonTypeSystem];
    [buttonLoadData setTitle:@"loadData" forState:UIControlStateNormal];
    buttonLoadData.frame = CGRectMake(137, 0, 67, 30);
    //指定事件处理方法
    [buttonLoadData addTarget:self action:@selector(testLoadData:) forControlEvents:UIControlEventTouchUpInside];
    [buttonBar addSubview: buttonLoadData];
    
    //3.添加loadRequest按钮
    UIButton *buttonLoadRequest = [UIButton buttonWithType:UIButtonTypeSystem];
    
    [buttonLoadRequest setTitle:@"loadRequest" forState:UIControlStateNormal];
    buttonLoadRequest.frame = CGRectMake(224, 0, 92, 30);
    [buttonLoadRequest addTarget:self action:@selector(testLoadRequest:) forControlEvents:UIControlEventTouchUpInside];
    
    [buttonBar addSubview:buttonLoadRequest];
    
    //添加 WKwebview
    self.webView = [[WKWebView alloc] initWithFrame:CGRectMake(0, 60, screen.size.width, screen.size.height - 60) ];
    [self.view addSubview: self.webView];
}

- (void) testLiadHTMLString: (id)sender{
    
    NSString *htmlPath = [[NSBundle mainBundle] pathForResource:@"index" ofType:@"html"];
    NSURL *boundUrl = [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:boundUrl];
    }
}

- (void) testLoadData: (id)sender{
    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{
    NSURL *url = [NSURL URLWithString:@"http://www.51work6.com"];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [self.webView loadRequest: request];
    self.webView.navigationDelegate = self;
}



//实现wknavigationDelegate委托协议
//l开始加载时调用
- (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 didFailProvisionalNavigation:(WKNavigation *)navigation withError:(NSError *)error {
    NSLog(@"加载失败");
}
@end

你可能感兴趣的:(IOS)