Commit 42064c77258d34bd3d22cfb2ffc73e6606935313

Authored by zhangxiaowen
1 parent 98ea20e2

no message

CNLiveUserAgreementManager/Classes/CNLiveUserAgreementWebController.h
@@ -11,6 +11,16 @@ @@ -11,6 +11,16 @@
11 NS_ASSUME_NONNULL_BEGIN 11 NS_ASSUME_NONNULL_BEGIN
12 12
13 @interface CNLiveUserAgreementWebController : UIViewController 13 @interface CNLiveUserAgreementWebController : UIViewController
  14 +
  15 +/**
  16 + * 对象方法 初始化方法
  17 + * @param url url
  18 + * @param name 标题
  19 + * 注意:默认没有关闭按钮
  20 + */
  21 +- (instancetype)initWithUrl:(NSString *)url name:(nullable NSString *)name;
  22 +
  23 +
14 /** 24 /**
15 * 对象方法 初始化方法 25 * 对象方法 初始化方法
16 * @param url url 26 * @param url url
CNLiveUserAgreementManager/Classes/CNLiveUserAgreementWebController.m
@@ -30,6 +30,10 @@ @@ -30,6 +30,10 @@
30 static const NSInteger margin = 15; 30 static const NSInteger margin = 15;
31 31
32 #pragma mark - 初始化方法 32 #pragma mark - 初始化方法
  33 +- (instancetype)initWithUrl:(NSString *)url name:(nullable NSString *)name{
  34 + return [self initWithUrl:url name:name showCloseBtn:NO];
  35 +}
  36 +
33 - (instancetype)initWithUrl:(NSString *)url name:(nullable NSString *)name showCloseBtn:(BOOL)isShow { 37 - (instancetype)initWithUrl:(NSString *)url name:(nullable NSString *)name showCloseBtn:(BOOL)isShow {
34 self = [super init]; 38 self = [super init];
35 if (self) { 39 if (self) {
@@ -43,27 +47,23 @@ static const NSInteger margin = 15; @@ -43,27 +47,23 @@ static const NSInteger margin = 15;
43 - (void)setName:(NSString *)name{ 47 - (void)setName:(NSString *)name{
44 _name = name; 48 _name = name;
45 _titleLabel.text = name; 49 _titleLabel.text = name;
46 -  
47 } 50 }
48 51
49 #pragma mark - 加载数据 52 #pragma mark - 加载数据
50 - (void)loadUrl { 53 - (void)loadUrl {
51 NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:self.url] cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:15.0]; 54 NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:self.url] cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:15.0];
52 [self.webView loadRequest:theRequest]; 55 [self.webView loadRequest:theRequest];
53 -  
54 } 56 }
55 57
56 #pragma mark - 生命周期 58 #pragma mark - 生命周期
57 - (void)viewWillAppear:(BOOL)animated { 59 - (void)viewWillAppear:(BOOL)animated {
58 [super viewWillAppear:animated]; 60 [super viewWillAppear:animated];
59 self.navigationController.navigationBarHidden = YES; 61 self.navigationController.navigationBarHidden = YES;
60 -  
61 } 62 }
62 63
63 - (void)viewWillDisappear:(BOOL)animated { 64 - (void)viewWillDisappear:(BOOL)animated {
64 [super viewWillDisappear:animated]; 65 [super viewWillDisappear:animated];
65 self.navigationController.navigationBarHidden = NO; 66 self.navigationController.navigationBarHidden = NO;
66 -  
67 } 67 }
68 68
69 - (void)viewDidLoad { 69 - (void)viewDidLoad {
@@ -76,7 +76,7 @@ static const NSInteger margin = 15; @@ -76,7 +76,7 @@ static const NSInteger margin = 15;
76 self.automaticallyAdjustsScrollViewInsets = NO; 76 self.automaticallyAdjustsScrollViewInsets = NO;
77 } 77 }
78 [self setupNavigationBar]; 78 [self setupNavigationBar];
79 - [self setupUI]; 79 + [self createSubViews];
80 [self loadUrl]; 80 [self loadUrl];
81 81
82 } 82 }
@@ -99,14 +99,14 @@ static const NSInteger margin = 15; @@ -99,14 +99,14 @@ static const NSInteger margin = 15;
99 99
100 #pragma mark - 创建导航条 100 #pragma mark - 创建导航条
101 - (void)setupNavigationBar { 101 - (void)setupNavigationBar {
102 - UIView *navView = [[UIView alloc] initWithFrame:CGRectMake(0, [UIApplication sharedApplication].statusBarFrame.size.height, [UIScreen mainScreen].bounds.size.width, 44)]; 102 + UIView *navView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 44+[UIApplication sharedApplication].statusBarFrame.size.height)];
103 navView.backgroundColor = [UIColor clearColor]; 103 navView.backgroundColor = [UIColor clearColor];
104 [self.view bringSubviewToFront:navView]; 104 [self.view bringSubviewToFront:navView];
105 [self.view addSubview:navView]; 105 [self.view addSubview:navView];
106 106
107 UIButton *leftBtn = [UIButton buttonWithType:UIButtonTypeCustom]; 107 UIButton *leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
108 leftBtn.adjustsImageWhenHighlighted = NO; 108 leftBtn.adjustsImageWhenHighlighted = NO;
109 - leftBtn.frame = CGRectMake(margin, 0, 44, 44); 109 + leftBtn.frame = CGRectMake(margin, [UIApplication sharedApplication].statusBarFrame.size.height, 44, 44);
110 UIImage *leftImage = [self xw_getImageName:@"web_black_back" bundleName:@"CNLiveUserAgreementManager" targetClass:[CNLiveUserAgreementWebController class]]; 110 UIImage *leftImage = [self xw_getImageName:@"web_black_back" bundleName:@"CNLiveUserAgreementManager" targetClass:[CNLiveUserAgreementWebController class]];
111 [leftBtn setImage:[leftImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal]; 111 [leftBtn setImage:[leftImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
112 [leftBtn addTarget:self action:@selector(goBack) forControlEvents:UIControlEventTouchUpInside]; 112 [leftBtn addTarget:self action:@selector(goBack) forControlEvents:UIControlEventTouchUpInside];
@@ -115,7 +115,7 @@ static const NSInteger margin = 15; @@ -115,7 +115,7 @@ static const NSInteger margin = 15;
115 115
116 UIButton *closeBtn = [UIButton buttonWithType:UIButtonTypeCustom]; 116 UIButton *closeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
117 closeBtn.adjustsImageWhenHighlighted = NO; 117 closeBtn.adjustsImageWhenHighlighted = NO;
118 - closeBtn.frame = CGRectMake(44+margin, 0, 44, 44); 118 + closeBtn.frame = CGRectMake(44+margin, [UIApplication sharedApplication].statusBarFrame.size.height, 44, 44);
119 UIImage *closeImage = [self xw_getImageName:@"web_black_close" bundleName:@"CNLiveUserAgreementManager" targetClass:[CNLiveUserAgreementWebController class]]; 119 UIImage *closeImage = [self xw_getImageName:@"web_black_close" bundleName:@"CNLiveUserAgreementManager" targetClass:[CNLiveUserAgreementWebController class]];
120 [closeBtn setImage:[closeImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal]; 120 [closeBtn setImage:[closeImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
121 [closeBtn addTarget:self action:@selector(closeClick) forControlEvents:UIControlEventTouchUpInside]; 121 [closeBtn addTarget:self action:@selector(closeClick) forControlEvents:UIControlEventTouchUpInside];
@@ -123,7 +123,7 @@ static const NSInteger margin = 15; @@ -123,7 +123,7 @@ static const NSInteger margin = 15;
123 closeBtn.hidden = !self.isShow; 123 closeBtn.hidden = !self.isShow;
124 [navView addSubview:closeBtn]; 124 [navView addSubview:closeBtn];
125 125
126 - UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(([[UIScreen mainScreen] bounds].size.width - 200)/2.0, 0, 200, 44)]; 126 + UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(([[UIScreen mainScreen] bounds].size.width - 200)/2.0, [UIApplication sharedApplication].statusBarFrame.size.height, 200, 44)];
127 titleLabel.font = [UIFont boldSystemFontOfSize:17]; 127 titleLabel.font = [UIFont boldSystemFontOfSize:17];
128 titleLabel.textColor = [UIColor blackColor]; 128 titleLabel.textColor = [UIColor blackColor];
129 titleLabel.text = self.name; 129 titleLabel.text = self.name;
@@ -133,25 +133,8 @@ static const NSInteger margin = 15; @@ -133,25 +133,8 @@ static const NSInteger margin = 15;
133 133
134 } 134 }
135 135
136 -- (void)setupUI {  
137 - WKWebViewConfiguration * configuration = [[WKWebViewConfiguration alloc] init];  
138 - configuration.allowsInlineMediaPlayback = YES;  
139 - WKPreferences *preferences = [WKPreferences new];  
140 - preferences.javaScriptCanOpenWindowsAutomatically = YES;  
141 - configuration.preferences = preferences;  
142 - self.webView = [[WKWebView alloc] initWithFrame:CGRectMake(0, [UIApplication sharedApplication].statusBarFrame.size.height+44, [[UIScreen mainScreen] bounds].size.width, [[UIScreen mainScreen] bounds].size.height-([UIApplication sharedApplication].statusBarFrame.size.height+44)) configuration:configuration];  
143 - _webView.UIDelegate = self;  
144 - _webView.navigationDelegate = self;  
145 - _webView.contentMode = UIViewContentModeScaleAspectFit;  
146 - _webView.scrollView.showsHorizontalScrollIndicator = NO;  
147 - _webView.scrollView.showsVerticalScrollIndicator = NO;  
148 - _webView.allowsBackForwardNavigationGestures = YES;  
149 - _webView.allowsLinkPreview = NO; 136 +- (void)createSubViews {
150 [self.view addSubview:self.webView]; 137 [self.view addSubview:self.webView];
151 -  
152 - self.processView = [[UIProgressView alloc] initWithFrame:CGRectMake(0, ([UIApplication sharedApplication].statusBarFrame.size.height+44)+0.5, [[UIScreen mainScreen] bounds].size.width, 1)];  
153 - _processView.progressTintColor = [UIColor colorWithRed:19/255.0 green:204/255.0 blue:12/255.0 alpha:1.0];  
154 - _processView.trackTintColor = [UIColor whiteColor];  
155 [self.view addSubview:self.processView]; 138 [self.view addSubview:self.processView];
156 139
157 [self.webView addObserver:self forKeyPath:@"estimatedProgress" options:NSKeyValueObservingOptionNew context:nil]; 140 [self.webView addObserver:self forKeyPath:@"estimatedProgress" options:NSKeyValueObservingOptionNew context:nil];
@@ -268,6 +251,35 @@ static const NSInteger margin = 15; @@ -268,6 +251,35 @@ static const NSInteger margin = 15;
268 } 251 }
269 } 252 }
270 #pragma mark - 懒加载 253 #pragma mark - 懒加载
  254 +- (WKWebView *)webView{
  255 + if(!_webView){
  256 + WKWebViewConfiguration * configuration = [[WKWebViewConfiguration alloc] init];
  257 + configuration.allowsInlineMediaPlayback = YES;
  258 + WKPreferences *preferences = [WKPreferences new];
  259 + preferences.javaScriptCanOpenWindowsAutomatically = YES;
  260 + configuration.preferences = preferences;
  261 + _webView = [[WKWebView alloc] initWithFrame:CGRectMake(0, [UIApplication sharedApplication].statusBarFrame.size.height+44, [[UIScreen mainScreen] bounds].size.width, [[UIScreen mainScreen] bounds].size.height-([UIApplication sharedApplication].statusBarFrame.size.height+44)) configuration:configuration];
  262 + _webView.UIDelegate = self;
  263 + _webView.navigationDelegate = self;
  264 + _webView.contentMode = UIViewContentModeScaleAspectFit;
  265 + _webView.scrollView.showsHorizontalScrollIndicator = NO;
  266 + _webView.scrollView.showsVerticalScrollIndicator = NO;
  267 + _webView.allowsBackForwardNavigationGestures = YES;
  268 + _webView.allowsLinkPreview = NO;
  269 +
  270 + }
  271 + return _webView;
  272 +}
  273 +
  274 +- (UIProgressView *)processView{
  275 + if(!_processView){
  276 + _processView = [[UIProgressView alloc] initWithFrame:CGRectMake(0, ([UIApplication sharedApplication].statusBarFrame.size.height+44)+0.5, [[UIScreen mainScreen] bounds].size.width, 1)];
  277 + _processView.progressTintColor = [UIColor colorWithRed:19/255.0 green:204/255.0 blue:12/255.0 alpha:1.0];
  278 + _processView.trackTintColor = [UIColor whiteColor];
  279 + }
  280 + return _processView;
  281 +}
  282 +
271 #pragma mark - 私有方法 283 #pragma mark - 私有方法
272 // 清除缓存 284 // 清除缓存
273 - (void)clearWKCache { 285 - (void)clearWKCache {
Example/CNLiveUserAgreementManager/CNLiveViewController.m
@@ -7,9 +7,11 @@ @@ -7,9 +7,11 @@
7 // 7 //
8 8
9 #import "CNLiveViewController.h" 9 #import "CNLiveViewController.h"
10 -#import "CNLiveUserAgreementManager.h"  
11 #import "CNLiveEnvironment.h" 10 #import "CNLiveEnvironment.h"
12 11
  12 +#import "CNLiveUserAgreementManager.h"
  13 +#import "CNLiveUserAgreementWebController.h"
  14 +
13 @interface CNLiveViewController () 15 @interface CNLiveViewController ()
14 16
15 @end 17 @end
@@ -33,8 +35,15 @@ @@ -33,8 +35,15 @@
33 35
34 } 36 }
35 - (void)testButtonClicked { 37 - (void)testButtonClicked {
36 - [CNLiveUserAgreementManager jumpToAgreementH5WithType:CNLiveUserAgreementCancellation]; 38 +// [CNLiveUserAgreementManager jumpToAgreementH5WithType:CNLiveUserAgreementCancellation];
37 // [CNLiveUserAgreementManager jumpToAgreementH5WithString:@"yhxy"]; 39 // [CNLiveUserAgreementManager jumpToAgreementH5WithString:@"yhxy"];
  40 + [CNLiveUserAgreementManager getAgreementH5WithType:CNLiveUserAgreementCancellation success:^(NSString * _Nonnull h5, NSString * _Nonnull title, BOOL isShow) {
  41 + CNLiveUserAgreementWebController *vc = [[CNLiveUserAgreementWebController alloc] initWithUrl:h5 name:title showCloseBtn:NO];
  42 + [self.navigationController pushViewController:vc animated:YES];
  43 +
  44 + } failure:^(NSError * _Nonnull error) {
  45 +
  46 + }];
38 47
39 } 48 }
40 49