YMGoddessCertifiedProtocolView.m 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. //
  2. // YMGoddessCertifiedProtocolView.m
  3. // MSYOUPAI
  4. //
  5. // Created by YoMi on 2024/3/2.
  6. // Copyright © 2024 MS. All rights reserved.
  7. //
  8. #import "YMGoddessCertifiedProtocolView.h"
  9. #import "YMGoddessCertifiedProtocolViewModel.h"
  10. @interface YMGoddessCertifiedProtocolView()<WKUIDelegate,WKNavigationDelegate>
  11. /// 女神认证协议VM
  12. @property (nonatomic, strong) YMGoddessCertifiedProtocolViewModel *viewModel;
  13. /// 浏览器配置
  14. @property (nonatomic, strong) WKWebViewConfiguration *webConfig;
  15. /// 网页文章浏览器
  16. @property (nonatomic, strong) WKWebView *webArticleView;
  17. /// 进度条
  18. @property (nonatomic, strong) UIProgressView *progressView;
  19. /// 当前Url
  20. @property (nonatomic, strong) NSURL *currentUrl;
  21. @end
  22. @implementation YMGoddessCertifiedProtocolView
  23. - (void)ym_setupViews{
  24. [self addSubview:self.progressView];
  25. [self addSubview:self.webArticleView];
  26. }
  27. - (void)updateConstraints{
  28. [self.progressView mas_makeConstraints:^(MASConstraintMaker *make) {
  29. make.top.equalTo(self);
  30. make.left.equalTo(self);
  31. make.right.equalTo(self);
  32. make.height.mas_equalTo(adapt(2));
  33. }];
  34. [self.webArticleView mas_makeConstraints:^(MASConstraintMaker *make) {
  35. make.top.equalTo(self.progressView.mas_bottom);
  36. make.left.equalTo(self);
  37. make.right.equalTo(self);
  38. make.bottom.equalTo(self);
  39. }];
  40. [super updateConstraints];
  41. }
  42. - (void)ym_bindViewModel:(YMGoddessCertifiedProtocolViewModel*)viewModel{
  43. if (!viewModel) {
  44. return;
  45. }
  46. _viewModel = viewModel;
  47. @weakify(self)
  48. [[[[RACObserve(self.viewModel, webArticleUrl) distinctUntilChanged] deliverOnMainThread] takeUntil:self.rac_willDeallocSignal] subscribeNext:^(NSString * webArticleUrl) {
  49. @strongify(self)
  50. [self web_loadURLString:webArticleUrl];
  51. }];
  52. [[self.viewModel.refreshUISubject takeUntil:self.rac_willDeallocSignal] subscribeNext:^(id result) {
  53. @strongify(self)
  54. [self.webArticleView reload];
  55. }];
  56. self.webArticleView.ba_web_didStartBlock = ^(WKWebView *webView, WKNavigation *navigation) {
  57. @strongify(self)
  58. NSLog(@"开始加载网页");
  59. };
  60. self.webArticleView.ba_web_didFinishBlock = ^(WKWebView *webView, WKNavigation *navigation) {
  61. @strongify(self)
  62. NSLog(@"加载网页结束");
  63. // WKWebview 禁止长按(超链接、图片、文本...)弹出效果
  64. [webView ba_web_stringByEvaluateJavaScript:@"document.documentElement.style.webkitTouchCallout='none'" completionHandler:nil];
  65. [webView ba_web_stringByEvaluateJavaScript:@"document.body.offsetHeight" completionHandler:^(id _Nullable result, NSError * _Nullable error) {
  66. [self.webArticleView mas_updateConstraints:^(MASConstraintMaker *make) {
  67. make.height.mas_equalTo([result doubleValue]);
  68. }];
  69. }];
  70. };
  71. self.webArticleView.ba_web_isLoadingBlock = ^(BOOL isLoading, CGFloat progress) {
  72. @strongify(self)
  73. [self web_progressShow];
  74. self.progressView.progress = progress;
  75. if (self.progressView.progress == 1.0f){
  76. [self web_progressHidden];
  77. }
  78. };
  79. self.webArticleView.ba_web_getCurrentUrlBlock = ^(NSURL * _Nonnull currentUrl) {
  80. @strongify(self)
  81. self.currentUrl = currentUrl;
  82. };
  83. }
  84. //iOS9.0以上异常终止时调用
  85. - (void)webViewWebContentProcessDidTerminate:(WKWebView *)webView{
  86. [webView reload];
  87. }
  88. - (void)web_progressShow{
  89. // 开始加载网页时展示出progressView
  90. self.progressView.hidden = NO;
  91. // 开始加载网页的时候将progressView的Height恢复为1.5倍
  92. self.progressView.transform = CGAffineTransformMakeScale(1.0f, 1.5f);
  93. // 防止progressView被网页挡住
  94. [self bringSubviewToFront:self.progressView];
  95. }
  96. - (void)web_progressHidden{
  97. /*
  98. *添加一个简单的动画,将progressView的Height变为1.4倍,在开始加载网页的代理中会恢复为1.5倍
  99. *动画时长0.25s,延时0.3s后开始动画
  100. *动画结束后将progressView隐藏
  101. */
  102. [UIView animateWithDuration:0.25f delay:0.3f options:UIViewAnimationOptionCurveEaseOut animations:^{
  103. self.progressView.transform = CGAffineTransformMakeScale(1.0f, 1.4f);
  104. } completion:^(BOOL finished) {
  105. self.progressView.hidden = YES;
  106. }];
  107. }
  108. /**
  109. * 加载一个 webview
  110. *
  111. * @param request 请求的 NSURL URLRequest
  112. */
  113. - (void)web_loadRequest:(NSURLRequest *)request{
  114. [self.webArticleView ba_web_loadRequest:request];
  115. }
  116. /**
  117. * 加载一个 webview
  118. *
  119. * @param URL 请求的 URL
  120. */
  121. - (void)web_loadURL:(NSURL *)URL{
  122. [self.webArticleView ba_web_loadURL:URL];
  123. }
  124. /**
  125. * 加载一个 webview
  126. *
  127. * @param URLString 请求的 URLString
  128. */
  129. - (void)web_loadURLString:(NSString *)URLString{
  130. [self.webArticleView ba_web_loadURLString:URLString];
  131. }
  132. /**
  133. * 加载本地网页
  134. *
  135. * @param htmlName 请求的本地 HTML 文件名
  136. */
  137. - (void)web_loadHTMLFileName:(NSString *)htmlName{
  138. [self.webArticleView ba_web_loadHTMLFileName:htmlName];
  139. }
  140. /**
  141. * 加载本地 htmlString
  142. *
  143. * @param htmlString 请求的本地 htmlString
  144. */
  145. - (void)web_loadHTMLString:(NSString *)htmlString{
  146. [self.webArticleView ba_web_loadHTMLString:htmlString];
  147. }
  148. /**
  149. * 加载 js 字符串,例如:高度自适应获取代码:
  150. // webView 高度自适应
  151. [self web_stringByEvaluateJavaScript:@"document.body.offsetHeight" completionHandler:^(id _Nullable result, NSError * _Nullable error) {
  152. // 获取页面高度,并重置 webview 的 frame
  153. self.ba_web_currentHeight = [result doubleValue];
  154. CGRect frame = webView.frame;
  155. frame.size.height = self.ba_web_currentHeight;
  156. webView.frame = frame;
  157. }];
  158. *
  159. * @param javaScriptString js 字符串
  160. */
  161. - (void)web_stringByEvaluateJavaScript:(NSString *)javaScriptString completionHandler:(void (^ _Nullable)(_Nullable id, NSError * _Nullable error))completionHandler{
  162. [self.webArticleView ba_web_stringByEvaluateJavaScript:javaScriptString completionHandler:completionHandler];
  163. }
  164. - (void)setWeb_progressTintColor:(UIColor *)web_progressTintColor{
  165. _web_progressTintColor = web_progressTintColor;
  166. self.progressView.progressTintColor = web_progressTintColor;
  167. }
  168. - (void)setWeb_progressTrackTintColor:(UIColor *)web_progressTrackTintColor{
  169. _web_progressTrackTintColor = web_progressTrackTintColor;
  170. self.progressView.trackTintColor = web_progressTrackTintColor;
  171. }
  172. - (UIProgressView *)progressView {
  173. if (!_progressView){
  174. _progressView = [[UIProgressView alloc] initWithFrame:CGRectZero];
  175. _progressView.tintColor = BAKit_Color_Green_pod;
  176. _progressView.trackTintColor = BAKit_Color_Gray_8_pod;
  177. _progressView.transform = CGAffineTransformMakeScale(1.0f, 1.5f);
  178. }
  179. return _progressView;
  180. }
  181. - (WKWebViewConfiguration *)webConfig{
  182. if (!_webConfig) {
  183. // 创建并配置WKWebView的相关参数
  184. // 1.WKWebViewConfiguration:是WKWebView初始化时的配置类,里面存放着初始化WK的一系列属性;
  185. // 2.WKUserContentController:为JS提供了一个发送消息的通道并且可以向页面注入JS的类,WKUserContentController对象可以添加多个scriptMessageHandler;
  186. // 3.addScriptMessageHandler:name:有两个参数,第一个参数是userContentController的代理对象,第二个参数是JS里发送postMessage的对象。添加一个脚本消息的处理器,同时需要在JS中添加,window.webkit.messageHandlers.<name>.postMessage(<messageBody>)才能起作用。
  187. _webConfig = [[WKWebViewConfiguration alloc] init];
  188. _webConfig.allowsInlineMediaPlayback = YES;
  189. WKUserContentController *userContentController = [[WKUserContentController alloc] init];
  190. _webConfig.userContentController = userContentController;
  191. // 初始化偏好设置属性:preferences
  192. _webConfig.preferences = [WKPreferences new];
  193. // The minimum font size in points default is 0;
  194. _webConfig.preferences.minimumFontSize = 15;
  195. // 是否支持 JavaScript
  196. _webConfig.preferences.javaScriptEnabled = YES;
  197. // 不通过用户交互,是否可以打开窗口
  198. _webConfig.preferences.javaScriptCanOpenWindowsAutomatically = NO;
  199. }
  200. return _webConfig;
  201. }
  202. - (WKWebView *)webArticleView{
  203. if (!_webArticleView) {
  204. _webArticleView = [[WKWebView alloc] initWithFrame:CGRectZero configuration:self.webConfig];
  205. [_webArticleView ba_web_initWithDelegate:self.webArticleView.navigationDelegate uIDelegate:self.webArticleView.UIDelegate];
  206. _webArticleView.ba_web_isAutoHeight = NO;
  207. _webArticleView.multipleTouchEnabled = YES;
  208. _webArticleView.autoresizesSubviews = YES;
  209. _webArticleView.opaque = NO;
  210. _webArticleView.backgroundColor = HexColorFromRGB(0xFFFFFF);
  211. _webArticleView.scrollView.showsVerticalScrollIndicator = NO;
  212. }
  213. return _webArticleView;
  214. }
  215. @end