YRWebVC.m 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. //
  2. // YRWebVC.m
  3. // UU_Ent
  4. //
  5. // Created by liujl on 2019/4/29.
  6. // Copyright © 2019 UAS. All rights reserved.
  7. //
  8. #import "YRWebVC.h"
  9. #import "AppDelegate.h"
  10. #import "UIDevice+extension.h"
  11. #import <WebKit/WebKit.h>
  12. #import <JavaScriptCore/JavaScriptCore.h>
  13. @interface YRWebVC ()<WKUIDelegate,WKNavigationDelegate,WKScriptMessageHandler,UINavigationControllerDelegate>
  14. @property(strong,nonatomic)UIProgressView *progress;
  15. @property(strong,nonatomic)WKWebView *webView;
  16. @end
  17. @implementation YRWebVC
  18. - (void)viewDidLoad {
  19. [super viewDidLoad];
  20. }
  21. -(void)createUI{
  22. //创建配置对象
  23. WKWebViewConfiguration *webConfig = [[WKWebViewConfiguration alloc]init];
  24. //为配置对象设置偏好设置
  25. WKPreferences *preferences = [[WKPreferences alloc]init];
  26. webConfig.preferences = preferences;
  27. //允许js和native交互
  28. preferences.javaScriptEnabled = YES;
  29. WKUserContentController *userContent = [[WKUserContentController alloc]init];
  30. [userContent addScriptMessageHandler:self name:@"event"];
  31. //监听相应的事件
  32. webConfig.userContentController = userContent;
  33. self.webView = [[WKWebView alloc]initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height) configuration:webConfig];
  34. [self.view addSubview:self.webView];
  35. [self.webView mas_makeConstraints:^(MASConstraintMaker *make) {
  36. make.edges.equalTo(self.view);
  37. }];
  38. self.webView.navigationDelegate = self;
  39. NSURL *url = [NSURL URLWithString:self.url];
  40. NSURLRequest *request = [NSURLRequest requestWithURL:url];
  41. [self.webView loadRequest:request];
  42. self.progress = [UIProgressView new];
  43. self.progress.trackTintColor = self.view.backgroundColor;
  44. self.progress.progressTintColor = [UIColor orangeColor];
  45. [self.view addSubview:self.progress];
  46. [self.progress mas_makeConstraints:^(MASConstraintMaker *make) {
  47. make.top.left.right.equalTo(self.view);
  48. make.height.mas_equalTo(2);
  49. }];
  50. [self.webView addObserver:self forKeyPath:@"estimatedProgress" options:NSKeyValueObservingOptionNew context:nil];
  51. }
  52. -(void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation{
  53. [self.webView evaluateJavaScript:@"document.title" completionHandler:^(NSString * _Nullable title , NSError * _Nullable error) {
  54. self.title = title;
  55. }];
  56. }
  57. //接受js发送的消息
  58. -(void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message{
  59. NSString *name = message.name;
  60. if ([name isEqualToString:@"event"]) {
  61. // 打开相册
  62. if ([message.body isEqualToString:@"ChartInterfaceOrientationMaskPortrait"]) {//横屏
  63. [self chartInterfaceOrientationMaskLandscapeRight];
  64. }else {//竖屏
  65. [self chartInterfaceOrientationMaskPortrait];
  66. }
  67. }
  68. }
  69. //横屏
  70. -(void)chartInterfaceOrientationMaskLandscapeRight{
  71. AppDelegate * appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  72. //允许转成横屏
  73. appDelegate.allowRotation = YES;
  74. //调用横屏代码
  75. [UIDevice switchNewOrientation:UIInterfaceOrientationLandscapeRight];
  76. [self.navigationController setNavigationBarHidden:YES animated:YES];
  77. }
  78. //竖屏
  79. -(void)chartInterfaceOrientationMaskPortrait{
  80. AppDelegate * appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  81. //允许转成横屏
  82. appDelegate.allowRotation = NO;
  83. //调用横屏代码
  84. [UIDevice switchNewOrientation:UIInterfaceOrientationPortrait];
  85. [self.navigationController setNavigationBarHidden:NO animated:YES];
  86. }
  87. -(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context{
  88. if ([keyPath isEqualToString:@"estimatedProgress"]) {
  89. self.progress.progress = self.webView.estimatedProgress;
  90. if (self.webView.estimatedProgress >= 1.0f) {
  91. [UIView animateWithDuration:0.5f animations:^{
  92. self.progress.alpha = 0.0f;
  93. self.progress.progress = 0.0f;
  94. }];
  95. }else{
  96. self.progress.alpha = 1.0;
  97. }
  98. }
  99. }
  100. -(void)viewDidDisappear:(BOOL)animated{
  101. [super viewDidDisappear:animated];
  102. if (self.backBlock) {
  103. self.backBlock();
  104. }
  105. }
  106. -(void)removeAllMsgHandler{
  107. [self.webView.configuration.userContentController removeScriptMessageHandlerForName:@"event"];
  108. }
  109. -(void)dealloc{
  110. [self.webView removeObserver:self forKeyPath:@"estimatedProgress"];
  111. }
  112. @end