//
|
// WEBViewController.m
|
// BuWanVideo2.0
|
//
|
// Created by apple on 16/9/18.
|
// Copyright © 2016年 com.yeshi.buwansheque.ios. All rights reserved.
|
//
|
|
#import "WEBViewController.h"
|
#import <WebKit/WebKit.h>
|
|
@interface WEBViewController ()<WKNavigationDelegate>{
|
UILabel *webtitle;//网页显示的标题
|
}
|
|
@property (strong, nonatomic) WKWebView *webView;
|
@property (strong, nonatomic) UILabel *titleView;
|
@property (strong, nonatomic) UILabel *urlView;
|
@property (strong, nonatomic) UIButton *backBt;
|
@property (strong, nonatomic) UIButton *reloadBt;
|
|
@end
|
|
@implementation WEBViewController
|
|
- (void)viewDidLoad{
|
[super viewDidLoad];
|
|
//做的假导航栏
|
_titleView=[[UILabel alloc] initWithFrame:CGRectMake(0, 0, KScreenW, 64)];
|
_titleView.backgroundColor=kGlobalMainColor;
|
_titleView.userInteractionEnabled=YES;
|
[self.view addSubview:_titleView];
|
//添加网页链接
|
_urlView=[[UILabel alloc] initWithFrame:CGRectMake(50, 34, KScreenW-100, 24)];
|
[_urlView setFont:[UIFont systemFontOfSize:12]];
|
[_urlView setTextColor:[UIColor whiteColor]];
|
_urlView.userInteractionEnabled=YES;
|
[self.view addSubview:_urlView];
|
|
//网页视图
|
_webView=[[WKWebView alloc] initWithFrame:CGRectMake(0, 64, KScreenW, KScreenH-64)];
|
_webView.contentMode=UIViewContentModeScaleAspectFill;
|
_webView.configuration.allowsInlineMediaPlayback=YES;
|
|
[_webView setNavigationDelegate:self];
|
[_webView setMultipleTouchEnabled:YES];
|
[_webView setAutoresizesSubviews:YES];
|
[_webView.scrollView setAlwaysBounceVertical:YES];
|
// [_webView setCustomUserAgent:@"Mozilla/5.0 (iPhone; CPU iPhone OS 12_3_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/12.1.1 Mobile/15E148 Safari/604.1"];
|
// 获取当前UserAgent, 并对其进行修改
|
[_webView evaluateJavaScript:@"navigator.userAgent" completionHandler:^(id userAgent, NSError * _Nullable error) {
|
if ([userAgent isKindOfClass:[NSString class]]) {
|
NSString *systemUserAgent =@"";
|
systemUserAgent = [NSString stringWithFormat:@"%@ %@", userAgent, @"Safari/604.1" ];
|
// 关键代码, 必须实现这个方法, 否则第一次打开UA还是原始值, 待第二次打开webview才是正确的UA;
|
[_webView setCustomUserAgent:systemUserAgent];
|
|
}
|
}];
|
|
|
// 这行代码可以是侧滑返回webView的上一级,而不是跟控制器(*指针对侧滑有效)
|
[_webView setAllowsBackForwardNavigationGestures:true];
|
[self.view addSubview:_webView];
|
|
//添加返回按钮
|
_backBt=[[UIButton alloc] initWithFrame:CGRectMake(8, 22, 32, 32)];
|
[_backBt setImage:[UIImage imageNamed:@"详情页面返回"] forState:UIControlStateNormal];
|
[_backBt addTarget:self action:@selector(backButtonPushed:) forControlEvents:UIControlEventTouchUpInside];
|
[self.view addSubview:_backBt];
|
|
//添加刷新按钮
|
_reloadBt=[[UIButton alloc] initWithFrame:CGRectMake(KScreenW-40, 22, 32, 32)];
|
[_reloadBt setImage:[UIImage imageNamed:@"网页刷新"] forState:UIControlStateNormal];
|
[_reloadBt addTarget:self action:@selector(reloadButtonPushed:) forControlEvents:UIControlEventTouchUpInside];
|
[self.view addSubview:_reloadBt];
|
|
|
|
//监听标题
|
[_webView addObserver:self forKeyPath:@"title" options:NSKeyValueObservingOptionNew context:NULL];
|
[_webView addObserver:self forKeyPath:@"estimatedProgress" options:NSKeyValueObservingOptionNew context:NULL];
|
|
|
|
|
[self loadwebViews];
|
}
|
|
- (void)viewWillAppear:(BOOL)animated{
|
[super viewWillAppear:animated];
|
//保持屏幕常亮
|
[[UIApplication sharedApplication] setIdleTimerDisabled:YES];
|
}
|
|
-(void)viewWillDisappear:(BOOL)animated{
|
[super viewWillDisappear:animated];
|
//强制装换屏幕
|
NSNumber *orientationUnknown = [NSNumber numberWithInt:UIInterfaceOrientationUnknown];
|
[[UIDevice currentDevice] setValue:orientationUnknown forKey:@"orientation"];
|
|
NSNumber *PortraitorientationUnknown = [NSNumber numberWithInt:UIInterfaceOrientationPortrait];
|
[[UIDevice currentDevice] setValue:PortraitorientationUnknown forKey:@"orientation"];
|
|
//取消屏幕常亮
|
[[UIApplication sharedApplication] setIdleTimerDisabled:NO];
|
|
//状态栏重现
|
[[UIApplication sharedApplication] setStatusBarHidden:NO];
|
}
|
|
- (void)backButtonPushed:(id)sender{
|
if ([_webView canGoBack]&&![_webView.backForwardList.backList[0].title isEqualToString:webtitle.text]) {
|
[_webView goToBackForwardListItem:_webView.backForwardList.backList[0]];
|
[SVProgressHUD dismiss];
|
}else{
|
[self dismissViewControllerAnimated:YES completion:^{
|
[SVProgressHUD dismiss];
|
}];
|
}
|
}
|
|
- (void)reloadButtonPushed:(id)sender{
|
[SVProgressHUD dismiss];
|
[_webView reload];
|
}
|
|
-(void)loadwebViews{
|
NSURLRequest *req = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:_url]];
|
[_webView loadRequest:req];
|
_urlView.text=_url;
|
}
|
|
|
|
#pragma mark -WKNavigationDelegate
|
// 页面开始加载时调用
|
- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation{
|
|
}
|
// 当内容开始返回时调用
|
- (void)webView:(WKWebView *)webView didCommitNavigation:(WKNavigation *)navigation{
|
|
}
|
// 页面加载完成之后调用
|
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation{
|
|
}
|
// 页面加载失败时调用
|
- (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(WKNavigation *)navigation{
|
|
}
|
|
-(void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler{
|
WKNavigationActionPolicy policy = WKNavigationActionPolicyAllow;
|
@try {
|
NSString *url;
|
url = navigationAction.request.URL.absoluteString;
|
NSLog(@"链接:%@",url);
|
|
|
if([url hasPrefix:@"iqiyi"]){
|
if ([[UIApplication sharedApplication]
|
canOpenURL:[NSURL URLWithString:url]]){
|
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];
|
}
|
}else{
|
|
|
/*
|
如果是域名,判断itunes的host链接 ;
|
如果是scheme跳转,就判断是否是 App Store 的 scheme
|
*/
|
|
if([[navigationAction.request.URL host] isEqualToString:@"itunes.apple.com"] || [[navigationAction.request.URL scheme] isEqualToString:@"itms-apps"]) {
|
if ( [[UIApplication sharedApplication] openURL:navigationAction.request.URL]) {
|
policy = WKNavigationActionPolicyCancel;
|
NSLog(@"已跳转至App Store");
|
}
|
}
|
|
}
|
} @catch (NSException *exception) {
|
|
} @finally {
|
|
}
|
decisionHandler(policy);
|
|
}
|
|
|
-(BOOL)shouldAutorotate{
|
return YES;
|
}
|
//支持的方向 因为界面A我们只需要支持竖屏
|
- (UIInterfaceOrientationMask)supportedInterfaceOrientations{
|
_titleView.frame=CGRectMake(0, 0, KScreenWp, 64);
|
_urlView.frame=CGRectMake(50, 34, KScreenWp-100, 24);
|
_webView.frame=CGRectMake(0, 64, KScreenWp, KScreenHp-64);
|
_backBt.frame=CGRectMake(8, 22, 32, 32);
|
_reloadBt.frame=CGRectMake(KScreenWp-40, 22, 32, 32);
|
return _orMake;
|
}
|
|
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
|
if ([keyPath isEqualToString:@"title"]) {
|
if (object == _webView) {
|
NSString *title = _webView.title;
|
if(webtitle==nil){
|
webtitle=[[UILabel alloc] initWithFrame:CGRectMake(50, 15, KScreenW-100, 32)];
|
[webtitle setFont:[UIFont systemFontOfSize:14]];
|
[webtitle setTextColor:[UIColor whiteColor]];
|
[webtitle setTextAlignment:NSTextAlignmentLeft];
|
[_titleView addSubview:webtitle];
|
}
|
webtitle.text =title;
|
} else {
|
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
|
}
|
}else if ([keyPath isEqualToString:@"estimatedProgress"]) {
|
if (object == _webView) {
|
// double progress = _webView.estimatedProgress;
|
// if(progress!=1){
|
// [SVProgressHUD showProgress:progress status:@"加载中"];
|
// }else{
|
// [SVProgressHUD dismiss];
|
// }
|
} else {
|
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
|
}
|
}
|
}
|
|
-(void)dealloc{
|
[_webView removeObserver:self forKeyPath:@"estimatedProgress"];
|
[_webView removeObserver:self forKeyPath:@"title"];
|
}
|
|
@end
|