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

140 lines
3.9 KiB
Objective-C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//
// YMWebViewController.m
// mew-ios
//
// Created by 触海 on 2023/11/9.
//
#import "YMWebViewController.h"
/// Tool
#import "ApiHost.h"
#import "YYUtility.h"
#import "AccountInfoStorage.h"
#import "ThemeColor.h"
#import "YMMacro.h"
#import "NewEncryptTool.h"
/// Third
#import <Masonry/Masonry.h>
//#ifdef DEBUG
//static const NSString *CompanyFirstDomainByWeChatRegister = @"api.uat.lecheng163.com";
//#else
//static const NSString *CompanyFirstDomainByWeChatRegister = @"api.lecheng163.com";
//#endif
@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;
@end
@implementation YMWebViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
[self initView];
NSString *urlString = [NSString stringWithFormat:@"%@/%@", [NewEncryptTool MEW_aesDecrypt: API_HOST_H5_URL], self.url];
[self.webview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:urlString]]];
}
- (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];
}
}
#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);
}];
}
#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)];
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;
}
@end