前言:H5 漂亮妹子給了個H5鏈接缴罗,需要老衲這邊接入,使用什么呢祭埂,app支持的是iOS8 以上面氓,當(dāng)然使用WKWebView啦,方便速度又快蛆橡。為了顯得高端一點舌界,當(dāng)然需要給一個加載網(wǎng)頁的進度條,再加個H5內(nèi)部頁面跳轉(zhuǎn)泰演。
//導(dǎo)航欄設(shè)置一個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:@"關(guān)閉" 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監(jiān)聽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];
返回按鈕點擊觸發(fā)
#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]) {
// 頁面跳轉(zhuǎn)
}
decisionHandler(WKNavigationActionPolicyAllow);//實現(xiàn)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;
}
}
}
//導(dǎo)航欄 返回和關(guān)閉按鈕點擊
#pragma mark -- back and close
-(void)personBack
{
[_person_webView goBack];
}
-(void)personClose
{
[self.navigationController popViewControllerAnimated:YES];
}
簡單完成 ... 略略略
附:
1.今天遇到一個問題就是webKit 內(nèi)嵌的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方法中
應(yīng)當(dāng)放在ViewDIdLoad 睦焕,這樣就沒得問題了
- (void)viewDidLoad
{
NSURLRequest *urlRequest = [[NSURLRequest alloc]initWithURL:[NSURL URLWithString:isLink]];
[_person_webView loadRequest:urlRequest];
}