RQWebViewViewController.m 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. //
  2. // RQWebViewViewController.m
  3. // RQCommon
  4. //
  5. // Created by 张嵘 on 2018/11/27.
  6. // Copyright © 2018 张嵘. All rights reserved.
  7. //
  8. #import "RQWebViewViewController.h"
  9. #import "WKWebViewJavascriptBridge.h"
  10. #import <AlipaySDK/AlipaySDK.h>
  11. @interface RQWebViewViewController ()
  12. /// webView
  13. @property (nonatomic, weak, readwrite) WKWebView *webView;
  14. /// 进度条
  15. @property (nonatomic, readwrite, strong) UIProgressView *progressView;
  16. /// 返回按钮
  17. @property (nonatomic, readwrite, strong) UIBarButtonItem *backItem;
  18. /// 关闭按钮 (点击关闭按钮 退出WebView)
  19. @property (nonatomic, readwrite, strong) UIBarButtonItem *closeItem;
  20. /// viewModel
  21. @property (nonatomic, strong, readonly) RQWebViewModel *viewModel;
  22. @end
  23. @implementation RQWebViewViewController
  24. @dynamic viewModel;
  25. - (void)dealloc{
  26. RQDealloc;
  27. /// remove observer ,otherwise will crash
  28. [_webView stopLoading];
  29. }
  30. - (void)viewWillAppear:(BOOL)animated {
  31. [super viewWillAppear:animated];
  32. /// 清除缓存
  33. NSSet *websiteDataTypes = [WKWebsiteDataStore allWebsiteDataTypes];
  34. NSDate *dateFrom = [NSDate dateWithTimeIntervalSince1970:0];
  35. [[WKWebsiteDataStore defaultDataStore] removeDataOfTypes:websiteDataTypes modifiedSince:dateFrom completionHandler:^{
  36. }];
  37. [self.navigationController.navigationBar addSubview:self.progressView];
  38. if (self.viewModel.request) {
  39. [self.webView loadRequest:self.viewModel.request];
  40. } else if (RQStringIsNotEmpty(self.viewModel.requestUrl)) {
  41. /// 加载请求数据
  42. NSString *encodedString = (NSString*) CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,(CFStringRef)self.viewModel.requestUrl,(CFStringRef)@"!$&'()*+,-./:;=?@_~%#[]",NULL, kCFStringEncodingUTF8));
  43. NSURL *httpUrl = [NSURL URLWithString:encodedString];
  44. NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:httpUrl];
  45. [self.webView loadRequest:request];
  46. }
  47. }
  48. - (void)viewWillDisappear:(BOOL)animated {
  49. [super viewWillDisappear:animated];
  50. [self.progressView removeFromSuperview];
  51. }
  52. - (void)viewDidLoad {
  53. [super viewDidLoad];
  54. /// 添加断言,request错误 应用直接crash
  55. // NSParameterAssert(self.viewModel.request);
  56. [self.navigationItem setLeftBarButtonItems:@[self.backItem]];
  57. [self.navigationItem setRightBarButtonItems:@[self.closeItem]];
  58. ///CoderMikeHe FIXED: 切记 lightempty_ios 是前端跟H5商量的结果,请勿修改。
  59. NSString *userAgent = @"";
  60. if (!(RQIOSVersion>=9.0)) [[NSUserDefaults standardUserDefaults] registerDefaults:@{@"userAgent":userAgent}];
  61. /// 注册JS
  62. WKUserContentController *userContentController = [[WKUserContentController alloc] init];
  63. /// 这里可以注册JS的处理 涉及公司私有方法 这里笔者不作处理
  64. if (self.viewModel.webViewType == RQWebViewType_Exam) {
  65. [userContentController addScriptMessageHandler:self name:@"backView"];
  66. [userContentController addScriptMessageHandler:self name:@"displayRow"];
  67. [userContentController addScriptMessageHandler:self name:@"displayCol"];
  68. }
  69. WKWebViewConfiguration *configuration = [[WKWebViewConfiguration alloc] init];
  70. configuration.allowsInlineMediaPlayback = YES;
  71. if (@available(iOS 10.0, *)) {
  72. configuration.mediaTypesRequiringUserActionForPlayback = WKAudiovisualMediaTypeNone;
  73. }
  74. NSMutableString *jsString = [NSMutableString string];
  75. /// 自适应屏幕宽度
  76. [jsString appendString:@"var meta = document.createElement('meta'); meta.setAttribute('name', 'viewport'); meta.setAttribute('content', 'width=device-width'); document.getElementsByTagName('head')[0].appendChild(meta);"];
  77. /// 禁止长按
  78. [jsString appendString:@"document.documentElement.style.webkitTouchCallout='none';"];
  79. /// 禁止选择
  80. [jsString appendString:@"document.documentElement.style.webkitUserSelect='none';"];
  81. /// 关闭H5的缩放手势
  82. [jsString appendString:@"var script = document.createElement('meta');"
  83. "script.name = 'viewport';"
  84. "script.content=\"width=device-width, initial-scale=1.0,maximum-scale=1.0, minimum-scale=1.0, user-scalable=no\";"
  85. "document.getElementsByTagName('head')[0].appendChild(script);"];
  86. WKUserScript *userScript = [[WKUserScript alloc] initWithSource:jsString injectionTime:WKUserScriptInjectionTimeAtDocumentEnd forMainFrameOnly:YES];
  87. // 添加自适应屏幕宽度js调用的方法
  88. [userContentController addUserScript:userScript];
  89. /// 赋值userContentController
  90. configuration.userContentController = userContentController;
  91. WKWebView *webView = [[WKWebView alloc] initWithFrame:RQ_SCREEN_BOUNDS configuration:configuration];
  92. webView.navigationDelegate = self;
  93. webView.UIDelegate = self;
  94. if (@available(iOS 9.0, *)) {
  95. webView.customUserAgent = userAgent;
  96. } else {
  97. // Fallback on earlier versions
  98. }
  99. self.webView = webView;
  100. [self.view addSubview:webView];
  101. /// oc调用js
  102. [webView evaluateJavaScript:@"navigator.userAgent" completionHandler:^(id result, NSError *error) {
  103. NSLog(@"navigator.userAgent.result is ++++ %@", result);
  104. }];
  105. /// 监听数据
  106. @weakify(self);
  107. /// binding self.viewModel.avatarUrlString
  108. [RACObserve(self.webView, title) subscribeNext:^(NSString *titleStr) {
  109. @strongify(self);
  110. NSLog(@"%@",titleStr);
  111. /// CoderMikeHe FIXED: 这里只设置导航栏的title 以免self.title 设置了tabBarItem.title
  112. if (!self.viewModel.shouldDisableWebViewTitle) self.titleView.title = self.webView.title;
  113. }];
  114. [RACObserve(self.webView, loading) subscribeNext:^(NSNumber *currentLoadingState) {
  115. @strongify(self)
  116. BOOL isLoading = [currentLoadingState boolValue];
  117. if (isLoading) {
  118. NSLog(@"--- webView is loading ---");
  119. if ((self.viewModel.webViewType == RQWebViewType_Exam)) {
  120. [self enterLandscapeFullScreen:UIInterfaceOrientationLandscapeLeft animated:YES];
  121. [self.webView setNeedsLayout];
  122. [self.webView layoutIfNeeded];
  123. }
  124. }else {
  125. NSLog(@"--- webView isn't loading ---");
  126. }
  127. }];
  128. [RACObserve(self.webView, estimatedProgress) subscribeNext:^(NSNumber *estimatedProgressValue) {
  129. NSLog(@"%@",estimatedProgressValue);
  130. @strongify(self)
  131. [self.progressView setAlpha:1.0f];
  132. [self.progressView setProgress:self.webView.estimatedProgress animated:YES];
  133. if(self.webView.estimatedProgress >= 1.0f) {
  134. @weakify(self);
  135. [UIView animateWithDuration:0.3 delay:0.3 options:UIViewAnimationOptionCurveEaseOut animations:^{
  136. @strongify(self)
  137. [self.progressView setAlpha:0.0f];
  138. } completion:^(BOOL finished) {
  139. @strongify(self)
  140. [self.progressView setProgress:0.0f animated:NO];
  141. }];
  142. }
  143. }];
  144. /// 添加刷新控件
  145. if(self.viewModel.shouldPullDownToRefresh){
  146. [self.webView.scrollView rq_addHeaderRefresh:^(MJRefreshNormalHeader *header) {
  147. @strongify(self)
  148. [self.webView reload];
  149. }];
  150. [self.webView.scrollView.mj_header beginRefreshing];
  151. }
  152. self.webView.scrollView.contentInset = self.contentInset;
  153. /// CoderMikeHe: 适配 iPhone X + iOS 11,去掉安全区域
  154. if (@available(iOS 11.0, *)) {
  155. RQAdjustsScrollViewInsets_Never(webView.scrollView);
  156. }
  157. }
  158. #pragma mark - 事件处理
  159. - (void)_backItemDidClicked{ /// 返回按钮事件处理
  160. /// 可以返回到上一个网页,就返回到上一个网页
  161. if (self.webView.canGoBack) {
  162. [self.webView goBack];
  163. }else{/// 不能返回上一个网页,就返回到上一个界面
  164. /// 判断 是Push还是Present进来的,
  165. if (self.presentingViewController) {
  166. if (self.viewModel.webViewType == RQWebViewType_VIP) {
  167. [self dismissViewControllerAnimated:YES completion:NULL];
  168. } else {
  169. [self.viewModel.services dismissViewModelAnimated:YES completion:NULL];
  170. }
  171. } else {
  172. [self.viewModel.services popViewModelAnimated:YES];
  173. }
  174. }
  175. }
  176. - (void)_closeItemDidClicked{
  177. /// 判断 是Push还是Present进来的
  178. if (self.presentingViewController) {
  179. if (self.viewModel.webViewType == RQWebViewType_VIP) {
  180. [self dismissViewControllerAnimated:YES completion:NULL];
  181. } else {
  182. [self.viewModel.services dismissViewModelAnimated:YES completion:NULL];
  183. }
  184. } else {
  185. [self.viewModel.services popViewModelAnimated:YES];
  186. }
  187. }
  188. - (UIEdgeInsets)contentInset{
  189. BOOL isExamUrl = (self.viewModel.webViewType == RQWebViewType_Exam);
  190. return UIEdgeInsetsMake(isExamUrl? 0 : (RQ_APPLICATION_NAV_BAR_HEIGHT + RQ_APPLICATION_STATUS_BAR_HEIGHT), isExamUrl? 0 : 0, isExamUrl? 0 : RQ_APPLICATION_SAFEAREA_BOTTOM_HEIGHT, isExamUrl? 0 : 0);
  191. }
  192. - (void)enterLandscapeFullScreen:(UIInterfaceOrientation)orientation animated:(BOOL)animated {
  193. CGFloat cellHeight = RQ_SCREEN_HEIGHT - RQ_APPLICATION_STATUS_BAR_HEIGHT - RQ_APPLICATION_SAFEAREA_BOTTOM_HEIGHT;
  194. CGFloat rotation = 0;
  195. CGFloat duration = [UIApplication sharedApplication].statusBarOrientationAnimationDuration;
  196. if (orientation == UIInterfaceOrientationLandscapeLeft) {
  197. rotation = M_PI_2;
  198. }else if (orientation == UIInterfaceOrientationLandscapeRight) {
  199. rotation = M_PI_2 * 3;
  200. }
  201. UIView *presentView = self.webView;
  202. @weakify(presentView)
  203. CGRect landRect = CGRectMake(RQ_APPLICATION_STATUS_BAR_HEIGHT, 0, cellHeight, RQ_SCREEN_WIDTH);
  204. [UIView animateWithDuration:duration animations:^{
  205. @strongify(presentView)
  206. presentView.layer.affineTransform = CGAffineTransformMakeRotation(rotation);
  207. } completion:^(BOOL finished) {
  208. @strongify(presentView)
  209. presentView.layer.bounds = landRect;
  210. }];
  211. }
  212. #pragma mark - WKScriptMessageHandler
  213. - (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message{
  214. /// js call OC function
  215. // [QMUIConsole log:[NSString stringWithFormat:@"js call OC function :%@",message.name]];
  216. NSLog(@"js call OC function :%@",message.name);
  217. if ([message.name isEqualToString:@"displayRow"]) {
  218. [self enterLandscapeFullScreen:UIInterfaceOrientationLandscapeLeft animated:YES];
  219. [self.webView setNeedsLayout];
  220. [self.webView layoutIfNeeded];
  221. } else if ([message.name isEqualToString:@"backView"]) {
  222. [userContentController removeScriptMessageHandlerForName:@"backView"];
  223. [userContentController removeScriptMessageHandlerForName:@"displayRow"];
  224. [userContentController removeScriptMessageHandlerForName:@"displayCol"];
  225. // [self _backItemDidClicked];
  226. [self.viewModel.services popViewModelAnimated:YES];
  227. }
  228. }
  229. #pragma mark - WKNavigationDelegate
  230. /// 内容开始返回时调用
  231. - (void)webView:(WKWebView *)webView didCommitNavigation:(null_unspecified WKNavigation *)navigation {
  232. /// 不显示关闭按钮
  233. if(self.viewModel.shouldDisableWebViewClose) return;
  234. UIBarButtonItem *backItem = self.navigationItem.leftBarButtonItems.firstObject;
  235. if (backItem) {
  236. if ([self.webView canGoBack]) {
  237. [self.navigationItem setLeftBarButtonItems:@[backItem, self.closeItem]];
  238. } else {
  239. [self.navigationItem setLeftBarButtonItems:@[backItem]];
  240. }
  241. }
  242. }
  243. // 导航完成时,会回调(也就是页面载入完成了)
  244. - (void)webView:(WKWebView *)webView didFinishNavigation:(null_unspecified WKNavigation *)navigation {
  245. if (self.viewModel.shouldPullDownToRefresh) [webView.scrollView.mj_header endRefreshing];
  246. [webView evaluateJavaScript:@"document.body.scrollHeight" completionHandler:^(id _Nullable result, NSError * _Nullable error) {
  247. double height = [result doubleValue];
  248. NSLog(@"%f",height);
  249. }];
  250. }
  251. // 导航失败时会回调
  252. - (void)webView:(WKWebView *)webView didFailNavigation:(null_unspecified WKNavigation *)navigation withError:(NSError *)error {
  253. if (self.viewModel.shouldPullDownToRefresh) [webView.scrollView.mj_header endRefreshing];
  254. }
  255. // 在发送请求之前,决定是否跳转
  256. - (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler {
  257. NSLog(@"navigationAction.request.URL: %@", navigationAction.request.URL);
  258. decisionHandler(WKNavigationActionPolicyAllow);
  259. }
  260. - (void)webView:(WKWebView *)webView decidePolicyForNavigationResponse:(WKNavigationResponse *)navigationResponse decisionHandler:(void (^)(WKNavigationResponsePolicy))decisionHandler {
  261. decisionHandler(WKNavigationResponsePolicyAllow);
  262. }
  263. - (void)webView:(WKWebView *)webView didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential *__nullable credential))completionHandler {
  264. NSURLCredential *cred = [[NSURLCredential alloc] initWithTrust:challenge.protectionSpace.serverTrust];
  265. completionHandler(NSURLSessionAuthChallengeUseCredential, cred);
  266. }
  267. #pragma mark - WKUIDelegate
  268. - (nullable WKWebView *)webView:(WKWebView *)webView createWebViewWithConfiguration:(WKWebViewConfiguration *)configuration forNavigationAction:(WKNavigationAction *)navigationAction windowFeatures:(WKWindowFeatures *)windowFeatures {
  269. /// CoderMike Fixed : 解决点击网页的链接 不跳转的Bug。
  270. WKFrameInfo *frameInfo = navigationAction.targetFrame;
  271. if (![frameInfo isMainFrame]) {
  272. [webView loadRequest:navigationAction.request];
  273. }
  274. return nil;
  275. }
  276. #pragma mark runJavaScript
  277. - (void)webView:(WKWebView *)webView runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(void))completionHandler {
  278. // [NSObject rq_showAlertViewWithTitle:nil message:message confirmTitle:@"我知道了"];
  279. completionHandler();
  280. }
  281. - (void)webView:(WKWebView *)webView runJavaScriptConfirmPanelWithMessage:(NSString *)message initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(BOOL result))completionHandler {
  282. completionHandler(YES);
  283. }
  284. - (void)webView:(WKWebView *)webView runJavaScriptTextInputPanelWithPrompt:(NSString *)prompt defaultText:(nullable NSString *)defaultText initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(NSString * __nullable result))completionHandler {
  285. completionHandler(defaultText);
  286. }
  287. #pragma mark - Getter & Setter
  288. - (UIProgressView *)progressView {
  289. if (!_progressView) {
  290. CGFloat progressViewW = RQ_SCREEN_WIDTH;
  291. CGFloat progressViewH = 3;
  292. CGFloat progressViewX = 0;
  293. CGFloat progressViewY = CGRectGetHeight(self.navigationController.navigationBar.frame) - progressViewH + 1;
  294. UIProgressView *progressView = [[UIProgressView alloc] initWithFrame:CGRectMake(progressViewX, progressViewY, progressViewW, progressViewH)];
  295. progressView.progressTintColor = RQ_MAIN_TINTCOLOR;
  296. progressView.trackTintColor = [UIColor clearColor];
  297. [self.view addSubview:progressView];
  298. self.progressView = progressView;
  299. }
  300. return _progressView;
  301. }
  302. - (UIBarButtonItem *)backItem
  303. {
  304. if (_backItem == nil) {
  305. _backItem = [UIBarButtonItem rq_backItemWithTitle:@"返回" imageName:@"backIcon" target:self action:@selector(_backItemDidClicked)];
  306. }
  307. return _backItem;
  308. }
  309. - (UIBarButtonItem *)closeItem {
  310. if (!_closeItem) {
  311. _closeItem = [UIBarButtonItem rq_systemItemWithTitle:@"关闭" titleColor:RQ_MAIN_COLOR imageName:nil target:self selector:@selector(_closeItemDidClicked) textType:YES];
  312. }
  313. return _closeItem;
  314. }
  315. @end