| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254 |
- //
- // CloudHelpView.m
- // StudentDaya
- //
- // Created by Kyle on 2021/12/15.
- // Copyright © 2021 DayaMusic. All rights reserved.
- //
- #import "CloudHelpView.h"
- #import <WebKit/WebKit.h>
- #import "WeakWebViewScriptMessageDelegate.h"
- @interface CloudHelpView ()
- @property (weak, nonatomic) IBOutlet UIButton *guideButton;
- @property (weak, nonatomic) IBOutlet UIView *backView;
- @property (weak, nonatomic) IBOutlet UIButton *helpButton;
- @property (weak, nonatomic) IBOutlet UIView *lineView;
- @property (weak, nonatomic) IBOutlet UIView *baseView;
- @property (weak, nonatomic) IBOutlet NSLayoutConstraint *lineViewLeft;
- @property (nonatomic, strong) UIScrollView *baseScrollView;
- @property (nonatomic, strong) WKWebView *guideWebView;
- @property (nonatomic, strong) WKWebView *helpWebView;
- @property (nonatomic, strong) UIButton *feedbackButton;
- @property (nonatomic, strong) CloudFeedbackAction callback;
- @end
- @implementation CloudHelpView
- + (instancetype)shareInstance {
- CloudHelpView *view = [[[NSBundle mainBundle] loadNibNamed:@"CloudHelpView" owner:nil options:nil] firstObject];
- [view configHelpView];
- return view;
- }
- - (void)configHelpView {
-
- if (@available(iOS 11.0, *)) {
- self.backView.layer.cornerRadius = 14;
- self.backView.layer.maskedCorners = kCALayerMinXMinYCorner | kCALayerMinXMaxYCorner; // 左上圆角
- }
- else {
- UIBezierPath * path = [UIBezierPath bezierPathWithRoundedRect:self.backView.bounds byRoundingCorners:UIRectCornerTopLeft | UIRectCornerBottomLeft cornerRadii:CGSizeMake(14, 14)];
- CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
- maskLayer.frame = self.backView.bounds;
- maskLayer.path = path.CGPath;
- self.backView.layer.mask = maskLayer;
- }
- [self.baseView addSubview:self.baseScrollView];
- [self.baseScrollView addSubview:self.guideWebView];
- [self.baseScrollView addSubview:self.helpWebView];
- [self setUserAgent];
- [self.baseScrollView addSubview:self.feedbackButton];
- }
- - (void)setUserAgent {
-
- MJWeakSelf;
- [self.guideWebView evaluateJavaScript:@"navigator.userAgent" completionHandler:^(id result, NSError *error) {
- NSString *oldUserAgent = result;
- NSString *newUserAgent = [NSString stringWithFormat:@"%@ %@",oldUserAgent,@"DAYAAPPI"];
- weakSelf.guideWebView.customUserAgent = newUserAgent;
- [weakSelf loadRequestIsGuide:YES];
- }];
- [self.helpWebView evaluateJavaScript:@"navigator.userAgent" completionHandler:^(id result, NSError *error) {
- NSString *oldUserAgent = result;
- NSString *newUserAgent = [NSString stringWithFormat:@"%@ %@",oldUserAgent,@"DAYAAPPI"];
- weakSelf.helpWebView.customUserAgent = newUserAgent;
- [weakSelf loadRequestIsGuide:NO];;
- }];
- }
- - (void)loadRequestIsGuide:(BOOL)isGuide {
- MJWeakSelf;
- if (isGuide) {
- [self.guideWebView evaluateJavaScript:@"navigator.userAgent" completionHandler:^(id _Nullable result, NSError * _Nullable error) {
- NSLog(@"%@",result);
- NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:[weakSelf getUrlIsGuide:YES]] cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:60.0f];
- [weakSelf.guideWebView loadRequest:request];
- }];
- }
- else {
- [self.helpWebView evaluateJavaScript:@"navigator.userAgent" completionHandler:^(id _Nullable result, NSError * _Nullable error) {
- NSLog(@"%@",result);
- NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:[weakSelf getUrlIsGuide:NO]] cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:60.0f];
- [weakSelf.helpWebView loadRequest:request];
- }];
- }
- }
- - (NSString *)getUrlIsGuide:(BOOL)isGuide {
- NSString *tokenStr = UserDefault(TokenKey);
- NSString *token = nil;
- if (![NSString isEmptyString:tokenStr]) {
- token = [[NSString stringWithFormat:@"Authorization=%@ %@", UserDefault(Token_type), tokenStr] stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
- }
- if (isGuide) {
- NSString *url = @"https://mteaonline.dayaedu.com/#/guide";
- if (![NSString isEmptyString:token]) {
- url = [NSString stringWithFormat:@"%@?%@",url,token];
- }
- return url;
- }
- else {
- NSString *url = @"https://mstuonline.dayaedu.com/#/KeepRepaire?mode=accompany";
- if (![NSString isEmptyString:token]) {
- url = [NSString stringWithFormat:@"%@&%@",url,token];
- }
- return url;
- }
- }
- - (void)feedbackCallback:(CloudFeedbackAction)callback {
- if (callback) {
- self.callback = callback;
- }
- }
- - (IBAction)guideAction:(id)sender { // 投屏
- [self configGuideChoose:YES];
- }
- - (IBAction)helpAction:(id)sender { // 帮助
- [self configGuideChoose:NO];
- }
- - (void)configGuideChoose:(BOOL)isChooseSettingButton {
- CGFloat positionX = 0.0f;
- NSInteger pageIndex = 0;
- if (isChooseSettingButton) {
- [self.guideButton setTitleColor:THEMECOLOR forState:UIControlStateNormal];
- [self.helpButton setTitleColor:HexRGB(0x1a1a1a) forState:UIControlStateNormal];
- positionX = 65.0f;
- pageIndex = 0;
- }
- else {
- [self.guideButton setTitleColor:HexRGB(0x1a1a1a) forState:UIControlStateNormal];
- [self.helpButton setTitleColor:THEMECOLOR forState:UIControlStateNormal];
- positionX = 225.0f;
- pageIndex = 1;
- }
- [UIView animateWithDuration:0.5f animations:^{
- self.lineViewLeft.constant = positionX;
- [self.baseScrollView setContentOffset:CGPointMake(pageIndex*320, 0)];
- }];
- }
- - (IBAction)hideButtonAction:(id)sender {
- [self removeFromSuperview];
- }
- - (void)feedbackButtonAction {
- if (self.callback) {
- self.callback();
- }
- }
- #pragma mark ---- lazying
- - (UIScrollView *)baseScrollView {
- if (!_baseScrollView) {
- CGFloat height = KLandscapeHeight - 55;
- _baseScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, height)];
- [_baseScrollView setContentSize:CGSizeMake(CGRectGetHeight(self.baseView.frame)*2, height)];
- _baseScrollView.scrollEnabled = NO;
- }
- return _baseScrollView;
- }
- - (WKWebView *)guideWebView {
- if (!_guideWebView) {
- _guideWebView = [[WKWebView alloc] initWithFrame:CGRectMake(0, 0, 320, KLandscapeHeight) configuration:[self getwebConfig]];
- _guideWebView.scrollView.bounces = NO;
- }
- return _guideWebView;
- }
- - (WKWebView *)helpWebView {
- if (!_helpWebView) {
- _helpWebView = [[WKWebView alloc] initWithFrame:CGRectMake(320, 0, 320, KLandscapeHeight - 55) configuration:[self getwebConfig]];
- _helpWebView.scrollView.bounces = NO;
- }
- return _helpWebView;
- }
- - (WKWebViewConfiguration *)getwebConfig {
- WKWebViewConfiguration *config = [[WKWebViewConfiguration alloc] init];
- config.selectionGranularity = WKSelectionGranularityDynamic;
- config.allowsInlineMediaPlayback = YES;
- if (@available(iOS 10.0, *)) {
- config.mediaTypesRequiringUserActionForPlayback = NO;
- } else {
- // Fallback on earlier versions
- config.requiresUserActionForMediaPlayback = NO;
- }
- config.processPool = [CloudHelpView singleWkProcessPool];
- config.websiteDataStore = [WKWebsiteDataStore defaultDataStore];
- //自定义的WKScriptMessageHandler 是为了解决内存不释放的问题
- WeakWebViewScriptMessageDelegate *weakScriptMessageDelegate = [[WeakWebViewScriptMessageDelegate alloc] initWithDelegate:self];
- //这个类主要用来做native与JavaScript的交互管理
- WKUserContentController * wkUController = [[WKUserContentController alloc] init];
- [wkUController addScriptMessageHandler:weakScriptMessageDelegate name:@"DAYA"];
- config.userContentController = wkUController;
-
- WKPreferences *preferences = [WKPreferences new];
- // 是否支出javaScript
- preferences.javaScriptEnabled = YES;
- //不通过用户交互,是否可以打开窗口
- preferences.javaScriptCanOpenWindowsAutomatically = YES;
- config.preferences = preferences;
- return config;
- }
- - (UIButton *)feedbackButton {
- if (!_feedbackButton) {
- _feedbackButton = [UIButton buttonWithType:UIButtonTypeCustom];
- _feedbackButton.frame = CGRectMake(320, KLandscapeHeight - 55 - 55, 320, 55);
- [_feedbackButton setTitle:@"意见反馈 >" forState:UIControlStateNormal];
- [_feedbackButton setTitleColor:THEMECOLOR forState:UIControlStateNormal];
- [_feedbackButton.titleLabel setFont:[UIFont systemFontOfSize:14.0f]];
- [_feedbackButton addTarget:self action:@selector(feedbackButtonAction) forControlEvents:UIControlEventTouchUpInside];
- }
- return _feedbackButton;
- }
- + (WKProcessPool*)singleWkProcessPool {
- static WKProcessPool *sharedPool;
- static dispatch_once_t onceToken;
- dispatch_once(&onceToken, ^{
- sharedPool = [[WKProcessPool alloc] init];
- });
- return sharedPool;
- }
- /*
- // Only override drawRect: if you perform custom drawing.
- // An empty implementation adversely affects performance during animation.
- - (void)drawRect:(CGRect)rect {
- // Drawing code
- }
- */
- @end
|