From 2932fe03e3b7be8c9a77b9bf175b17c1ae04e320 Mon Sep 17 00:00:00 2001 From: zu Date: Thu, 16 Sep 2021 17:21:52 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0WKWebViewController?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- xplan-ios.xcodeproj/project.pbxproj | 12 + xplan-ios/Base/UI/BaseNavigationController.m | 5 + xplan-ios/Base/UI/BaseViewController.m | 1 - xplan-ios/Global/XPConstant.h | 12 +- xplan-ios/Global/XPHtmlUrl.h | 1 + xplan-ios/Global/XPHtmlUrl.m | 3 +- .../Main/Match/XplanFBFlutterViewContainer.h | 17 ++ .../Main/Match/XplanFBFlutterViewContainer.m | 22 ++ .../Main/Match/XplanFlutterBoostDelegate.m | 26 +- xplan-ios/Main/Tabbar/TabbarViewController.m | 16 +- xplan-ios/Main/XPWebViewController.h | 19 ++ xplan-ios/Main/XPWebViewController.m | 275 ++++++++++++++++++ 12 files changed, 383 insertions(+), 26 deletions(-) create mode 100644 xplan-ios/Main/Match/XplanFBFlutterViewContainer.h create mode 100644 xplan-ios/Main/Match/XplanFBFlutterViewContainer.m create mode 100644 xplan-ios/Main/XPWebViewController.h create mode 100644 xplan-ios/Main/XPWebViewController.m diff --git a/xplan-ios.xcodeproj/project.pbxproj b/xplan-ios.xcodeproj/project.pbxproj index 81ddba8c..6e44da63 100644 --- a/xplan-ios.xcodeproj/project.pbxproj +++ b/xplan-ios.xcodeproj/project.pbxproj @@ -33,6 +33,7 @@ 189DD75026E21D9000AB55B1 /* GCDHelper.m in Sources */ = {isa = PBXBuildFile; fileRef = 189DD74E26E21D9000AB55B1 /* GCDHelper.m */; }; 189DD75926E6003C00AB55B1 /* Api.m in Sources */ = {isa = PBXBuildFile; fileRef = 189DD75826E6003C00AB55B1 /* Api.m */; }; 189DD76226E60DDC00AB55B1 /* Api+Login.m in Sources */ = {isa = PBXBuildFile; fileRef = 189DD76126E60DDC00AB55B1 /* Api+Login.m */; }; + 18C17A5D26F338F300C48E11 /* XplanFBFlutterViewContainer.m in Sources */ = {isa = PBXBuildFile; fileRef = 18C17A5C26F338F300C48E11 /* XplanFBFlutterViewContainer.m */; }; 18E7B1B226E8AF980064BC9B /* MainPresenter.m in Sources */ = {isa = PBXBuildFile; fileRef = 18E7B1B126E8AF980064BC9B /* MainPresenter.m */; }; 18E7B1B726E8B2D10064BC9B /* Api+Main.m in Sources */ = {isa = PBXBuildFile; fileRef = 18E7B1B626E8B2D10064BC9B /* Api+Main.m */; }; 18E7B22626E8CDCF0064BC9B /* XplanFlutterBoostDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = 18E7B22326E8CDCF0064BC9B /* XplanFlutterBoostDelegate.m */; }; @@ -59,6 +60,7 @@ 18E7B31B26F0982E0064BC9B /* UserExpand.m in Sources */ = {isa = PBXBuildFile; fileRef = 18E7B31A26F0982E0064BC9B /* UserExpand.m */; }; 18E7B31E26F0984C0064BC9B /* UserLevelVo.m in Sources */ = {isa = PBXBuildFile; fileRef = 18E7B31D26F0984C0064BC9B /* UserLevelVo.m */; }; 18E7B32126F098650064BC9B /* UserInfoSkillVo.m in Sources */ = {isa = PBXBuildFile; fileRef = 18E7B32026F098650064BC9B /* UserInfoSkillVo.m */; }; + 18E7B33226F317A20064BC9B /* XPWebViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 18E7B33126F317A20064BC9B /* XPWebViewController.m */; }; 73FFADDC93E195344047A2EC /* Pods_xplan_ios.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = CACF623970097D653132D69A /* Pods_xplan_ios.framework */; }; 9B0E1C5926E77022005D4442 /* BaseNavigationController.m in Sources */ = {isa = PBXBuildFile; fileRef = 9B0E1C5826E77022005D4442 /* BaseNavigationController.m */; }; E81366E326F0A1FC0076364C /* LoginBindPhoneViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = E81366E226F0A1FC0076364C /* LoginBindPhoneViewController.m */; }; @@ -168,6 +170,8 @@ 189DD76026E60DDC00AB55B1 /* Api+Login.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "Api+Login.h"; sourceTree = ""; }; 189DD76126E60DDC00AB55B1 /* Api+Login.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = "Api+Login.m"; sourceTree = ""; }; 189DD78026E620FE00AB55B1 /* ApiHost.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = ApiHost.h; sourceTree = ""; }; + 18C17A5B26F338F300C48E11 /* XplanFBFlutterViewContainer.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = XplanFBFlutterViewContainer.h; sourceTree = ""; }; + 18C17A5C26F338F300C48E11 /* XplanFBFlutterViewContainer.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = XplanFBFlutterViewContainer.m; sourceTree = ""; }; 18E7B1AE26E8AD760064BC9B /* MainProtocol.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = MainProtocol.h; sourceTree = ""; }; 18E7B1B026E8AF980064BC9B /* MainPresenter.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = MainPresenter.h; sourceTree = ""; }; 18E7B1B126E8AF980064BC9B /* MainPresenter.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = MainPresenter.m; sourceTree = ""; }; @@ -197,6 +201,8 @@ 18E7B31D26F0984C0064BC9B /* UserLevelVo.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = UserLevelVo.m; sourceTree = ""; }; 18E7B31F26F098650064BC9B /* UserInfoSkillVo.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = UserInfoSkillVo.h; sourceTree = ""; }; 18E7B32026F098650064BC9B /* UserInfoSkillVo.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = UserInfoSkillVo.m; sourceTree = ""; }; + 18E7B33026F317A20064BC9B /* XPWebViewController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = XPWebViewController.h; sourceTree = ""; }; + 18E7B33126F317A20064BC9B /* XPWebViewController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = XPWebViewController.m; sourceTree = ""; }; 7DB00EC07F1D0ADFF900B38D /* Pods-xplan-ios.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-xplan-ios.debug.xcconfig"; path = "Target Support Files/Pods-xplan-ios/Pods-xplan-ios.debug.xcconfig"; sourceTree = ""; }; 9B0E1C5726E77022005D4442 /* BaseNavigationController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = BaseNavigationController.h; sourceTree = ""; }; 9B0E1C5826E77022005D4442 /* BaseNavigationController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = BaseNavigationController.m; sourceTree = ""; }; @@ -396,6 +402,8 @@ 18E7B1B426E8B2960064BC9B /* Tabbar */, 18E7B1FD26E8CC510064BC9B /* Match */, 189DD56226DE460400AB55B1 /* Login */, + 18E7B33026F317A20064BC9B /* XPWebViewController.h */, + 18E7B33126F317A20064BC9B /* XPWebViewController.m */, ); path = Main; sourceTree = ""; @@ -590,6 +598,8 @@ 18E7B22826E8CE100064BC9B /* XCFlutterKeyConst.h */, 18E7B22426E8CDCF0064BC9B /* XplanFlutterBoostDelegate.h */, 18E7B22326E8CDCF0064BC9B /* XplanFlutterBoostDelegate.m */, + 18C17A5B26F338F300C48E11 /* XplanFBFlutterViewContainer.h */, + 18C17A5C26F338F300C48E11 /* XplanFBFlutterViewContainer.m */, 18E7B28E26EA0E6B0064BC9B /* FlutterBoost+Xplan.h */, 18E7B28F26EA0E6B0064BC9B /* FlutterBoost+Xplan.m */, ); @@ -897,6 +907,8 @@ E81C278D26EAFAF60031E639 /* DESEncrypt.m in Sources */, E81366E726F0A49E0076364C /* NSString+Regex.m in Sources */, 189DD56526DE465A00AB55B1 /* LoginViewController.m in Sources */, + 18E7B33226F317A20064BC9B /* XPWebViewController.m in Sources */, + 18C17A5D26F338F300C48E11 /* XplanFBFlutterViewContainer.m in Sources */, E8B825BF26E9E57D009E8E9F /* LoginTicketInfo.m in Sources */, 187EEEF026E89FE8002833B2 /* AccountInfoStorage.m in Sources */, E872309326E8D31500B90D4F /* LoginVerifCodeView.m in Sources */, diff --git a/xplan-ios/Base/UI/BaseNavigationController.m b/xplan-ios/Base/UI/BaseNavigationController.m index eb48969c..4d2c23dd 100644 --- a/xplan-ios/Base/UI/BaseNavigationController.m +++ b/xplan-ios/Base/UI/BaseNavigationController.m @@ -21,7 +21,12 @@ } - (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated { + if(self.topViewController == viewController) return; + if (self.childViewControllers.count > 0) { + viewController.hidesBottomBarWhenPushed = YES; + } viewController.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:nil action:nil]; + [super pushViewController:viewController animated:animated]; } diff --git a/xplan-ios/Base/UI/BaseViewController.m b/xplan-ios/Base/UI/BaseViewController.m index 174bdcf5..004b4ea9 100644 --- a/xplan-ios/Base/UI/BaseViewController.m +++ b/xplan-ios/Base/UI/BaseViewController.m @@ -22,7 +22,6 @@ [self.navigationController setNavigationBarHidden:self.isHiddenNavBar animated:animated]; } - - (void)viewDidLoad { [super viewDidLoad]; [self themeConfig]; diff --git a/xplan-ios/Global/XPConstant.h b/xplan-ios/Global/XPConstant.h index 1809e5b0..336334d4 100644 --- a/xplan-ios/Global/XPConstant.h +++ b/xplan-ios/Global/XPConstant.h @@ -16,15 +16,15 @@ UIKIT_EXTERN NSString * const kChannelKey; typedef NS_ENUM(NSUInteger, KeyType) { - KeyType_PasswordEncode,///密码des 加密的 + KeyType_PasswordEncode,///密码 des 加密的 KeyType_NTESQuickLoginBusinessId,///玩意易盾 快捷登录 id - KeyType_QQAppid,///qqde appid - KeyType_QQSecret, ///QQ的 secret - KeyType_WechatAppid,///微信的appid - KeyType_WechatSecret,///微信的secret + KeyType_QQAppid,///qq appid + KeyType_QQSecret, ///QQ 的 secret + KeyType_WechatAppid,///微信的 appid + KeyType_WechatSecret,///微信的 secret }; -/// 获取当前项目中所用到的 type 所对应的valu的值 type 类型 +/// 获取当前项目中所用到的 type 所对应的 value 的值 type 类型 NSString * const KeyWithType(KeyType type); @end diff --git a/xplan-ios/Global/XPHtmlUrl.h b/xplan-ios/Global/XPHtmlUrl.h index 7c9890bd..6a9f7acc 100644 --- a/xplan-ios/Global/XPHtmlUrl.h +++ b/xplan-ios/Global/XPHtmlUrl.h @@ -14,6 +14,7 @@ NS_ASSUME_NONNULL_BEGIN typedef NS_ENUM(NSUInteger, URLType) { kPrivacyURL,///隐私政策 kUserProtocalURL, ///用户协议 + kUserInviteFriendURL, ///邀请好友 }; NSString * const URLWithType(URLType type); diff --git a/xplan-ios/Global/XPHtmlUrl.m b/xplan-ios/Global/XPHtmlUrl.m index 5deb1b51..eea72686 100644 --- a/xplan-ios/Global/XPHtmlUrl.m +++ b/xplan-ios/Global/XPHtmlUrl.m @@ -13,7 +13,8 @@ NSString * const URLWithType(URLType type) { NSString * prefix = @"yinyou"; NSDictionary * dic = @{ @(kPrivacyURL): @"modules/rule/privacy-wap.html", - @(kUserProtocalURL) : @"modules/rule/protocol.html" + @(kUserProtocalURL) : @"modules/rule/protocol.html", + @(kUserInviteFriendURL): @"modules/game/index.html" }; NSString * url = [dic objectForKey:@(type)]; diff --git a/xplan-ios/Main/Match/XplanFBFlutterViewContainer.h b/xplan-ios/Main/Match/XplanFBFlutterViewContainer.h new file mode 100644 index 00000000..e538739d --- /dev/null +++ b/xplan-ios/Main/Match/XplanFBFlutterViewContainer.h @@ -0,0 +1,17 @@ +// +// XplanFBFlutterViewContainer.h +// PlanetStar +// +// Created by apple on 2021/3/30. +// Copyright © 2021 WUJIE INTERACTIVE. All rights reserved. +// + +#import + +NS_ASSUME_NONNULL_BEGIN + +@interface XplanFBFlutterViewContainer : FBFlutterViewContainer + +@end + +NS_ASSUME_NONNULL_END diff --git a/xplan-ios/Main/Match/XplanFBFlutterViewContainer.m b/xplan-ios/Main/Match/XplanFBFlutterViewContainer.m new file mode 100644 index 00000000..e97cece3 --- /dev/null +++ b/xplan-ios/Main/Match/XplanFBFlutterViewContainer.m @@ -0,0 +1,22 @@ +// +// XplanFBFlutterViewContainer.m +// PlanetStar +// +// Created by apple on 2021/3/30. +// Copyright © 2021 WUJIE INTERACTIVE. All rights reserved. +// + +#import "XplanFBFlutterViewContainer.h" + +@interface XplanFBFlutterViewContainer () + +@end + +@implementation XplanFBFlutterViewContainer + +- (void)viewWillAppear:(BOOL)animated { + [super viewWillAppear:animated]; + [self.navigationController setNavigationBarHidden:YES animated:YES]; +} + +@end diff --git a/xplan-ios/Main/Match/XplanFlutterBoostDelegate.m b/xplan-ios/Main/Match/XplanFlutterBoostDelegate.m index 338ab73c..4f7d10b6 100644 --- a/xplan-ios/Main/Match/XplanFlutterBoostDelegate.m +++ b/xplan-ios/Main/Match/XplanFlutterBoostDelegate.m @@ -11,6 +11,9 @@ #import "BaseNavigationController.h" #import "XplanFlutterBoostDelegate.h" #import "XCFlutterKeyConst.h" +#import "XPWebViewController.h" +#import "XPHtmlUrl.h" +#import "XplanFBFlutterViewContainer.h" @implementation XplanFlutterBoostDelegate @@ -39,7 +42,7 @@ ///原生的页面打开一个flutter页面 - (void)pushFlutterRoute:(NSString *)pageName uniqueId:(NSString *)uniqueId arguments:(NSDictionary *)arguments completion:(void (^)(BOOL))completion { - FBFlutterViewContainer *vc = FBFlutterViewContainer.new; + XplanFBFlutterViewContainer *vc = XplanFBFlutterViewContainer.new; [vc setName:pageName uniqueId:nil params:arguments]; UIViewController *rvc = [XCCurrentVCStackManager shareManager].getCurrentVC; self.navigationController = rvc.navigationController; @@ -75,17 +78,18 @@ // TTWKWebViewViewController * webVC = [[TTWKWebViewViewController alloc] init]; // webVC.urlString = HtmlUrlKey(kGameBindAccountURL); // return webVC; -// } else if([pageName isEqualToString:YYFlutterPushNavitePageInvite]){ -// TTWKWebViewViewController * webVC = [[TTWKWebViewViewController alloc] init]; -// webVC.urlString = HtmlUrlKey(kUserInviteFriendURL); -// return webVC; -// } else if([pageName isEqualToString:YYFlutterPushNavitePageWebview]) { -// TTWKWebViewViewController * webVC = [[TTWKWebViewViewController alloc] init]; -// webVC.urlString = arguments[@"webviewUrl"]; -// return webVC; -// } else { +// } else + if([pageName isEqualToString:YYFlutterPushNavitePageInvite]){ + XPWebViewController * webVC = [[XPWebViewController alloc] init]; + webVC.url = URLWithType(kUserInviteFriendURL); + return webVC; + } else if([pageName isEqualToString:YYFlutterPushNavitePageWebview]) { + XPWebViewController * webVC = [[XPWebViewController alloc] init]; + webVC.url = arguments[@"webviewUrl"]; + return webVC; + } else { return nil; -// } + } } diff --git a/xplan-ios/Main/Tabbar/TabbarViewController.m b/xplan-ios/Main/Tabbar/TabbarViewController.m index aeda4d0b..736d5ed0 100644 --- a/xplan-ios/Main/Tabbar/TabbarViewController.m +++ b/xplan-ios/Main/Tabbar/TabbarViewController.m @@ -19,6 +19,7 @@ #import "BaseNavigationController.h" #import "LoginBindPhoneViewController.h" #import "LoginFullInfoViewController.h" +#import "XplanFBFlutterViewContainer.h" ///Present #import "MainPresenter.h" #import "MainProtocol.h" @@ -110,28 +111,29 @@ NSArray *selectImageNames = @[@"tab_game_selected", @"tab_mine_selected"]; NSArray *tabLabel = @[@"赛事", @"我的"]; - FBFlutterViewContainer *fvcGame = FBFlutterViewContainer.new; + XplanFBFlutterViewContainer *fvcGame = XplanFBFlutterViewContainer.new; #ifdef DEBUG [fvcGame setName:@"debug" uniqueId:nil params:nil]; #else [fvc setName:@"/" uniqueId:nil params:nil]; #endif - [self createTabBarItem:fvcGame title:tabLabel[0] image:normalImageNames[0] selectedImage:selectImageNames[0]]; - [self addChildViewController:fvcGame]; + [self addChildViewController:[self createTabBarItem:fvcGame title:tabLabel[0] image:normalImageNames[0] selectedImage:selectImageNames[0]]]; BaseViewController *bvcMe = [[BaseViewController alloc] init]; - [self createTabBarItem:bvcMe title:tabLabel[1] image:normalImageNames[1] selectedImage:selectImageNames[1]]; - [self addChildViewController:bvcMe]; + [self addChildViewController:[self createTabBarItem:bvcMe title:tabLabel[1] image:normalImageNames[1] selectedImage:selectImageNames[1]]]; self.selectedIndex = 1; } -- (void)createTabBarItem:(UIViewController *)itemVc title:(NSString *)title image:(NSString *)image selectedImage:(NSString *)selectedImage{ +- (BaseNavigationController *)createTabBarItem:(UIViewController *)itemVc title:(NSString *)title image:(NSString *)image selectedImage:(NSString *)selectedImage{ itemVc.title = title; itemVc.tabBarItem.image = [[UIImage imageNamed:image] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]; itemVc.tabBarItem.selectedImage = [[UIImage imageNamed:selectedImage] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]; [itemVc.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName:[ThemeColor appMainColor]} forState:UIControlStateSelected]; [itemVc.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName:[ThemeColor tabbarNormalColor]} forState:UIControlStateNormal]; + + BaseNavigationController *nav = [[BaseNavigationController alloc] initWithRootViewController:itemVc]; + return nav; } #pragma mark - 如果不实现的话 如果在main里面请求接口的时候 需要的话 就会导致方法找不到的错误 @@ -144,7 +146,7 @@ } - (void)showLoading { - [XCHUDTool showGIFLoading]; + [XCHUDTool showLoading]; } - (void)hideHUD { diff --git a/xplan-ios/Main/XPWebViewController.h b/xplan-ios/Main/XPWebViewController.h new file mode 100644 index 00000000..7af78d1b --- /dev/null +++ b/xplan-ios/Main/XPWebViewController.h @@ -0,0 +1,19 @@ +// +// XPWebViewController.h +// xplan-ios +// +// Created by zu on 2021/9/16. +// + +#import "BaseViewController.h" +#import + +NS_ASSUME_NONNULL_BEGIN + +@interface XPWebViewController : BaseViewController + +@property (copy, nonatomic) NSString *url; + +@end + +NS_ASSUME_NONNULL_END diff --git a/xplan-ios/Main/XPWebViewController.m b/xplan-ios/Main/XPWebViewController.m new file mode 100644 index 00000000..371c6823 --- /dev/null +++ b/xplan-ios/Main/XPWebViewController.m @@ -0,0 +1,275 @@ +// +// XPWebViewController.m +// xplan-ios +// +// Created by zu on 2021/9/16. +// + +#import "XPWebViewController.h" +#import "AccountInfoStorage.h" +#import "ThemeColor.h" +#import "ApiHost.h" +#import "YYUtility.h" +#import "HttpRequestHelper.h" +#import +#import + +@interface WeakWebViewScriptMessageDelegate : NSObject + +//WKScriptMessageHandler 这个协议类专门用来处理JavaScript调用原生OC的方法 +@property (nonatomic, weak) id scriptDelegate; + +- (instancetype)initWithDelegate:(id)scriptDelegate; + +@end +@implementation WeakWebViewScriptMessageDelegate + +- (instancetype)initWithDelegate:(id)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 + +@interface XPWebViewController () + +@property (strong, nonatomic) WKWebView *webview; +@property (strong, nonatomic) UIProgressView *progressView; +@property (nonatomic, strong) WKUserContentController *userContentController; + +@end + +NSString * const kJSOpenPurse = @"openPurse"; +NSString * const kJSOpenChargePage = @"openChargePage"; +NSString * const kJSOpenSharePage = @"openSharePage"; +NSString * const kJSGetUid = @"getUid"; +NSString * const kJSGetDeviceId = @"getDeviceId"; +NSString * const kJSGetTicket = @"getTicket"; +NSString * const kJSGetDeviceInfo = @"getDeviceInfo"; + +@implementation XPWebViewController + +- (void)viewDidLoad { + [super viewDidLoad]; + [self initView]; +} + +- (void)initView { + if (self.navigationController.viewControllers.count > 1){ + UIBarButtonItem *leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"common_nav_back"] style:UIBarButtonItemStylePlain target:self action:@selector(backButtonClick)]; + + leftBarButtonItem.tintColor = UIColor.whiteColor; + + self.navigationItem.leftBarButtonItem = leftBarButtonItem; + } + self.automaticallyAdjustsScrollViewInsets = NO; + [self.view addSubview:self.webview]; + [self.webview mas_makeConstraints:^(MASConstraintMaker *make) { + make.left.right.top.bottom.mas_equalTo(self.view); + }]; + + [self.view addSubview:self.progressView]; +} + + +- (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message { + if ([message.name isEqualToString:kJSOpenSharePage]) { + [self showSuccessToast:@"打开分享面板,尚未实现"]; + } else if ([message.name isEqualToString:kJSGetUid]) { + NSString *uid = [[AccountInfoStorage instance] getUid]; + NSString *js = [NSString stringWithFormat:@"getMessage(\"uid\",%@)", uid]; + [self.webview evaluateJavaScript:js completionHandler:^(id _Nullable other, NSError * _Nullable error) { + NSLog(@"%@",error); + }]; + } else if ([message.name isEqualToString:kJSGetTicket]) { + NSString *ticket = [[AccountInfoStorage instance] getTicket]; + NSString *js = [NSString stringWithFormat:@"getMessage(\"ticket\",\"%@\")",ticket]; + [self.webview evaluateJavaScript:js completionHandler:^(id _Nullable other, NSError * _Nullable error) { + NSLog(@"%@",error); + }]; + } else if ([message.name isEqualToString:kJSGetDeviceId]) { + NSString *js = [NSString stringWithFormat:@"getMessage(\"deviceId\",\"%@\")",[YYUtility deviceUniqueIdentification]]; + [self.webview evaluateJavaScript:js completionHandler:^(id _Nullable other, NSError * _Nullable error) { + NSLog(@"%@",error); + }]; + } else if ([message.name isEqualToString:kJSGetDeviceInfo]) { + NSDictionary *basicParmars = [HttpRequestHelper configBaseParmars:[[NSDictionary alloc] init]]; + NSString *json = [basicParmars mj_JSONString]; + NSString *js = [NSString stringWithFormat:@"getMessage(\"deviceInfo\",%@)", json]; + [self.webview evaluateJavaScript:js completionHandler:^(id _Nullable ohter, NSError * _Nullable error) { + NSLog(@"%@", error); + }]; + } else if ([message.name isEqualToString:kJSOpenPurse]) { + + } else if ([message.name isEqualToString:kJSOpenChargePage]) { + + } +} + +- (void)setUrl:(NSString *)urlString{ + _url = urlString; + if (_url == nil) { + return; + } + if (![_url hasPrefix:@"http"] && ![_url hasPrefix:@"https"]){ + _url = [NSString stringWithFormat:@"%@/%@", API_HOST_URL, _url]; + } + + if (![_url containsString:@"?"]) { + _url = [NSString stringWithFormat:@"%@?platform=%@", _url, [YYUtility appName]]; + } else { + _url = [NSString stringWithFormat:@"%@&platform=%@", _url, [YYUtility appName]]; + } + + // 去掉 urlString 中的空格。 + NSString *noSpaceTextUrl = [_url stringByReplacingOccurrencesOfString:@" " withString:@""]; + + NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:noSpaceTextUrl]]; + [self.webview loadRequest:request]; +} + +- (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]; + } +} + +- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context { + if ([keyPath isEqualToString:@"estimatedProgress"]) { + self.progressView.progress = self.webview.estimatedProgress; + if (self.progressView.progress == 1) { + __weak typeof (self)weakSelf = self; + [UIView animateWithDuration:0.25f delay:0.3f options:UIViewAnimationOptionCurveEaseOut animations:^{ + weakSelf.progressView.transform = CGAffineTransformMakeScale(1.0f, 1.0f); + } completion:^(BOOL finished) { + weakSelf.progressView.hidden = YES; + }]; + } + } else if ([keyPath isEqualToString:@"title"]) { + if (object == self.webview) { + self.navigationItem.title = self.webview.title; + }else{ + [super observeValueForKeyPath:keyPath ofObject:object change:change context:context]; + } + } else { + [super observeValueForKeyPath:keyPath ofObject:object change:change context:context]; + } +} + +- (void)dealloc { + [self.webview removeObserver:self forKeyPath:@"estimatedProgress"]; + [self.webview removeObserver:self forKeyPath:@"title"]; +} + +- (WKWebView *)webview { + if (_webview == nil) { + WKWebViewConfiguration *configuration = [[WKWebViewConfiguration alloc]init]; + + if (@available(iOS 10.0, *)) { + configuration.mediaTypesRequiringUserActionForPlayback = NO; + } else { + // Fallback on earlier versions + } + configuration.allowsInlineMediaPlayback = YES; + + NSString *uid = [[AccountInfoStorage instance] getUid]; + NSString *realCookie = [NSString stringWithFormat:@"%@=%@",@"uid",uid]; + + WKUserScript *cookieScript = [[WKUserScript alloc] initWithSource: [NSString stringWithFormat:@"document.cookie = '%@';", realCookie] injectionTime:WKUserScriptInjectionTimeAtDocumentStart forMainFrameOnly:NO]; + [self.userContentController addUserScript:cookieScript]; + //根据生成的WKUserScript对象,初始化WKWebViewConfiguration + configuration.preferences.javaScriptEnabled = YES; + configuration.preferences.javaScriptCanOpenWindowsAutomatically = YES; + configuration.preferences.minimumFontSize = 10; + configuration.selectionGranularity = WKSelectionGranularityCharacter; + configuration.userContentController = self.userContentController; + + CGSize size = [UIScreen mainScreen].bounds.size; + _webview = [[WKWebView alloc]initWithFrame:CGRectMake(0, 0, size.width,size.height) configuration:configuration]; + + _webview.navigationDelegate = self; + //添加KVO,WKWebView有一个属性estimatedProgress,就是当前网页加载的进度,所以监听这个属性 + [_webview addObserver:self forKeyPath:@"estimatedProgress" options:NSKeyValueObservingOptionNew context:nil]; + //添加KVO,监听title属性 + [_webview addObserver:self forKeyPath:@"title" options:NSKeyValueObservingOptionNew context:NULL]; + + UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(backButtonClick)]; + [_webview addGestureRecognizer:swipeGesture]; + + [_webview.scrollView setShowsVerticalScrollIndicator:NO]; + [_webview.scrollView setShowsHorizontalScrollIndicator:NO]; + + //set useragent + __weak typeof(self) weakSelf = self; + [_webview evaluateJavaScript:@"navigator.userAgent" completionHandler:^(id result, NSError *error) { + NSString *userAgent = result; + + if (![userAgent containsString:@"tutuAppIos erbanAppIos"]){ + NSString *newUserAgent = [userAgent stringByAppendingString:@" tutuAppIos erbanAppIos"]; + NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:newUserAgent, @"UserAgent", nil]; + [[NSUserDefaults standardUserDefaults] registerDefaults:dictionary]; + [[NSUserDefaults standardUserDefaults] synchronize]; + [weakSelf.webview setCustomUserAgent:newUserAgent]; + } + + }]; + + } + return _webview; +} + +- (UIProgressView *)progressView{ + if (!_progressView) { + _progressView = [[UIProgressView alloc] initWithFrame:CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width, 1)]; + _progressView.progressTintColor = [ThemeColor appMainColor]; + _progressView.trackTintColor = [UIColor clearColor]; + _progressView.transform = CGAffineTransformMakeScale(1.0f, 1.0f); + } + return _progressView; +} + +- (WKUserContentController *)userContentController{ + if (!_userContentController) { + //自定义的WKScriptMessageHandler 是为了解决内存不释放的问题 + WeakWebViewScriptMessageDelegate *weakScriptMessageDelegate = [[WeakWebViewScriptMessageDelegate alloc] initWithDelegate:self]; + _userContentController = [[WKUserContentController alloc] init]; + // 分享面板 + [_userContentController addScriptMessageHandler:weakScriptMessageDelegate name:kJSOpenSharePage]; + // 钱包页面 + [_userContentController addScriptMessageHandler:weakScriptMessageDelegate name:kJSOpenPurse]; + // 充值页面 + [_userContentController addScriptMessageHandler:weakScriptMessageDelegate name:kJSOpenChargePage]; + // 获取uid + [_userContentController addScriptMessageHandler:weakScriptMessageDelegate name:kJSGetUid]; + // 获取设备id + [_userContentController addScriptMessageHandler:weakScriptMessageDelegate name:kJSGetDeviceId]; + // 获取Ticket + [_userContentController addScriptMessageHandler:weakScriptMessageDelegate name:kJSGetTicket]; + // 获取设备info + [_userContentController addScriptMessageHandler:weakScriptMessageDelegate name:kJSGetDeviceInfo]; + + } + return _userContentController; +} + +@end