Files
yinmeng-ios-store/yinmeng-ios/yinmeng-ios/Main/YMWebViewController.m

140 lines
3.9 KiB
Mathematica
Raw Normal View History

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"
#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
//#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 JavaScriptOC
@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
//JSname
- (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];
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
}
_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-20 21:29:45 -08:00
@end