CloudHelpView.m 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. //
  2. // CloudHelpView.m
  3. // StudentDaya
  4. //
  5. // Created by Kyle on 2021/12/15.
  6. // Copyright © 2021 DayaMusic. All rights reserved.
  7. //
  8. #import "CloudHelpView.h"
  9. #import <WebKit/WebKit.h>
  10. #import "WeakWebViewScriptMessageDelegate.h"
  11. @interface CloudHelpView ()
  12. @property (weak, nonatomic) IBOutlet UIButton *guideButton;
  13. @property (weak, nonatomic) IBOutlet UIView *backView;
  14. @property (weak, nonatomic) IBOutlet UIButton *helpButton;
  15. @property (weak, nonatomic) IBOutlet UIView *lineView;
  16. @property (weak, nonatomic) IBOutlet UIView *baseView;
  17. @property (weak, nonatomic) IBOutlet NSLayoutConstraint *lineViewLeft;
  18. @property (nonatomic, strong) UIScrollView *baseScrollView;
  19. @property (nonatomic, strong) WKWebView *guideWebView;
  20. @property (nonatomic, strong) WKWebView *helpWebView;
  21. @property (nonatomic, strong) UIButton *feedbackButton;
  22. @property (nonatomic, strong) CloudFeedbackAction callback;
  23. @end
  24. @implementation CloudHelpView
  25. + (instancetype)shareInstance {
  26. CloudHelpView *view = [[[NSBundle mainBundle] loadNibNamed:@"CloudHelpView" owner:nil options:nil] firstObject];
  27. [view configHelpView];
  28. return view;
  29. }
  30. - (void)configHelpView {
  31. if (@available(iOS 11.0, *)) {
  32. self.backView.layer.cornerRadius = 14;
  33. self.backView.layer.maskedCorners = kCALayerMinXMinYCorner | kCALayerMinXMaxYCorner; // 左上圆角
  34. }
  35. else {
  36. UIBezierPath * path = [UIBezierPath bezierPathWithRoundedRect:self.backView.bounds byRoundingCorners:UIRectCornerTopLeft | UIRectCornerBottomLeft cornerRadii:CGSizeMake(14, 14)];
  37. CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
  38. maskLayer.frame = self.backView.bounds;
  39. maskLayer.path = path.CGPath;
  40. self.backView.layer.mask = maskLayer;
  41. }
  42. [self.baseView addSubview:self.baseScrollView];
  43. [self.baseScrollView addSubview:self.guideWebView];
  44. [self.baseScrollView addSubview:self.helpWebView];
  45. [self setUserAgent];
  46. [self.baseScrollView addSubview:self.feedbackButton];
  47. }
  48. - (void)setUserAgent {
  49. MJWeakSelf;
  50. [self.guideWebView evaluateJavaScript:@"navigator.userAgent" completionHandler:^(id result, NSError *error) {
  51. NSString *oldUserAgent = result;
  52. NSString *newUserAgent = [NSString stringWithFormat:@"%@ %@",oldUserAgent,@"DAYAAPPI"];
  53. weakSelf.guideWebView.customUserAgent = newUserAgent;
  54. [weakSelf loadRequestIsGuide:YES];
  55. }];
  56. [self.helpWebView evaluateJavaScript:@"navigator.userAgent" completionHandler:^(id result, NSError *error) {
  57. NSString *oldUserAgent = result;
  58. NSString *newUserAgent = [NSString stringWithFormat:@"%@ %@",oldUserAgent,@"DAYAAPPI"];
  59. weakSelf.helpWebView.customUserAgent = newUserAgent;
  60. [weakSelf loadRequestIsGuide:NO];;
  61. }];
  62. }
  63. - (void)loadRequestIsGuide:(BOOL)isGuide {
  64. MJWeakSelf;
  65. if (isGuide) {
  66. [self.guideWebView evaluateJavaScript:@"navigator.userAgent" completionHandler:^(id _Nullable result, NSError * _Nullable error) {
  67. NSLog(@"%@",result);
  68. NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:[weakSelf getUrlIsGuide:YES]] cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:60.0f];
  69. [weakSelf.guideWebView loadRequest:request];
  70. }];
  71. }
  72. else {
  73. [self.helpWebView evaluateJavaScript:@"navigator.userAgent" completionHandler:^(id _Nullable result, NSError * _Nullable error) {
  74. NSLog(@"%@",result);
  75. NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:[weakSelf getUrlIsGuide:NO]] cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:60.0f];
  76. [weakSelf.helpWebView loadRequest:request];
  77. }];
  78. }
  79. }
  80. - (NSString *)getUrlIsGuide:(BOOL)isGuide {
  81. NSString *tokenStr = UserDefault(TokenKey);
  82. NSString *token = nil;
  83. if (![NSString isEmptyString:tokenStr]) {
  84. token = [[NSString stringWithFormat:@"Authorization=%@ %@", UserDefault(Token_type), tokenStr] stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
  85. }
  86. if (isGuide) {
  87. NSString *url = @"https://mteaonline.dayaedu.com/#/guide";
  88. if (![NSString isEmptyString:token]) {
  89. url = [NSString stringWithFormat:@"%@?%@",url,token];
  90. }
  91. return url;
  92. }
  93. else {
  94. NSString *url = @"https://mstuonline.dayaedu.com/#/KeepRepaire?mode=accompany";
  95. if (![NSString isEmptyString:token]) {
  96. url = [NSString stringWithFormat:@"%@&%@",url,token];
  97. }
  98. return url;
  99. }
  100. }
  101. - (void)feedbackCallback:(CloudFeedbackAction)callback {
  102. if (callback) {
  103. self.callback = callback;
  104. }
  105. }
  106. - (IBAction)guideAction:(id)sender { // 投屏
  107. [self configGuideChoose:YES];
  108. }
  109. - (IBAction)helpAction:(id)sender { // 帮助
  110. [self configGuideChoose:NO];
  111. }
  112. - (void)configGuideChoose:(BOOL)isChooseSettingButton {
  113. CGFloat positionX = 0.0f;
  114. NSInteger pageIndex = 0;
  115. if (isChooseSettingButton) {
  116. [self.guideButton setTitleColor:THEMECOLOR forState:UIControlStateNormal];
  117. [self.helpButton setTitleColor:HexRGB(0x1a1a1a) forState:UIControlStateNormal];
  118. positionX = 65.0f;
  119. pageIndex = 0;
  120. }
  121. else {
  122. [self.guideButton setTitleColor:HexRGB(0x1a1a1a) forState:UIControlStateNormal];
  123. [self.helpButton setTitleColor:THEMECOLOR forState:UIControlStateNormal];
  124. positionX = 225.0f;
  125. pageIndex = 1;
  126. }
  127. [UIView animateWithDuration:0.5f animations:^{
  128. self.lineViewLeft.constant = positionX;
  129. [self.baseScrollView setContentOffset:CGPointMake(pageIndex*320, 0)];
  130. }];
  131. }
  132. - (IBAction)hideButtonAction:(id)sender {
  133. [self removeFromSuperview];
  134. }
  135. - (void)feedbackButtonAction {
  136. if (self.callback) {
  137. self.callback();
  138. }
  139. }
  140. #pragma mark ---- lazying
  141. - (UIScrollView *)baseScrollView {
  142. if (!_baseScrollView) {
  143. CGFloat height = KLandscapeHeight - 55;
  144. _baseScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, height)];
  145. [_baseScrollView setContentSize:CGSizeMake(CGRectGetHeight(self.baseView.frame)*2, height)];
  146. _baseScrollView.scrollEnabled = NO;
  147. }
  148. return _baseScrollView;
  149. }
  150. - (WKWebView *)guideWebView {
  151. if (!_guideWebView) {
  152. _guideWebView = [[WKWebView alloc] initWithFrame:CGRectMake(0, 0, 320, KLandscapeHeight) configuration:[self getwebConfig]];
  153. _guideWebView.scrollView.bounces = NO;
  154. }
  155. return _guideWebView;
  156. }
  157. - (WKWebView *)helpWebView {
  158. if (!_helpWebView) {
  159. _helpWebView = [[WKWebView alloc] initWithFrame:CGRectMake(320, 0, 320, KLandscapeHeight - 55) configuration:[self getwebConfig]];
  160. _helpWebView.scrollView.bounces = NO;
  161. }
  162. return _helpWebView;
  163. }
  164. - (WKWebViewConfiguration *)getwebConfig {
  165. WKWebViewConfiguration *config = [[WKWebViewConfiguration alloc] init];
  166. config.selectionGranularity = WKSelectionGranularityDynamic;
  167. config.allowsInlineMediaPlayback = YES;
  168. if (@available(iOS 10.0, *)) {
  169. config.mediaTypesRequiringUserActionForPlayback = NO;
  170. } else {
  171. // Fallback on earlier versions
  172. config.requiresUserActionForMediaPlayback = NO;
  173. }
  174. config.processPool = [CloudHelpView singleWkProcessPool];
  175. config.websiteDataStore = [WKWebsiteDataStore defaultDataStore];
  176. //自定义的WKScriptMessageHandler 是为了解决内存不释放的问题
  177. WeakWebViewScriptMessageDelegate *weakScriptMessageDelegate = [[WeakWebViewScriptMessageDelegate alloc] initWithDelegate:self];
  178. //这个类主要用来做native与JavaScript的交互管理
  179. WKUserContentController * wkUController = [[WKUserContentController alloc] init];
  180. [wkUController addScriptMessageHandler:weakScriptMessageDelegate name:@"DAYA"];
  181. config.userContentController = wkUController;
  182. WKPreferences *preferences = [WKPreferences new];
  183. // 是否支出javaScript
  184. preferences.javaScriptEnabled = YES;
  185. //不通过用户交互,是否可以打开窗口
  186. preferences.javaScriptCanOpenWindowsAutomatically = YES;
  187. config.preferences = preferences;
  188. return config;
  189. }
  190. - (UIButton *)feedbackButton {
  191. if (!_feedbackButton) {
  192. _feedbackButton = [UIButton buttonWithType:UIButtonTypeCustom];
  193. _feedbackButton.frame = CGRectMake(320, KLandscapeHeight - 55 - 55, 320, 55);
  194. [_feedbackButton setTitle:@"意见反馈 >" forState:UIControlStateNormal];
  195. [_feedbackButton setTitleColor:THEMECOLOR forState:UIControlStateNormal];
  196. [_feedbackButton.titleLabel setFont:[UIFont systemFontOfSize:14.0f]];
  197. [_feedbackButton addTarget:self action:@selector(feedbackButtonAction) forControlEvents:UIControlEventTouchUpInside];
  198. }
  199. return _feedbackButton;
  200. }
  201. + (WKProcessPool*)singleWkProcessPool {
  202. static WKProcessPool *sharedPool;
  203. static dispatch_once_t onceToken;
  204. dispatch_once(&onceToken, ^{
  205. sharedPool = [[WKProcessPool alloc] init];
  206. });
  207. return sharedPool;
  208. }
  209. /*
  210. // Only override drawRect: if you perform custom drawing.
  211. // An empty implementation adversely affects performance during animation.
  212. - (void)drawRect:(CGRect)rect {
  213. // Drawing code
  214. }
  215. */
  216. @end