前言:H5 漂亮妹子给了个H5链接,需要老衲这边接入,使用什么呢,app支持的是iOS8 以上,当然使用WKWebView啦,方便速度又快。为了显得高端一点,当然需要给一个加载网页的进度条,再加个H5内部页面跳转。
//导航栏设置一个h5返回按钮和pop按钮
UIImage *backImage = [[UIImage imageNamed:@"back"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
UIBarButtonItem *backItem = [[UIBarButtonItem alloc]initWithImage:backImage style:UIBarButtonItemStylePlain target:self action:@selector(personBack)];
UIBarButtonItem *closeItem = [[UIBarButtonItem alloc]initWithTitle:@"关闭" style:UIBarButtonItemStylePlain target:self action:@selector(personClose)];
[closeItem setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]} forState:UIControlStateNormal];
self.navigationItem.leftBarButtonItems = @[backItem,closeItem];
//WKWebView
_person_webView = [[WKWebView alloc]initWithFrame:CGRectMake(0, 0, LHQSCREEN_W, LHQSCREEN_H-LHQTopHeight-LHQXHeight)];
_person_webView.scrollView.showsVerticalScrollIndicator = NO;
_person_webView.scrollView.showsHorizontalScrollIndicator = NO;
_person_webView.navigationDelegate = self;//WKNavigationDelegate
[self.view addSubview:_person_webView];
//进度条
_progressView = [[UIProgressView alloc]initWithFrame:CGRectMake(0, 0, LHQSCREEN_W, 2)];
_progressView.trackTintColor = [UIColor whiteColor];
[self.view addSubview:_progressView];
//KVO监听estimatedProgress属性值变化
[_person_webView addObserver:self forKeyPath:@"estimatedProgress" options:NSKeyValueObservingOptionNew context:nil];
//链接请求
NSString *isLink = [NSString stringWithFormat:@"www.baidu.com"];
NSURLRequest *urlRequest = [[NSURLRequest alloc]initWithURL:[NSURL URLWithString:isLink]];
[_person_webView loadRequest:urlRequest];
返回按钮点击触发
#pragma mark -- WKNavigationDelegate
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler
{
DLog(@"1");
NSString *url = navigationAction.request.URL.absoluteString;
if(![url isEqualToString:link]) {
// 页面跳转
}
decisionHandler(WKNavigationActionPolicyAllow);//实现H5页面返回
}
#pragma mark -- kvo
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context
{
if ([keyPath isEqualToString:@"estimatedProgress"]) {
self.progressView.progress = self.person_webView.estimatedProgress;
// 加载完成
if (self.person_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;
}
}
}
//导航栏 返回和关闭按钮点击
#pragma mark -- back and close
-(void)personBack
{
[_person_webView goBack];
}
-(void)personClose
{
[self.navigationController popViewControllerAnimated:YES];
}
简单完成 ... 略略略
附:
1.今天遇到一个问题就是webKit 内嵌的H5 ,无法添加图片上传
问题在哪呢?
参考文章:https://www.cnblogs.com/jiajin/p/6077160.html
- (void)viewWillAppear:(BOOL)animated
{
NSURLRequest *urlRequest = [[NSURLRequest alloc]initWithURL:[NSURL URLWithString:isLink]];
[_person_webView loadRequest:urlRequest];
}
//不能把 load 放在 viewWillAppear方法中
应当放在ViewDIdLoad ,这样就没得问题了
- (void)viewDidLoad
{
NSURLRequest *urlRequest = [[NSURLRequest alloc]initWithURL:[NSURL URLWithString:isLink]];
[_person_webView loadRequest:urlRequest];
}