Commit 1400df32511ca635f5d8e5648f6906790e97d707
1 parent
c31a1d34
no message
Showing
9 changed files
with
419 additions
and
46 deletions
CNLiveUserAgreementManager.podspec
| @@ -32,9 +32,9 @@ TODO: Add long description of the pod here. | @@ -32,9 +32,9 @@ TODO: Add long description of the pod here. | ||
| 32 | 32 | ||
| 33 | s.source_files = 'CNLiveUserAgreementManager/Classes/**/*' | 33 | s.source_files = 'CNLiveUserAgreementManager/Classes/**/*' |
| 34 | 34 | ||
| 35 | - # s.resource_bundles = { | ||
| 36 | - # 'CNLiveUserAgreementManager' => ['CNLiveUserAgreementManager/Assets/*.png'] | ||
| 37 | - # } | 35 | + s.resource_bundles = { |
| 36 | + 'CNLiveAgreementManager' => ['CNLiveUserAgreementManager/Assets/CNLiveAgreementManager.bundle'] | ||
| 37 | + } | ||
| 38 | 38 | ||
| 39 | s.public_header_files = 'CNLiveUserAgreementManager/Classes/**/*.h' | 39 | s.public_header_files = 'CNLiveUserAgreementManager/Classes/**/*.h' |
| 40 | # s.frameworks = 'UIKit', 'MapKit' | 40 | # s.frameworks = 'UIKit', 'MapKit' |
CNLiveUserAgreementManager/Assets/CNLiveAgreementManager.bundle/web_black_back@2x.png
0 → 100644
628 Bytes
CNLiveUserAgreementManager/Assets/CNLiveAgreementManager.bundle/web_black_back@3x.png
0 → 100644
1.15 KB
CNLiveUserAgreementManager/Classes/CNUserAgreementManager.h renamed to CNLiveUserAgreementManager/Classes/CNLiveAgreementManager.h
| 1 | // | 1 | // |
| 2 | -// CNUserAgreementManager.h | 2 | +// CNLiveAgreementManager.h |
| 3 | // Test | 3 | // Test |
| 4 | // | 4 | // |
| 5 | // Created by CNLive-zxw on 2019/4/13. | 5 | // Created by CNLive-zxw on 2019/4/13. |
| @@ -28,13 +28,40 @@ typedef enum { | @@ -28,13 +28,40 @@ typedef enum { | ||
| 28 | 28 | ||
| 29 | } CNGetAgreementH5Type; | 29 | } CNGetAgreementH5Type; |
| 30 | 30 | ||
| 31 | -typedef void(^SuccessBlock)(NSString *h5,NSString *title); | 31 | +typedef void(^SuccessBlock)(NSString *h5, NSString *title); |
| 32 | typedef void(^FailureBlock)(NSError *error); | 32 | typedef void(^FailureBlock)(NSError *error); |
| 33 | 33 | ||
| 34 | -@interface CNUserAgreementManager : NSObject | 34 | +@interface CNLiveAgreementManager : NSObject |
| 35 | + | ||
| 36 | ++ (instancetype)manager; | ||
| 37 | + | ||
| 38 | +/** | ||
| 39 | + * 请求协议并跳转 | ||
| 40 | + * @param string 类型字符串 | ||
| 41 | + */ | ||
| 42 | ++ (void)jumpToAgreementH5WithString:(NSString *)string; | ||
| 43 | + | ||
| 44 | +/** | ||
| 45 | + * 请求协议并跳转 | ||
| 46 | + * @param type 类型 | ||
| 47 | + */ | ||
| 48 | ++ (void)jumpToAgreementH5WithType:(CNGetAgreementH5Type)type; | ||
| 49 | + | ||
| 50 | +/** | ||
| 51 | + * 类方法 请求协议 | ||
| 52 | + * @param type 类型 | ||
| 53 | + * @param success 请求成功回调 | ||
| 54 | + * @param failure 请求失败回调 | ||
| 55 | + */ | ||
| 35 | + (void)getAgreementH5WithType:(CNGetAgreementH5Type)type success:(SuccessBlock)success failure:(FailureBlock)failure; | 56 | + (void)getAgreementH5WithType:(CNGetAgreementH5Type)type success:(SuccessBlock)success failure:(FailureBlock)failure; |
| 36 | 57 | ||
| 37 | -+ (void)jumpToAgreementWebWithType:(CNGetAgreementH5Type)type; | 58 | +/** |
| 59 | + * 对象方法 请求协议 | ||
| 60 | + * @param type 类型 | ||
| 61 | + * @param success 请求成功回调 | ||
| 62 | + * @param failure 请求失败回调 | ||
| 63 | + */ | ||
| 64 | +- (void)getAgreementH5WithType:(CNGetAgreementH5Type)type success:(SuccessBlock)success failure:(FailureBlock)failure; | ||
| 38 | 65 | ||
| 39 | @end | 66 | @end |
| 40 | 67 |
CNLiveUserAgreementManager/Classes/CNUserAgreementManager.m renamed to CNLiveUserAgreementManager/Classes/CNLiveAgreementManager.m
| 1 | // | 1 | // |
| 2 | -// CNUserAgreementManager.m | 2 | +// CNLiveAgreementManager.m |
| 3 | // Test | 3 | // Test |
| 4 | // | 4 | // |
| 5 | // Created by CNLive-zxw on 2019/4/13. | 5 | // Created by CNLive-zxw on 2019/4/13. |
| 6 | // Copyright © 2019 CNLive. All rights reserved. | 6 | // Copyright © 2019 CNLive. All rights reserved. |
| 7 | // | 7 | // |
| 8 | 8 | ||
| 9 | -#import "CNUserAgreementManager.h" | 9 | +#import "CNLiveAgreementManager.h" |
| 10 | #import <CNLiveRequestBastKit/CNLiveNetworking.h> | 10 | #import <CNLiveRequestBastKit/CNLiveNetworking.h> |
| 11 | #import "CNLiveEnvironment.h" | 11 | #import "CNLiveEnvironment.h" |
| 12 | +#import "CNLiveAgreementWebController.h" | ||
| 13 | + | ||
| 14 | +@interface CNLiveAgreementManager() | ||
| 12 | 15 | ||
| 13 | -@interface CNUserAgreementManager() | ||
| 14 | 16 | ||
| 15 | @end | 17 | @end |
| 16 | 18 | ||
| 17 | -@implementation CNUserAgreementManager | ||
| 18 | -static CNUserAgreementManager *_instance = nil; | 19 | +@implementation CNLiveAgreementManager |
| 19 | 20 | ||
| 20 | -/* 单例控制器 */ | ||
| 21 | -+ (instancetype)shared { | ||
| 22 | - return [[self alloc] init]; | ||
| 23 | -} | 21 | +static CNLiveAgreementManager *_instance = nil; |
| 24 | 22 | ||
| 25 | -- (instancetype)init { | ||
| 26 | - static dispatch_once_t onceToken; | ||
| 27 | - dispatch_once(&onceToken, ^{ | ||
| 28 | - _instance = [super init]; | ||
| 29 | - | 23 | +/** |
| 24 | + * @abstract 单例初始化 | ||
| 25 | + */ | ||
| 26 | ++ (instancetype)manager{ | ||
| 27 | + static dispatch_once_t once; | ||
| 28 | + dispatch_once(&once, ^{ | ||
| 29 | + _instance = [[CNLiveAgreementManager alloc]init]; | ||
| 30 | }); | 30 | }); |
| 31 | return _instance; | 31 | return _instance; |
| 32 | + | ||
| 32 | } | 33 | } |
| 33 | 34 | ||
| 34 | -+ (instancetype)allocWithZone:(struct _NSZone *)zone { | 35 | ++ (id)allocWithZone:(struct _NSZone *)zone{ |
| 35 | static dispatch_once_t onceToken; | 36 | static dispatch_once_t onceToken; |
| 36 | dispatch_once(&onceToken, ^{ | 37 | dispatch_once(&onceToken, ^{ |
| 37 | _instance = [super allocWithZone:zone]; | 38 | _instance = [super allocWithZone:zone]; |
| 38 | - | ||
| 39 | }); | 39 | }); |
| 40 | return _instance; | 40 | return _instance; |
| 41 | } | 41 | } |
| 42 | 42 | ||
| 43 | -- (id)copyWithZone:(NSZone *)zone { | 43 | +- (nonnull id)copyWithZone:(nullable NSZone *)zone { |
| 44 | return _instance; | 44 | return _instance; |
| 45 | } | 45 | } |
| 46 | 46 | ||
| 47 | -- (id)mutableCopyWithZone:(NSZone *)zone { | 47 | +- (nonnull id)mutableCopyWithZone:(nullable NSZone *)zone { |
| 48 | return _instance; | 48 | return _instance; |
| 49 | } | 49 | } |
| 50 | 50 | ||
| @@ -228,7 +228,7 @@ static CNUserAgreementManager *_instance = nil; | @@ -228,7 +228,7 @@ static CNUserAgreementManager *_instance = nil; | ||
| 228 | } | 228 | } |
| 229 | 229 | ||
| 230 | + (void)getAgreementH5WithType:(CNGetAgreementH5Type)type success:(SuccessBlock)success failure:(FailureBlock)failure { | 230 | + (void)getAgreementH5WithType:(CNGetAgreementH5Type)type success:(SuccessBlock)success failure:(FailureBlock)failure { |
| 231 | - [[CNUserAgreementManager shared] getAgreementH5WithType:type success:^(NSString * _Nonnull h5, NSString * _Nonnull title) { | 231 | + [[CNLiveAgreementManager manager] getAgreementH5WithType:type success:^(NSString * _Nonnull h5, NSString * _Nonnull title) { |
| 232 | success(h5, title); | 232 | success(h5, title); |
| 233 | 233 | ||
| 234 | } failure:^(NSError * _Nonnull error) { | 234 | } failure:^(NSError * _Nonnull error) { |
| @@ -238,23 +238,62 @@ static CNUserAgreementManager *_instance = nil; | @@ -238,23 +238,62 @@ static CNUserAgreementManager *_instance = nil; | ||
| 238 | 238 | ||
| 239 | } | 239 | } |
| 240 | 240 | ||
| 241 | - | ||
| 242 | -+ (void)jumpToAgreementWebWithType:(CNGetAgreementH5Type)type{ | 241 | ++ (void)jumpToAgreementH5WithType:(CNGetAgreementH5Type)type{ |
| 243 | 242 | ||
| 244 | - __weak typeof(self) weakSelf = self; | ||
| 245 | - [[CNUserAgreementManager shared] getAgreementH5WithType:type success:^(NSString * _Nonnull h5, NSString * _Nonnull title) { | ||
| 246 | - __strong typeof(self) strongSelf = weakSelf; | ||
| 247 | - | 243 | + [CNLiveAgreementManager getAgreementH5WithType:type success:^(NSString *h5, NSString *title) { |
| 248 | 244 | ||
| 249 | - } failure:^(NSError * _Nonnull error) { | ||
| 250 | - __strong typeof(self) strongSelf = weakSelf; | 245 | + [self pushToWebControllerUrl:h5 name:title]; |
| 246 | + | ||
| 247 | + } failure:^(NSError *error) { | ||
| 248 | + | ||
| 249 | + }]; | ||
| 250 | + | ||
| 251 | +} | ||
| 251 | 252 | ||
| 252 | - if(TestEnvironment){ | ||
| 253 | - NSLog(@"获取协议请求失败"); | 253 | ++ (void)jumpToAgreementH5WithString:(NSString *)string { |
| 254 | + [CNLiveNetworking setAllowRequestDefaultArgument:NO]; | ||
| 255 | + [CNLiveNetworking requestNetworkWithMethod:CNLiveRequestMethodGET URLString:CNGetAgreementH5Url Param:@{} CacheType:CNLiveNetworkCacheTypeNetworkOnly CompletionBlock:^(NSURLSessionTask *requestTask, id responseObject, NSError *error) { | ||
| 256 | + if (!error) { | ||
| 257 | + if ([responseObject isKindOfClass:[NSArray class]]) { | ||
| 258 | + for (NSDictionary *dic in responseObject) { | ||
| 259 | + if ([[NSString stringWithFormat:@"%@",dic[@"type"]] isEqualToString:string]) { | ||
| 260 | + [self pushToWebControllerUrl:dic[@"detailUrl"] name:dic[@"title"]]; | ||
| 261 | + break; | ||
| 262 | + } | ||
| 263 | + | ||
| 264 | + } | ||
| 265 | + | ||
| 266 | + } | ||
| 267 | + | ||
| 254 | } | 268 | } |
| 255 | - | ||
| 256 | }]; | 269 | }]; |
| 257 | 270 | ||
| 258 | } | 271 | } |
| 259 | 272 | ||
| 273 | +#pragma mark - 跳转控制器 | ||
| 274 | ++ (void)pushToWebControllerUrl:(NSString *)url name:(NSString *)name { | ||
| 275 | + UIViewController *root = [UIApplication sharedApplication].delegate.window.rootViewController; | ||
| 276 | + | ||
| 277 | + //跟控制器是Navigation | ||
| 278 | + if ([root isKindOfClass:[UINavigationController class]]){ | ||
| 279 | + UINavigationController *nav = (UINavigationController *)root; | ||
| 280 | + CNLiveAgreementWebController *vc = [[CNLiveAgreementWebController alloc] initWithUrl:url name:name]; | ||
| 281 | + [nav pushViewController:vc animated:YES]; | ||
| 282 | + | ||
| 283 | + } | ||
| 284 | + //跟控制器是TabBar | ||
| 285 | + else if ([root isKindOfClass:[UITabBarController class]]){ | ||
| 286 | + UITabBarController *tab = (UITabBarController *)root; | ||
| 287 | + UINavigationController *nav = tab.selectedViewController; | ||
| 288 | + CNLiveAgreementWebController *vc = [[CNLiveAgreementWebController alloc] initWithUrl:url name:name]; | ||
| 289 | + [nav pushViewController:vc animated:YES]; | ||
| 290 | + | ||
| 291 | + }else{ | ||
| 292 | + CNLiveAgreementWebController *vc = [[CNLiveAgreementWebController alloc] initWithUrl:url name:name]; | ||
| 293 | + [root presentViewController:vc animated:YES completion:nil]; | ||
| 294 | + | ||
| 295 | + } | ||
| 296 | + | ||
| 297 | +} | ||
| 298 | + | ||
| 260 | @end | 299 | @end |
CNLiveUserAgreementManager/Classes/CNLiveAgreementWebController.h
0 → 100644
| 1 | +// | ||
| 2 | +// CNLiveAgreementWebController.h | ||
| 3 | +// CNLiveServices_Example | ||
| 4 | +// | ||
| 5 | +// Created by CNLive-zxw on 2019/7/30. | ||
| 6 | +// Copyright © 2019 153993236@qq.com. All rights reserved. | ||
| 7 | +// | ||
| 8 | + | ||
| 9 | +#import <UIKit/UIKit.h> | ||
| 10 | + | ||
| 11 | +NS_ASSUME_NONNULL_BEGIN | ||
| 12 | + | ||
| 13 | +@interface CNLiveAgreementWebController : UIViewController | ||
| 14 | +- (instancetype)initWithUrl:(NSString *)url name:(NSString *)name; | ||
| 15 | + | ||
| 16 | +@end | ||
| 17 | + | ||
| 18 | +NS_ASSUME_NONNULL_END |
CNLiveUserAgreementManager/Classes/CNLiveAgreementWebController.m
0 → 100644
| 1 | +// | ||
| 2 | +// CNLiveAgreementWebController.m | ||
| 3 | +// CNLiveServices_Example | ||
| 4 | +// | ||
| 5 | +// Created by CNLive-zxw on 2019/7/30. | ||
| 6 | +// Copyright © 2019 153993236@qq.com. All rights reserved. | ||
| 7 | +// | ||
| 8 | + | ||
| 9 | +#import "CNLiveAgreementWebController.h" | ||
| 10 | +#import <WebKit/WebKit.h> | ||
| 11 | + | ||
| 12 | +@interface CNLiveAgreementWebController ()<WKUIDelegate, WKNavigationDelegate> | ||
| 13 | +@property (nonatomic, copy) NSString *url; | ||
| 14 | +@property (nonatomic, copy) NSString *name; | ||
| 15 | +@property (nonatomic, strong) WKWebView *webView; | ||
| 16 | +@property (nonatomic, strong) UIProgressView *processView; | ||
| 17 | +@property (nonatomic, weak) UILabel *titleLabel; | ||
| 18 | + | ||
| 19 | +@end | ||
| 20 | + | ||
| 21 | +@implementation CNLiveAgreementWebController | ||
| 22 | + | ||
| 23 | +- (instancetype)initWithUrl:(NSString *)url name:(NSString *)name { | ||
| 24 | + self = [super init]; | ||
| 25 | + if (self) { | ||
| 26 | + self.url = url; | ||
| 27 | + self.name = name; | ||
| 28 | + } | ||
| 29 | + return self; | ||
| 30 | +} | ||
| 31 | + | ||
| 32 | +- (void)setName:(NSString *)name{ | ||
| 33 | + _name = name; | ||
| 34 | + _titleLabel.text = name; | ||
| 35 | + | ||
| 36 | +} | ||
| 37 | + | ||
| 38 | +#pragma mark - 加载数据 | ||
| 39 | +- (void)loadUrl { | ||
| 40 | + NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:self.url] cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:15.0]; | ||
| 41 | + [self.webView loadRequest:theRequest]; | ||
| 42 | + | ||
| 43 | +} | ||
| 44 | + | ||
| 45 | +#pragma mark - 生命周期 | ||
| 46 | +- (void)viewWillAppear:(BOOL)animated { | ||
| 47 | + [super viewWillAppear:animated]; | ||
| 48 | + self.navigationController.navigationBarHidden = YES; | ||
| 49 | + | ||
| 50 | +} | ||
| 51 | + | ||
| 52 | +- (void)viewWillDisappear:(BOOL)animated { | ||
| 53 | + [super viewWillDisappear:animated]; | ||
| 54 | + self.navigationController.navigationBarHidden = NO; | ||
| 55 | + | ||
| 56 | +} | ||
| 57 | + | ||
| 58 | +- (void)viewDidLoad { | ||
| 59 | + [super viewDidLoad]; | ||
| 60 | + self.view.backgroundColor = [UIColor whiteColor]; | ||
| 61 | + if (@available(iOS 11, *)) { | ||
| 62 | + self.webView.scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentAutomatic; | ||
| 63 | + } | ||
| 64 | + else { | ||
| 65 | + self.automaticallyAdjustsScrollViewInsets = NO; | ||
| 66 | + } | ||
| 67 | + [self setupNavigationBar]; | ||
| 68 | + [self setupUI]; | ||
| 69 | + [self loadUrl]; | ||
| 70 | + | ||
| 71 | +} | ||
| 72 | + | ||
| 73 | +- (void)dealloc { | ||
| 74 | + NSLog(@"CNUserAgreementWebController dealloc"); | ||
| 75 | + [self.webView stopLoading]; | ||
| 76 | + [self.webView removeObserver:self forKeyPath:@"estimatedProgress"]; | ||
| 77 | + if (!self.name) { | ||
| 78 | + [self.webView removeObserver:self forKeyPath:@"title"]; | ||
| 79 | + } | ||
| 80 | + self.webView.UIDelegate = nil; | ||
| 81 | + self.webView.navigationDelegate = nil; | ||
| 82 | + self.webView = nil; | ||
| 83 | + [self clearWKCache]; | ||
| 84 | + | ||
| 85 | +} | ||
| 86 | + | ||
| 87 | +#pragma mark - 创建导航条 | ||
| 88 | +- (void)setupNavigationBar { | ||
| 89 | + UIView *navView = [[UIView alloc] initWithFrame:CGRectMake(0, [UIApplication sharedApplication].statusBarFrame.size.height, [UIScreen mainScreen].bounds.size.width, 44)]; | ||
| 90 | + navView.backgroundColor = [UIColor clearColor]; | ||
| 91 | + [self.view bringSubviewToFront:navView]; | ||
| 92 | + [self.view addSubview:navView]; | ||
| 93 | + | ||
| 94 | + UIButton *leftBtn = [UIButton buttonWithType:UIButtonTypeSystem]; | ||
| 95 | + leftBtn.frame = CGRectMake(0, 0, 44, 44); | ||
| 96 | + UIImage *image = [self xw_getImageName:@"web_black_back" bundle:@"CNLiveAgreementManager.bundle/CNLiveAgreementManager" targetClass:[CNLiveAgreementWebController class]]; | ||
| 97 | + [leftBtn setImage:[image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal]; | ||
| 98 | +// [leftBtn setContentHorizontalAlignment:UIControlContentHorizontalAlignmentRight]; | ||
| 99 | + [leftBtn addTarget:self action:@selector(goBack) forControlEvents:UIControlEventTouchUpInside]; | ||
| 100 | + [navView addSubview:leftBtn]; | ||
| 101 | + | ||
| 102 | + UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(([[UIScreen mainScreen] bounds].size.width - 200)/2.0, 0, 200, 44)]; | ||
| 103 | + titleLabel.font = [UIFont boldSystemFontOfSize:17]; | ||
| 104 | + titleLabel.textColor = [UIColor blackColor]; | ||
| 105 | + titleLabel.text = self.name; | ||
| 106 | + titleLabel.textAlignment = NSTextAlignmentCenter; | ||
| 107 | + [navView addSubview:titleLabel]; | ||
| 108 | + _titleLabel = titleLabel; | ||
| 109 | + | ||
| 110 | +} | ||
| 111 | + | ||
| 112 | +- (void)setupUI { | ||
| 113 | + WKWebViewConfiguration * configuration = [[WKWebViewConfiguration alloc] init]; | ||
| 114 | + configuration.allowsInlineMediaPlayback = YES; | ||
| 115 | + WKPreferences *preferences = [WKPreferences new]; | ||
| 116 | + preferences.javaScriptCanOpenWindowsAutomatically = YES; | ||
| 117 | + configuration.preferences = preferences; | ||
| 118 | + 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]; | ||
| 119 | + _webView.UIDelegate = self; | ||
| 120 | + _webView.navigationDelegate = self; | ||
| 121 | + _webView.contentMode = UIViewContentModeScaleAspectFit; | ||
| 122 | + _webView.scrollView.showsHorizontalScrollIndicator = NO; | ||
| 123 | + _webView.allowsBackForwardNavigationGestures = YES; | ||
| 124 | + _webView.allowsLinkPreview = NO; | ||
| 125 | + [self.view addSubview:self.webView]; | ||
| 126 | + | ||
| 127 | + self.processView = [[UIProgressView alloc] initWithFrame:CGRectMake(0, ([UIApplication sharedApplication].statusBarFrame.size.height+44)+0.5, [[UIScreen mainScreen] bounds].size.width, 1)]; | ||
| 128 | + _processView.progressTintColor = [UIColor colorWithRed:0.0 green:1.0 blue:0.0 alpha:1.0]; | ||
| 129 | + _processView.trackTintColor = [UIColor whiteColor]; | ||
| 130 | + [self.view addSubview:self.processView]; | ||
| 131 | + | ||
| 132 | + [self.webView addObserver:self forKeyPath:@"estimatedProgress" options:NSKeyValueObservingOptionNew context:nil]; | ||
| 133 | + if (!self.name) { | ||
| 134 | + [self.webView addObserver:self forKeyPath:@"title" options:NSKeyValueObservingOptionNew context:NULL]; | ||
| 135 | + } | ||
| 136 | + | ||
| 137 | +} | ||
| 138 | + | ||
| 139 | +#pragma mark - WKNavigationDelegate | ||
| 140 | +// 在发送请求之前,决定是否跳转 | ||
| 141 | +- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler{ | ||
| 142 | + //允许跳转 | ||
| 143 | + decisionHandler(WKNavigationActionPolicyAllow); | ||
| 144 | + | ||
| 145 | +} | ||
| 146 | + | ||
| 147 | +// 页面开始加载时调用 | ||
| 148 | +- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation{ | ||
| 149 | + | ||
| 150 | +} | ||
| 151 | + | ||
| 152 | +// 在收到响应后,决定是否跳转 | ||
| 153 | +- (void)webView:(WKWebView *)webView decidePolicyForNavigationResponse:(WKNavigationResponse *)navigationResponse decisionHandler:(void (^)(WKNavigationResponsePolicy))decisionHandler{ | ||
| 154 | + //允许跳转 | ||
| 155 | + decisionHandler(WKNavigationResponsePolicyAllow); | ||
| 156 | + //不允许跳转 | ||
| 157 | + //decisionHandler(WKNavigationResponsePolicyCancel); | ||
| 158 | +} | ||
| 159 | + | ||
| 160 | +// 当内容开始返回时调用 | ||
| 161 | +- (void)webView:(WKWebView *)webView didCommitNavigation:(WKNavigation *)navigation{ | ||
| 162 | + | ||
| 163 | +} | ||
| 164 | + | ||
| 165 | +// 页面加载完成之后调用 | ||
| 166 | +- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation{ | ||
| 167 | + | ||
| 168 | +} | ||
| 169 | + | ||
| 170 | +// 页面加载失败时调用 | ||
| 171 | +- (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(null_unspecified WKNavigation *)navigation withError:(nonnull NSError *)error{ | ||
| 172 | + NSLog(@"打印这个error%@",error); | ||
| 173 | + | ||
| 174 | +} | ||
| 175 | + | ||
| 176 | +// 接收到服务器跳转请求之后调用(重定向) | ||
| 177 | +- (void)webView:(WKWebView *)webView didReceiveServerRedirectForProvisionalNavigation:(WKNavigation *)navigation{ | ||
| 178 | + | ||
| 179 | +} | ||
| 180 | + | ||
| 181 | +#pragma mark - 响应方法 | ||
| 182 | +//回退goBack | ||
| 183 | +- (void)goBack { | ||
| 184 | + if ([self.webView canGoBack]) { | ||
| 185 | + [self.webView goBack]; | ||
| 186 | + } | ||
| 187 | + else { | ||
| 188 | + [self closeClick]; | ||
| 189 | + } | ||
| 190 | +} | ||
| 191 | + | ||
| 192 | +//关闭pop | ||
| 193 | +- (void)closeClick { | ||
| 194 | + if (self.presentingViewController != nil) { | ||
| 195 | + [self dismissViewControllerAnimated:YES completion:nil]; | ||
| 196 | + | ||
| 197 | + } | ||
| 198 | + else { | ||
| 199 | + [self.navigationController popViewControllerAnimated:YES]; | ||
| 200 | + } | ||
| 201 | +} | ||
| 202 | + | ||
| 203 | +#pragma mark - KVO | ||
| 204 | +- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context { | ||
| 205 | + if ([keyPath isEqualToString:@"estimatedProgress"]) { | ||
| 206 | + if (object == self.webView) { | ||
| 207 | + [self.processView setAlpha:1.0f]; | ||
| 208 | + [self.processView setProgress:self.webView.estimatedProgress animated:YES]; | ||
| 209 | + | ||
| 210 | + if(self.webView.estimatedProgress >= 1.0f) { | ||
| 211 | + | ||
| 212 | + [UIView animateWithDuration:0.3 delay:0.5 options:UIViewAnimationOptionCurveEaseOut animations:^{ | ||
| 213 | + [self.processView setAlpha:0.0f]; | ||
| 214 | + | ||
| 215 | + } completion:^(BOOL finished) { | ||
| 216 | + [self.processView setProgress:0.0f animated:NO]; | ||
| 217 | + | ||
| 218 | + }]; | ||
| 219 | + | ||
| 220 | + } | ||
| 221 | + | ||
| 222 | + } | ||
| 223 | + else { | ||
| 224 | + [super observeValueForKeyPath:keyPath ofObject:object change:change context:context]; | ||
| 225 | + } | ||
| 226 | + | ||
| 227 | + } | ||
| 228 | + else if ([keyPath isEqualToString:@"title"]){ | ||
| 229 | + if (object == self.webView) { | ||
| 230 | + if (self.webView.title.length != 0) { | ||
| 231 | + self.name = self.webView.title; | ||
| 232 | + | ||
| 233 | + } | ||
| 234 | + } | ||
| 235 | + else { | ||
| 236 | + [super observeValueForKeyPath:keyPath ofObject:object change:change context:context]; | ||
| 237 | + } | ||
| 238 | + } | ||
| 239 | + else { | ||
| 240 | + [super observeValueForKeyPath:keyPath ofObject:object change:change context:context]; | ||
| 241 | + | ||
| 242 | + } | ||
| 243 | +} | ||
| 244 | + | ||
| 245 | +#pragma mark - 清除缓存 | ||
| 246 | +// 清除缓存 | ||
| 247 | +- (void)clearWKCache { | ||
| 248 | + NSArray *types = @[WKWebsiteDataTypeDiskCache,WKWebsiteDataTypeOfflineWebApplicationCache,WKWebsiteDataTypeMemoryCache,WKWebsiteDataTypeIndexedDBDatabases,WKWebsiteDataTypeWebSQLDatabases]; | ||
| 249 | + NSSet *websiteDataTypes = [NSSet setWithArray:types]; | ||
| 250 | + NSDate *dateFrom = [NSDate dateWithTimeIntervalSince1970:0]; | ||
| 251 | + [[WKWebsiteDataStore defaultDataStore] removeDataOfTypes:websiteDataTypes modifiedSince:dateFrom completionHandler:^{ | ||
| 252 | + NSLog(@"清楚缓存完毕"); | ||
| 253 | + }]; | ||
| 254 | + | ||
| 255 | +} | ||
| 256 | +/** | ||
| 257 | + * 创建图片 | ||
| 258 | + * | ||
| 259 | + * @param imageName 图片名字 | ||
| 260 | + * @param bundlePath 图片所在的bundle名字 | ||
| 261 | + * @param targetClass 类和bundle的同级目录 | ||
| 262 | + * @return 返回UIImage | ||
| 263 | + */ | ||
| 264 | +- (UIImage *)xw_getImageName:(NSString *)imageName bundle:(NSString *)bundlePath targetClass:(Class)targetClass{ | ||
| 265 | + NSBundle *bundle = [NSBundle bundleForClass:targetClass]; | ||
| 266 | + NSURL *url = [bundle URLForResource:bundlePath withExtension:@"bundle"]; | ||
| 267 | + NSBundle *targetBundle = [NSBundle bundleWithURL:url]; | ||
| 268 | + UIImage *image = [UIImage imageNamed:imageName inBundle:targetBundle compatibleWithTraitCollection:nil]; | ||
| 269 | + return image?image:[UIImage imageNamed:imageName inBundle:[NSBundle mainBundle] compatibleWithTraitCollection:nil]; | ||
| 270 | + | ||
| 271 | +} | ||
| 272 | + | ||
| 273 | +@end |
Example/CNLiveUserAgreementManager/CNLiveUserAgreementManager-Info.plist
| @@ -2,6 +2,11 @@ | @@ -2,6 +2,11 @@ | ||
| 2 | <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> | 2 | <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> |
| 3 | <plist version="1.0"> | 3 | <plist version="1.0"> |
| 4 | <dict> | 4 | <dict> |
| 5 | + <key>NSAppTransportSecurity</key> | ||
| 6 | + <dict> | ||
| 7 | + <key>NSAllowsArbitraryLoads</key> | ||
| 8 | + <true/> | ||
| 9 | + </dict> | ||
| 5 | <key>CFBundleDevelopmentRegion</key> | 10 | <key>CFBundleDevelopmentRegion</key> |
| 6 | <string>en</string> | 11 | <string>en</string> |
| 7 | <key>CFBundleDisplayName</key> | 12 | <key>CFBundleDisplayName</key> |
Example/CNLiveUserAgreementManager/CNLiveViewController.m
| @@ -7,8 +7,7 @@ | @@ -7,8 +7,7 @@ | ||
| 7 | // | 7 | // |
| 8 | 8 | ||
| 9 | #import "CNLiveViewController.h" | 9 | #import "CNLiveViewController.h" |
| 10 | -#import "CNUserAgreementManager.h" | ||
| 11 | -#import "CNLiveEnvironmentConfiguration.h" | 10 | +#import "CNLiveAgreementManager.h" |
| 12 | 11 | ||
| 13 | @interface CNLiveViewController () | 12 | @interface CNLiveViewController () |
| 14 | 13 | ||
| @@ -20,15 +19,27 @@ | @@ -20,15 +19,27 @@ | ||
| 20 | { | 19 | { |
| 21 | [super viewDidLoad]; | 20 | [super viewDidLoad]; |
| 22 | // Do any additional setup after loading the view, typically from a nib. | 21 | // Do any additional setup after loading the view, typically from a nib. |
| 23 | - NSLog(@"%@",CNGetAgreementH5Url); | ||
| 24 | 22 | ||
| 25 | - [CNUserAgreementManager getAgreementH5WithType:CNAgreementUser success:^(NSString * _Nonnull h5, NSString * _Nonnull title) { | ||
| 26 | - NSLog(@"%@",h5); | ||
| 27 | - NSLog(@"%@",title); | 23 | + UIButton *btn0 = [UIButton buttonWithType:UIButtonTypeCustom]; |
| 24 | + btn0.frame = CGRectMake(0, 0, 100, 100); | ||
| 25 | + btn0.center = self.view.center; | ||
| 26 | + btn0.backgroundColor = [UIColor redColor]; | ||
| 27 | + [btn0 setTitle:@"进入" forState:UIControlStateNormal]; | ||
| 28 | + [btn0 addTarget:self action:@selector(testButtonClicked) forControlEvents:UIControlEventTouchUpInside]; | ||
| 29 | + [self.view addSubview:btn0]; | ||
| 30 | + | ||
| 31 | +// [CNLiveAgreementManager getAgreementH5WithType:CNAgreementUser success:^(NSString * _Nonnull h5, NSString * _Nonnull title) { | ||
| 32 | +// NSLog(@"%@",h5); | ||
| 33 | +// NSLog(@"%@",title); | ||
| 34 | +// | ||
| 35 | +// } failure:^(NSError * _Nonnull error) { | ||
| 36 | +// | ||
| 37 | +// }]; | ||
| 38 | +} | ||
| 39 | +- (void)testButtonClicked { | ||
| 40 | +// [CNLiveAgreementManager jumpToAgreementH5WithType:CNAgreementUser]; | ||
| 41 | + [CNLiveAgreementManager jumpToAgreementH5WithString:@"bzfk"]; | ||
| 28 | 42 | ||
| 29 | - } failure:^(NSError * _Nonnull error) { | ||
| 30 | - | ||
| 31 | - }]; | ||
| 32 | } | 43 | } |
| 33 | 44 | ||
| 34 | - (void)didReceiveMemoryWarning | 45 | - (void)didReceiveMemoryWarning |