RDSUserProtocolViewController.m 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. //
  2. // RDSUserProtocolViewController.m
  3. // SmartLightForBigFish
  4. //
  5. // Created by coderYK on 2017/11/22.
  6. // Copyright © 2017年 RD. All rights reserved.
  7. //
  8. #import "RDSUserProtocolViewController.h"
  9. #import <WebKit/WebKit.h>
  10. @interface RDSUserProtocolViewController () <WKUIDelegate, WKNavigationDelegate, WKScriptMessageHandler>
  11. @property (nonatomic, strong) WKWebView *webView;
  12. @end
  13. @implementation RDSUserProtocolViewController
  14. - (void)dealloc {
  15. WKUserContentController *controller = self.webView.configuration.userContentController;
  16. [controller removeScriptMessageHandlerForName:@"closePage"];
  17. }
  18. - (void)viewDidLoad {
  19. [super viewDidLoad];
  20. self.title = _vcTitle;
  21. [self p_setupUI];
  22. [self p_loadData];
  23. }
  24. - (void)viewWillAppear:(BOOL)animated {
  25. [super viewWillAppear:animated];
  26. [self.navigationController setNavigationBarHidden:YES];
  27. }
  28. - (void)viewWillDisappear:(BOOL)animated {
  29. [super viewWillDisappear:animated];
  30. [self.navigationController setNavigationBarHidden:NO];
  31. }
  32. - (void)p_loadData {
  33. /* 加载服务器url的方法*/
  34. NSString *url = self.url;
  35. NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]];
  36. [_webView loadRequest:request];
  37. }
  38. - (void)p_setupUI {
  39. WKWebViewConfiguration *configuration = [[WKWebViewConfiguration alloc] init];
  40. [configuration.userContentController addScriptMessageHandler:self name:@"closePage"];
  41. self.webView = [[WKWebView alloc] initWithFrame:CGRectZero configuration:configuration];
  42. // 放在AppDelegate中全局修改
  43. // [_webView evaluateJavaScript:@"navigator.userAgent" completionHandler:^(id _Nullable result, NSError * _Nullable error) {
  44. // NSString *newUserAgent = [result stringByAppendingFormat:@"/%@", @"yongxulvjian"];
  45. // [[NSUserDefaults standardUserDefaults] registerDefaults:@{@"UserAgent":newUserAgent}];
  46. // }];
  47. [self.view addSubview:_webView];
  48. [_webView mas_makeConstraints:^(MASConstraintMaker *make) {
  49. // make.left.equalTo(self.view);
  50. // make.right.equalTo(self.view);
  51. // make.top.equalTo(self.view);
  52. // if (@available(iOS 11.0, *)) {
  53. // make.bottom.equalTo(self.view.mas_safeAreaLayoutGuideBottom);
  54. // } else {
  55. // make.bottom.equalTo(self.view.mas_bottom);
  56. // }
  57. make.edges.mas_equalTo(UIEdgeInsetsZero);
  58. }];
  59. _webView.UIDelegate = self;
  60. _webView.navigationDelegate = self;
  61. _webView.allowsBackForwardNavigationGestures = YES;
  62. }
  63. - (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message {
  64. if ([message.name isEqualToString:@"closePage"]) {
  65. [self.navigationController popViewControllerAnimated:YES];
  66. }
  67. }
  68. @end