RDSControlWebVC.m 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. //
  2. // RDSControlWebVC.m
  3. // Temperature
  4. //
  5. // Created by Kevin on 2023/12/6.
  6. #import "RDSControlWebVC.h"
  7. #import <WebViewJavascriptBridge/WebViewJavascriptBridge.h>
  8. #import <WebKit/WebKit.h>
  9. @interface RDSControlWebVC () <WKUIDelegate, WKNavigationDelegate>
  10. @property (nonatomic, strong) WKWebView *webView;
  11. @property (nonatomic, strong) WebViewJavascriptBridge *bridge;
  12. @property (nonatomic, copy) NSString *requestURL;
  13. @end
  14. @implementation RDSControlWebVC
  15. - (instancetype)initWithRecordId:(NSString *)recordId code:(NSString *)code {
  16. self = [super init];
  17. if (self) {
  18. self.requestURL = [NSString stringWithFormat:@"https://app.yongxulvjian.com/#/deviceManage/%@/%@", recordId, code];
  19. }
  20. return self;
  21. }
  22. - (void)viewDidLoad {
  23. [super viewDidLoad];
  24. // Do any additional setup after loading the view.
  25. [self configSubviews];
  26. [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:self.requestURL]]];
  27. [RDSHudShower showWithStatus:nil];
  28. }
  29. - (void)viewWillAppear:(BOOL)animated {
  30. [super viewWillAppear:animated];
  31. [self.navigationController setNavigationBarHidden:YES];
  32. }
  33. - (void)viewWillDisappear:(BOOL)animated {
  34. [super viewWillDisappear:animated];
  35. [self.navigationController setNavigationBarHidden:NO];
  36. }
  37. - (void)configSubviews {
  38. [self.view addSubview:self.webView];
  39. [self.webView mas_makeConstraints:^(MASConstraintMaker *make) {
  40. make.left.right.equalTo(self.view);
  41. if (@available(iOS 11, *)) {
  42. make.top.equalTo(self.view.mas_safeAreaLayoutGuideTop);
  43. } else {
  44. make.top.equalTo(self.mas_topLayoutGuide);
  45. }
  46. if (@available(iOS 11.0, *)) {
  47. make.bottom.equalTo(self.view.mas_safeAreaLayoutGuideBottom);
  48. } else {
  49. make.bottom.equalTo(self.view.mas_bottom);
  50. }
  51. //make.edges.mas_equalTo(UIEdgeInsetsZero);
  52. }];
  53. }
  54. - (void)jsBridge {
  55. RDS_WEAKSELF(weakSelf)
  56. self.bridge = [WebViewJavascriptBridge bridgeForWebView:_webView];
  57. [self.bridge setWebViewDelegate:self];
  58. // js 回调
  59. [self.bridge registerHandler:@"closeWebView" handler:^(id data, WVJBResponseCallback responseCallback) {
  60. [weakSelf.navigationController popViewControllerAnimated:YES];
  61. }];
  62. }
  63. - (WKWebView *)webView {
  64. if (!_webView) {
  65. WKWebViewConfiguration *config = [[WKWebViewConfiguration alloc] init];
  66. config.userContentController = [[WKUserContentController alloc] init];
  67. config.allowsInlineMediaPlayback = YES;
  68. _webView = [[WKWebView alloc] initWithFrame:CGRectZero configuration:config];
  69. _webView.scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
  70. _webView.UIDelegate = self;
  71. _webView.navigationDelegate = self;
  72. [self jsBridge];
  73. }
  74. return _webView;
  75. }
  76. - (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation {
  77. [RDSHudShower dismissHUD];
  78. }
  79. - (void)webView:(WKWebView *)webView didFailNavigation:(WKNavigation *)navigation withError:(NSError *)error {
  80. [RDSHudShower dismissHUD];
  81. }
  82. @end