2023-11-20 21:29:45 -08:00
|
|
|
|
//
|
|
|
|
|
// YMWebViewController.m
|
|
|
|
|
// mew-ios
|
|
|
|
|
//
|
|
|
|
|
// Created by 触海 on 2023/11/9.
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
#import "YMWebViewController.h"
|
2023-11-23 14:43:42 -08:00
|
|
|
|
/// Tool
|
|
|
|
|
#import "ApiHost.h"
|
|
|
|
|
#import "YYUtility.h"
|
|
|
|
|
#import "AccountInfoStorage.h"
|
|
|
|
|
#import "ThemeColor.h"
|
2023-11-24 15:17:43 -08:00
|
|
|
|
#import "YMMacro.h"
|
|
|
|
|
#import "NewEncryptTool.h"
|
2023-11-23 14:43:42 -08:00
|
|
|
|
/// Third
|
|
|
|
|
#import <Masonry/Masonry.h>
|
2023-11-20 21:29:45 -08:00
|
|
|
|
|
|
|
|
|
|
2023-11-24 15:17:43 -08:00
|
|
|
|
//#ifdef DEBUG
|
|
|
|
|
//static const NSString *CompanyFirstDomainByWeChatRegister = @"api.uat.lecheng163.com";
|
|
|
|
|
//#else
|
|
|
|
|
//static const NSString *CompanyFirstDomainByWeChatRegister = @"api.lecheng163.com";
|
|
|
|
|
//#endif
|
2023-11-23 14:43:42 -08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@interface WeakWebViewScriptMessageDelegate : NSObject<WKScriptMessageHandler>
|
|
|
|
|
|
|
|
|
|
//WKScriptMessageHandler 这个协议类专门用来处理JavaScript调用原生OC的方法
|
|
|
|
|
@property (nonatomic, weak) id<WKScriptMessageHandler> scriptDelegate;
|
|
|
|
|
|
|
|
|
|
- (instancetype)initWithDelegate:(id<WKScriptMessageHandler>)scriptDelegate;
|
|
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
@implementation WeakWebViewScriptMessageDelegate
|
|
|
|
|
|
|
|
|
|
- (instancetype)initWithDelegate:(id<WKScriptMessageHandler>)scriptDelegate {
|
|
|
|
|
self = [super init];
|
|
|
|
|
if (self) {
|
|
|
|
|
_scriptDelegate = scriptDelegate;
|
|
|
|
|
}
|
|
|
|
|
return self;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//遵循WKScriptMessageHandler协议,必须实现如下方法,然后把方法向外传递
|
|
|
|
|
//通过接收JS传出消息的name进行捕捉的回调方法
|
|
|
|
|
- (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message {
|
|
|
|
|
|
|
|
|
|
if ([self.scriptDelegate respondsToSelector:@selector(userContentController:didReceiveScriptMessage:)]) {
|
|
|
|
|
[self.scriptDelegate userContentController:userContentController didReceiveScriptMessage:message];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
NSString * const kJSGetUid = @"getUid";
|
|
|
|
|
@interface YMWebViewController ()<WKNavigationDelegate, WKScriptMessageHandler>
|
|
|
|
|
@property (strong, nonatomic) WKWebView *webview;
|
2023-11-20 21:29:45 -08:00
|
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
|
|
@implementation YMWebViewController
|
|
|
|
|
|
|
|
|
|
- (void)viewDidLoad {
|
|
|
|
|
[super viewDidLoad];
|
|
|
|
|
// Do any additional setup after loading the view.
|
2023-11-23 14:43:42 -08:00
|
|
|
|
[self initView];
|
2023-11-24 15:17:43 -08:00
|
|
|
|
NSString *urlString = [NSString stringWithFormat:@"%@/%@", [NewEncryptTool MEW_aesDecrypt: API_HOST_H5_URL], self.url];
|
|
|
|
|
[self.webview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:urlString]]];
|
2023-11-23 14:43:42 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
- (void)backButtonClick {
|
|
|
|
|
BOOL canGoBack = YES;
|
|
|
|
|
if (self.webview.backForwardList.backList.count <= 1) {
|
|
|
|
|
canGoBack = NO;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ([self.webview canGoBack]) {
|
|
|
|
|
[self.webview goBack];
|
|
|
|
|
} else {
|
|
|
|
|
[self.navigationController popViewControllerAnimated:YES];
|
|
|
|
|
// [self.userContentController removeAllUserScripts];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-20 21:29:45 -08:00
|
|
|
|
|
2023-11-23 14:43:42 -08:00
|
|
|
|
#pragma mark - InitView
|
|
|
|
|
- (void)initView {
|
|
|
|
|
if (@available(iOS 11.0, *)) {
|
|
|
|
|
self.webview.scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
|
|
|
|
|
}else{
|
|
|
|
|
self.automaticallyAdjustsScrollViewInsets = NO;
|
|
|
|
|
}
|
|
|
|
|
[self.view addSubview:self.webview];
|
|
|
|
|
[self.webview mas_makeConstraints:^(MASConstraintMaker *make) {
|
|
|
|
|
make.left.right.top.bottom.mas_equalTo(self.view);
|
|
|
|
|
}];
|
|
|
|
|
|
|
|
|
|
|
2023-11-20 21:29:45 -08:00
|
|
|
|
}
|
|
|
|
|
|
2023-11-23 14:43:42 -08:00
|
|
|
|
#pragma mark - Set
|
|
|
|
|
- (void)setUrl:(NSString *)urlString{
|
|
|
|
|
_url = urlString;
|
|
|
|
|
if (_url == nil) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#pragma mark - Get
|
|
|
|
|
|
|
|
|
|
- (WKWebView *)webview {
|
|
|
|
|
if (_webview == nil) {
|
|
|
|
|
WKWebViewConfiguration *configuration = [[WKWebViewConfiguration alloc]init];
|
|
|
|
|
|
|
|
|
|
if (@available(iOS 10.0, *)) {
|
|
|
|
|
configuration.mediaTypesRequiringUserActionForPlayback = NO;
|
|
|
|
|
} else {
|
|
|
|
|
// Fallback on earlier versions
|
|
|
|
|
}
|
2023-11-24 15:17:43 -08:00
|
|
|
|
|
|
|
|
|
_webview = [[WKWebView alloc]initWithFrame:CGRectMake(0, 0, KScreenWidth, KScreenHeight)];
|
2023-11-23 14:43:42 -08:00
|
|
|
|
|
|
|
|
|
UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(backButtonClick)];
|
|
|
|
|
[_webview addGestureRecognizer:swipeGesture];
|
|
|
|
|
|
|
|
|
|
[_webview.scrollView setShowsVerticalScrollIndicator:NO];
|
|
|
|
|
[_webview.scrollView setShowsHorizontalScrollIndicator:NO];
|
|
|
|
|
_webview.scrollView.bounces = NO;
|
|
|
|
|
}
|
|
|
|
|
return _webview;
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-24 15:17:43 -08:00
|
|
|
|
|
2023-11-20 21:29:45 -08:00
|
|
|
|
@end
|