ios WKWebView混合开发案例

说明

加载本地的html页面。

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

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

  • loadRequest : 使用详情见下面的demo。
    权限增加:
    在这里插入图片描述
运行效果

在这里插入图片描述

项目结果

在这里插入图片描述

示例代码
#import "ViewController.h"
#import <WebKit/WebKit.h>

@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

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ITzhongzi

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值