猎豆优选

LDNewWebVC.m 29KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786
  1. //
  2. // LDNewWebVC.m
  3. // YouHuiProject
  4. //
  5. // Created by liuxueli on 2019/2/22.
  6. // Copyright © 2019 kuxuan. All rights reserved.
  7. //
  8. #import "LDNewWebVC.h"
  9. #import <AlibcTradeSDK/AlibcTradeSDK.h>
  10. #import <AlibabaAuthSDK/ALBBSession.h>
  11. #import <AlibabaAuthSDK/ALBBSDK.h>
  12. #import <WebKit/WebKit.h>
  13. #import "WebviewProgressLine.h"
  14. #import <JavaScriptCore/JavaScriptCore.h>
  15. #import "LDFeedbackController.h"
  16. #import "WechatShareTool.h"
  17. #import "WYWeakScriptMessageDelegate.h"
  18. #import "LDLoginViewController.h"
  19. #import "LDChildAccountViewController.h"
  20. static NSString *app_h5_close = @"app_h5_close";
  21. static NSString *app_h5_get_token = @"app_h5_get_token";
  22. static NSString *app_h5_wx_share = @"app_h5_wx_share";
  23. static NSString *app_h5_get_wechat_info = @"app_h5_get_wechat_info";
  24. static NSString *refresh_h5 = @"refresh_h5";
  25. static NSString *app_h5_login = @"app_h5_login"; //h5唤起原生的登录
  26. static NSString *app_h5_wx_share_url = @"app_h5_wx_share_url"; //分享url
  27. static NSString *app_h5_wx_share_image = @"app_h5_wx_share_image"; //分享到微信image
  28. static NSString *app_h5_wx_share_video = @"app_h5_wx_share_video"; //分享到微信video
  29. static NSString *app_h5_wx_share_min = @"app_h5_wx_share_min"; //分享微信小程序(只支持微信好友)
  30. static NSString *app_h5_open_taobao_url = @"app_h5_open_taobao_url"; //打开淘宝url
  31. static NSString *app_h5_open_goodDetail =@"app_h5_open_goodDetail";//唤起原生的详情页
  32. static NSString *app_h5_open_url =@"app_h5_open_url";//h5打开URL
  33. static NSString *app_h5_isLogin =@"app_h5_isLogin";//是否登录
  34. static NSString *app_h5_withdraw =@"app_h5_withdraw";//提现页面
  35. static NSString *openWeiChat =@"openWeiChat";//打开微信
  36. @interface LDNewWebVC ()<WKUIDelegate, WKNavigationDelegate,WKScriptMessageHandler,UIScrollViewDelegate>
  37. {
  38. CGFloat _lastContentOffset;
  39. }
  40. @property (nonatomic, strong) WKWebView *webView;
  41. /** 加载进度条 */
  42. @property (nonatomic, strong) UIProgressView *progressView;
  43. @end
  44. @implementation LDNewWebVC
  45. - (void)viewDidAppear:(BOOL)animated {
  46. [super viewDidAppear:animated];
  47. [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault];
  48. }
  49. - (void)viewWillAppear:(BOOL)animated {
  50. [super viewWillAppear:animated];
  51. [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault];
  52. [MobClick event:HomeSaleList label:@"特卖专区"];
  53. }
  54. - (void)viewDidLoad {
  55. [super viewDidLoad];
  56. self.navigationController.navigationBar.hidden = YES;
  57. [self addNotification];
  58. [self configNavigationBar];
  59. [self addUserhandler];
  60. [self loadRequest];
  61. self.webView.navigationDelegate = self;
  62. self.webView.UIDelegate = self;
  63. }
  64. - (void)addNotification {
  65. [[NSNotificationCenter defaultCenter] addObserver:self
  66. selector:@selector(refreshH5Action)
  67. name:UIApplicationDidBecomeActiveNotification object:nil];
  68. [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(endFullScreen) name:UIWindowDidBecomeHiddenNotification object:nil];
  69. }
  70. //处理web视频全屏播放时 状态栏消失
  71. - (void)endFullScreen {
  72. [[UIApplication sharedApplication]setStatusBarHidden:false animated:false];
  73. }
  74. - (void)configNavigationBar {
  75. self.view.backgroundColor = [UIColor whiteColor];
  76. [self.view addSubview:self.webView];
  77. [self.view addSubview:self.progressView];
  78. self.navigationBar.hidden=YES;
  79. if (@available(iOS 11.0, *)) {
  80. self.webView.scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;//UIScrollView也适用
  81. }else {
  82. self.automaticallyAdjustsScrollViewInsets = NO;
  83. }
  84. }
  85. - (void)loadRequest {
  86. if (@available(iOS 9.0, *)) {
  87. NSArray * types=@[WKWebsiteDataTypeCookies,WKWebsiteDataTypeLocalStorage];
  88. NSSet *websiteDataTypes= [NSSet setWithArray:types];
  89. NSDate *dateFrom = [NSDate dateWithTimeIntervalSince1970:0];
  90. [[WKWebsiteDataStore defaultDataStore] removeDataOfTypes:websiteDataTypes modifiedSince:dateFrom completionHandler:^{
  91. }];
  92. }
  93. // NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:self.url]];
  94. NSString *urlStr = [self.url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
  95. NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:urlStr]
  96. cachePolicy:NSURLRequestReloadIgnoringCacheData
  97. timeoutInterval:15.0];
  98. [self.webView loadRequest:theRequest];
  99. }
  100. - (void)addUserhandler {
  101. [[self.webView configuration].userContentController addScriptMessageHandler:[[WYWeakScriptMessageDelegate alloc] initWithDelegate:self] name:app_h5_close];
  102. [[self.webView configuration].userContentController addScriptMessageHandler:[[WYWeakScriptMessageDelegate alloc] initWithDelegate:self] name:app_h5_get_token];
  103. [[self.webView configuration].userContentController addScriptMessageHandler:[[WYWeakScriptMessageDelegate alloc] initWithDelegate:self] name:app_h5_wx_share];
  104. [[self.webView configuration].userContentController addScriptMessageHandler:[[WYWeakScriptMessageDelegate alloc] initWithDelegate:self] name:app_h5_get_wechat_info];
  105. [[self.webView configuration].userContentController addScriptMessageHandler:[[WYWeakScriptMessageDelegate alloc] initWithDelegate:self] name:app_h5_login];
  106. [[self.webView configuration].userContentController addScriptMessageHandler:[[WYWeakScriptMessageDelegate alloc] initWithDelegate:self] name:app_h5_wx_share_url];
  107. [[self.webView configuration].userContentController addScriptMessageHandler:[[WYWeakScriptMessageDelegate alloc] initWithDelegate:self] name:app_h5_wx_share_image];
  108. [[self.webView configuration].userContentController addScriptMessageHandler:[[WYWeakScriptMessageDelegate alloc] initWithDelegate:self] name:app_h5_wx_share_video];
  109. [[self.webView configuration].userContentController addScriptMessageHandler:[[WYWeakScriptMessageDelegate alloc] initWithDelegate:self] name:app_h5_wx_share_min];
  110. [[self.webView configuration].userContentController addScriptMessageHandler:[[WYWeakScriptMessageDelegate alloc] initWithDelegate:self] name:app_h5_open_taobao_url];
  111. [[self.webView configuration].userContentController addScriptMessageHandler:[[WYWeakScriptMessageDelegate alloc] initWithDelegate:self] name:app_h5_open_goodDetail];
  112. [[self.webView configuration].userContentController addScriptMessageHandler:[[WYWeakScriptMessageDelegate alloc] initWithDelegate:self] name:app_h5_open_url];
  113. [[self.webView configuration].userContentController addScriptMessageHandler:[[WYWeakScriptMessageDelegate alloc] initWithDelegate:self] name:app_h5_isLogin];
  114. [[self.webView configuration].userContentController addScriptMessageHandler:[[WYWeakScriptMessageDelegate alloc] initWithDelegate:self] name:app_h5_withdraw];
  115. [[self.webView configuration].userContentController addScriptMessageHandler:[[WYWeakScriptMessageDelegate alloc] initWithDelegate:self] name:openWeiChat];
  116. }
  117. #pragma mark --- WKScriptMessageHandler
  118. - (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message {
  119. if ([message.name isEqualToString:app_h5_close]) {
  120. //关闭页面
  121. [self.navigationController popViewControllerAnimated:YES];
  122. }
  123. else if ([message.name isEqualToString:app_h5_get_token]){
  124. //传token给js
  125. [self sendUserTokenToJS:message];
  126. }
  127. else if ([message.name isEqualToString:app_h5_wx_share]){
  128. //调起原生分享
  129. [self shareToNative:message];
  130. }
  131. else if ([message.name isEqualToString:app_h5_get_wechat_info]){
  132. //获取微信信息
  133. [self getWechatInfoWithMessage:message];
  134. }
  135. else if ([message.name isEqualToString:app_h5_wx_share_image]){
  136. //分享图片
  137. [self shareImage:message];
  138. }
  139. else if ([message.name isEqualToString:app_h5_wx_share_url]){
  140. //分享url
  141. [self shareURL:message];
  142. }
  143. else if ([message.name isEqualToString:app_h5_wx_share_video]){
  144. //分享video
  145. [self shareVideo:message];
  146. }
  147. else if ([message.name isEqualToString:app_h5_wx_share_min]){
  148. //分享小程序
  149. [self shareMinProgram:message];
  150. }else if ([message.name isEqualToString:app_h5_login]){
  151. //获取token
  152. [self gotoNativeLogin:message];
  153. }else if ([message.name isEqualToString:app_h5_open_taobao_url]){
  154. //打开淘宝
  155. [self openTaobao:message];
  156. }else if ([message.name isEqualToString:app_h5_open_goodDetail] )
  157. {
  158. [self goodDetail:message];
  159. }else if ([message.name isEqualToString:app_h5_open_url] )
  160. {//h5打开URL
  161. [self openUrl:message];
  162. }else if ([message.name isEqualToString:app_h5_isLogin] )
  163. {
  164. [self sendUserTokenISLoginToJS:message];
  165. }else if ([message.name isEqualToString:app_h5_withdraw] )//提现
  166. {
  167. [self withdrawal:message];
  168. }else if ([message.name isEqualToString:openWeiChat] )//打开微信
  169. {
  170. [self callWeChat];
  171. }
  172. else {
  173. }
  174. }
  175. //提现页面
  176. -(void)withdrawal:(WKScriptMessage *)message
  177. {
  178. if (![AccountTool isLogin]) {
  179. LDNewLoginVC *login = [[LDNewLoginVC alloc] init];
  180. login.loginSucc = ^{
  181. NSDictionary *dict = [self dictionaryWithJsonString:message.body];
  182. NSString *jsMethod = dict[@"js_callback"];
  183. NSString *token = [AccountTool account].token;
  184. NSDictionary *param = @{@"token":token,
  185. };
  186. NSString *jsonDict = [self convertToJsonData:param];
  187. NSString *callBacKMethod = [NSString stringWithFormat:@"%@('%@')",jsMethod,jsonDict];
  188. [self.webView evaluateJavaScript:callBacKMethod completionHandler:^(id _Nullable value, NSError * _Nullable error) {
  189. }];
  190. };
  191. login.modalPresentationStyle = UIModalPresentationFullScreen;
  192. [self presentViewController:login animated:YES completion:nil];
  193. return;
  194. }
  195. LDChildAccountViewController *accountDetail = [[LDChildAccountViewController alloc] init];//提现
  196. [self.navigationController pushViewController:accountDetail animated:YES];
  197. }
  198. //调起微信
  199. - (void)callWeChat
  200. {
  201. NSString* WeChatUrl = [NSString stringWithFormat:@"weixin://"];
  202. UIWebView* webview = [[UIWebView alloc]initWithFrame:CGRectZero];
  203. [[UIApplication sharedApplication].keyWindow addSubview:webview];
  204. NSURL*url = [NSURL URLWithString:WeChatUrl];
  205. NSURLRequest *request = [NSURLRequest requestWithURL:url];
  206. [webview loadRequest:request];
  207. }
  208. //h5打开URL
  209. -(void)openUrl:(WKScriptMessage *)message
  210. {
  211. NSDictionary *dict = [self dictionaryWithJsonString:message.body];
  212. NSString *jsMethod = dict[@"push_url"];
  213. LDAdWebViewController *web =[[LDAdWebViewController alloc]init];
  214. web.url=jsMethod;
  215. [self.navigationController pushViewController:web animated:YES];
  216. }
  217. /**
  218. 传token判断登录给js
  219. */
  220. - (void)sendUserTokenISLoginToJS:(WKScriptMessage *)message {
  221. __block NSString *token = [AccountTool account].token;
  222. NSDictionary *dict = [self dictionaryWithJsonString:message.body];
  223. NSString *jsMethod = dict[@"js_callback"];
  224. if (!token) {
  225. token = @"";
  226. }
  227. NSString *islogin=@"false";
  228. if ([AccountTool isLogin] ) {
  229. islogin = @"true";
  230. }
  231. NSDictionary *tokenPara = @{@"token":token,
  232. @"isLogin":islogin
  233. };
  234. NSString *jsonDict = [self convertToJsonData:tokenPara];
  235. NSString *callBacKMethod = [NSString stringWithFormat:@"%@('%@')",jsMethod,jsonDict];
  236. [self.webView evaluateJavaScript:callBacKMethod completionHandler:^(id _Nullable value, NSError * _Nullable error) {
  237. }];
  238. }
  239. - (void)refreshH5Action {
  240. if (@available(iOS 9.0, *)) {
  241. NSArray * types=@[WKWebsiteDataTypeCookies,WKWebsiteDataTypeLocalStorage];
  242. NSSet *websiteDataTypes= [NSSet setWithArray:types];
  243. NSDate *dateFrom = [NSDate dateWithTimeIntervalSince1970:0];
  244. [[WKWebsiteDataStore defaultDataStore] removeDataOfTypes:websiteDataTypes modifiedSince:dateFrom completionHandler:^{
  245. }];
  246. }
  247. NSString *callBacKMethod = [NSString stringWithFormat:@"%@()",refresh_h5];
  248. [self.webView evaluateJavaScript:callBacKMethod completionHandler:^(id _Nullable value, NSError * _Nullable error) {
  249. }];
  250. }
  251. /**
  252. 传token给js
  253. */
  254. - (void)sendUserTokenToJS:(WKScriptMessage *)message {
  255. __block NSString *token = [AccountTool account].token;
  256. NSString *openid = [AccountTool account].openId;
  257. NSString *unionid = [AccountTool account].unionid;
  258. if (![AccountTool isLogin] || openid==nil || unionid ==nil) {
  259. LDNewLoginVC *login = [[LDNewLoginVC alloc] init];
  260. login.loginSucc = ^{
  261. NSDictionary *dict = [self dictionaryWithJsonString:message.body];
  262. NSString *jsMethod = dict[@"js_callback"];
  263. if (!token) {
  264. token = @"";
  265. }
  266. NSDictionary *tokenPara = @{@"token":token,
  267. @"openid":openid,
  268. @"unionid":unionid,
  269. };
  270. NSString *jsonDict = [self convertToJsonData:tokenPara];
  271. NSString *callBacKMethod = [NSString stringWithFormat:@"%@('%@')",jsMethod,jsonDict];
  272. [self.webView evaluateJavaScript:callBacKMethod completionHandler:^(id _Nullable value, NSError * _Nullable error) {
  273. }];
  274. };
  275. login.modalPresentationStyle = UIModalPresentationFullScreen;
  276. [self presentViewController:login animated:YES completion:nil];
  277. return;
  278. }
  279. NSDictionary *dict = [self dictionaryWithJsonString:message.body];
  280. NSString *jsMethod = dict[@"js_callback"];
  281. if (!token) {
  282. token = @"";
  283. }
  284. NSDictionary *tokenPara = @{@"token":token,
  285. @"openid":openid,
  286. @"unionid":unionid,
  287. };
  288. NSString *jsonDict = [self convertToJsonData:tokenPara];
  289. NSString *callBacKMethod = [NSString stringWithFormat:@"%@('%@')",jsMethod,jsonDict];
  290. [self.webView evaluateJavaScript:callBacKMethod completionHandler:^(id _Nullable value, NSError * _Nullable error) {
  291. }];
  292. }
  293. //商品详情页
  294. -(void)goodDetail:(WKScriptMessage *)message
  295. {
  296. NSDictionary *dict = [self dictionaryWithJsonString:message.body];
  297. DetailRequestModel *model =[[DetailRequestModel alloc]init];
  298. [model setValuesForKeysWithDictionary:dict];
  299. [MobClick event:HomeSaleListDetail label:@"特卖专区跳转详情"];
  300. LDGoodDetailViewController *detail = [[LDGoodDetailViewController alloc] init];
  301. detail.requestModel = model;
  302. [self.navigationController pushViewController:detail animated:YES];
  303. }
  304. /**
  305. 唤起登录
  306. */
  307. - (void)gotoNativeLogin:(WKScriptMessage *)message {
  308. if (![AccountTool isLogin]) {
  309. LDNewLoginVC *login = [[LDNewLoginVC alloc] init];
  310. login.loginSucc = ^{
  311. NSDictionary *dict = [self dictionaryWithJsonString:message.body];
  312. NSString *jsMethod = dict[@"js_callback"];
  313. NSString *token = [AccountTool account].token;
  314. NSDictionary *param = @{@"token":token,
  315. };
  316. NSString *jsonDict = [self convertToJsonData:param];
  317. NSString *callBacKMethod = [NSString stringWithFormat:@"%@('%@')",jsMethod,jsonDict];
  318. [self.webView evaluateJavaScript:callBacKMethod completionHandler:^(id _Nullable value, NSError * _Nullable error) {
  319. }];
  320. };
  321. login.modalPresentationStyle = UIModalPresentationFullScreen;
  322. [self presentViewController:login animated:YES completion:nil];
  323. return;
  324. }
  325. NSDictionary *dict = [self dictionaryWithJsonString:message.body];
  326. NSString *jsMethod = dict[@"js_callback"];
  327. NSString *token = [AccountTool account].token;
  328. NSDictionary *param = @{@"token":token,
  329. };
  330. NSString *jsonDict = [self convertToJsonData:param];
  331. NSString *callBacKMethod = [NSString stringWithFormat:@"%@('%@')",jsMethod,jsonDict];
  332. [self.webView evaluateJavaScript:callBacKMethod completionHandler:^(id _Nullable value, NSError * _Nullable error) {
  333. }];
  334. }
  335. /**
  336. 获取微信信息
  337. */
  338. - (void)getWechatInfoWithMessage:(WKScriptMessage *)message {
  339. NSString *openid = [AccountTool account].openId;
  340. NSString *unionid = [AccountTool account].unionid;
  341. if (![AccountTool isLogin] || openid==nil || unionid ==nil) {
  342. LDNewLoginVC *login = [[LDNewLoginVC alloc] init];
  343. login.loginSucc = ^{
  344. NSDictionary *dict = [self dictionaryWithJsonString:message.body];
  345. NSString *jsMethod = dict[@"js_callback"];
  346. NSDictionary *param = @{@"openid":openid,
  347. @"unionid":unionid
  348. };
  349. NSString *jsonDict = [self convertToJsonData:param];
  350. NSString *callBacKMethod = [NSString stringWithFormat:@"%@('%@')",jsMethod,jsonDict];
  351. [self.webView evaluateJavaScript:callBacKMethod completionHandler:^(id _Nullable value, NSError * _Nullable error) {
  352. }];
  353. };
  354. login.modalPresentationStyle = UIModalPresentationFullScreen;
  355. [self presentViewController:login animated:YES completion:nil];
  356. return;
  357. }
  358. NSDictionary *dict = [self dictionaryWithJsonString:message.body];
  359. NSString *jsMethod = dict[@"js_callback"];
  360. NSDictionary *param = @{@"openid":openid,
  361. @"unionid":unionid
  362. };
  363. NSString *jsonDict = [self convertToJsonData:param];
  364. NSString *callBacKMethod = [NSString stringWithFormat:@"%@('%@')",jsMethod,jsonDict];
  365. [self.webView evaluateJavaScript:callBacKMethod completionHandler:^(id _Nullable value, NSError * _Nullable error) {
  366. }];
  367. }
  368. /**
  369. 调起分享
  370. */
  371. - (void)shareToNative:(WKScriptMessage *)message {
  372. NSDictionary *dict = [self dictionaryWithJsonString:message.body];
  373. if ([dict[@"shareType"] isEqualToString:@"1"]) {
  374. [self shareImageWithplatformType:UMSocialPlatformType_WechatSession withDict:dict];
  375. }else {
  376. [self shareImageWithplatformType:UMSocialPlatformType_WechatTimeLine withDict:dict];
  377. }
  378. }
  379. - (void)shareImage:(WKScriptMessage *)message {
  380. NSDictionary *dict = [self dictionaryWithJsonString:message.body];
  381. if ([dict[@"shareType"] isEqualToString:@"1"]) {
  382. [WechatShareTool shareImageWithplatformType:UMSocialPlatformType_WechatSession withDict:dict];
  383. }else {
  384. [WechatShareTool shareImageWithplatformType:UMSocialPlatformType_WechatTimeLine withDict:dict];
  385. }
  386. }
  387. - (void)shareURL:(WKScriptMessage *)message {
  388. NSDictionary *dict = [self dictionaryWithJsonString:message.body];
  389. if ([dict[@"shareType"] isEqualToString:@"1"]) {
  390. [WechatShareTool shareURLWithplatformType:UMSocialPlatformType_WechatSession withDict:dict];
  391. }else {
  392. [WechatShareTool shareURLWithplatformType:UMSocialPlatformType_WechatTimeLine withDict:dict];
  393. }
  394. }
  395. - (void)shareVideo:(WKScriptMessage *)message {
  396. NSDictionary *dict = [self dictionaryWithJsonString:message.body];
  397. if ([dict[@"shareType"] isEqualToString:@"1"]) {
  398. [WechatShareTool shareVideoWithplatformType:UMSocialPlatformType_WechatSession withDict:dict];
  399. }else {
  400. [WechatShareTool shareVideoWithplatformType:UMSocialPlatformType_WechatTimeLine withDict:dict];
  401. }
  402. }
  403. - (void)shareMinProgram:(WKScriptMessage *)message {
  404. NSDictionary *dict = [self dictionaryWithJsonString:message.body];
  405. [WechatShareTool shareMinProgramWithplatformType:UMSocialPlatformType_WechatSession withDict:dict];
  406. }
  407. - (void)openTaobao:(WKScriptMessage *)message {
  408. NSDictionary *dict = [self dictionaryWithJsonString:message.body];
  409. NSString *url = dict[@"taobaoUrl"];
  410. [self jumpToTaobaoWithUrl:url];
  411. }
  412. - (void)jumpToTaobaoWithUrl:(NSString *)url {
  413. id<AlibcTradePage> page = [AlibcTradePageFactory page:url];
  414. //淘客信息
  415. AlibcTradeTaokeParams *taoKeParams=[[AlibcTradeTaokeParams alloc] init];
  416. taoKeParams.pid= ALTK_PID;
  417. //打开方式
  418. AlibcTradeShowParams* showParam = [[AlibcTradeShowParams alloc] init];
  419. showParam.openType = AlibcOpenTypeNative;
  420. [[AlibcTradeSDK sharedInstance].tradeService openByUrl:url identity:@"trade" webView:nil parentController:self.parentViewController showParams:showParam taoKeParams:taoKeParams trackParam:nil tradeProcessSuccessCallback:^(AlibcTradeResult * _Nullable result) {
  421. } tradeProcessFailedCallback:^(NSError * _Nullable error) {
  422. }];
  423. }
  424. -(void)shareImageWithplatformType:(UMSocialPlatformType)platformType withDict:(NSDictionary *)dict{
  425. //创建分享消息对象
  426. UMSocialMessageObject *messageObject = [UMSocialMessageObject messageObject];
  427. //创建图片内容对象
  428. UMShareWebpageObject *shareObject = [UMShareWebpageObject shareObjectWithTitle:dict[@"shareTitle"] descr:dict[@"shareDescirpt"] thumImage:dict[@"shareImageUrl"]];
  429. shareObject.webpageUrl = dict[@"shareUrl"];
  430. messageObject.shareObject = shareObject;
  431. [[UMSocialManager defaultManager] shareToPlatform:platformType messageObject:messageObject currentViewController:nil completion:^(id data, NSError *error) {
  432. }];
  433. }
  434. /**
  435. kvo监听进度条
  436. @param keyPath keyPath description
  437. @param object object description
  438. @param change change description
  439. @param context context description
  440. */
  441. - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context{
  442. if ([keyPath isEqualToString:NSStringFromSelector(@selector(estimatedProgress))] && object == self.webView ) {
  443. [self.progressView setAlpha:1.0];
  444. [self.progressView setProgress:self.webView.estimatedProgress ];
  445. if (self.webView.estimatedProgress >= 1.0) {
  446. [UIView animateWithDuration:0.7 animations:^{
  447. [self.progressView setProgress:1.0 animated:YES];
  448. [self.progressView setAlpha:0.0];
  449. }];
  450. }
  451. }else{
  452. [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
  453. }
  454. }
  455. #pragma mark-
  456. #pragma mark- WKNavigationDelegate delegate
  457. /**
  458. 开始加载web的时候
  459. @param webView webView description
  460. @param navigation navigation description
  461. */
  462. - (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation{
  463. // //开始加载的时候,让进度条显示
  464. // self.progressView.hidden = NO;
  465. // self.progressView.progress = 0;
  466. // self.progressView.alpha = 1.0;
  467. // [UIView animateWithDuration:0.8 animations:^{
  468. // self.progressView.progress = 0.6;
  469. //
  470. // }];
  471. }
  472. /**
  473. 当网页加载完成的时候调用
  474. @param webView web描述
  475. @param navigation 导航的描述
  476. */
  477. - (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation{
  478. [[UIApplication sharedApplication]setNetworkActivityIndicatorVisible:NO];
  479. }
  480. -(void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler
  481. {
  482. NSURL *url = navigationAction.request.URL;
  483. UIApplication *app = [UIApplication sharedApplication];
  484. // 打开appstore
  485. if ([url.absoluteString containsString:@"https://itunes.apple.com/cn/app/"])
  486. {
  487. if ([app canOpenURL:url])
  488. {
  489. //设备系统为IOS 10.0或者以上的
  490. if (@available(iOS 10.0, *)) {
  491. [[UIApplication sharedApplication] openURL:url options:@{} completionHandler:nil];
  492. } else {
  493. [[UIApplication sharedApplication] openURL:url];
  494. }
  495. decisionHandler(WKNavigationActionPolicyCancel);
  496. return;
  497. }
  498. }
  499. decisionHandler(WKNavigationActionPolicyAllow);
  500. }
  501. #pragma mark -----------
  502. - (void)backAction {
  503. NSArray *viewControllers = self.navigationController.viewControllers;
  504. if (viewControllers.count < 1){
  505. [self dismissViewControllerAnimated:YES completion:nil];
  506. }else {
  507. if (self.webView.canGoBack) {
  508. [self.webView goBack];
  509. }else{
  510. [self.navigationController popViewControllerAnimated:YES];
  511. }
  512. }
  513. }
  514. - (void)closeAction {
  515. [self.navigationController popViewControllerAnimated:YES];
  516. }
  517. - (void)refreshAction {
  518. [self.webView reload];
  519. }
  520. - (WKWebView *)webView{
  521. if (!_webView) {
  522. WKWebViewConfiguration *config = [[WKWebViewConfiguration alloc] init];
  523. config.allowsInlineMediaPlayback = NO;
  524. _webView = [[WKWebView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, ChildTableViewHeight) configuration:config];
  525. _webView.backgroundColor = [UIColor colorWithRed:240.0/255 green:240.0/255 blue:240.0/255 alpha:1.0];
  526. //使用kvo监听进度
  527. [_webView addObserver:self forKeyPath:NSStringFromSelector(@selector(estimatedProgress)) options:0 context:nil];
  528. // _webView.scrollView.delegate=self;
  529. //手势触摸滑动
  530. _webView.allowsBackForwardNavigationGestures = YES;
  531. //自适应
  532. [_webView sizeToFit];
  533. //自适应
  534. [_webView sizeToFit];
  535. }
  536. return _webView;
  537. }
  538. #pragma mark-
  539. #pragma mark- SetupConstraints
  540. /** 加载配置以及视图添加*/
  541. - (void)loadSubViewsConfiguration{
  542. [self.view addSubview:self.webView];
  543. }
  544. #pragma mark - scrollView
  545. - (void)scrollViewDidScroll:(UIScrollView *)scrollView
  546. {
  547. if (scrollView.contentOffset.y < 0 ||[self.webView.scrollView.mj_footer isRefreshing]) {
  548. return;
  549. }
  550. //scrollView已经有拖拽手势,直接拿到scrollView的拖拽手势
  551. UIPanGestureRecognizer *pan = scrollView.panGestureRecognizer;
  552. //获取到拖拽的速度 >0 向下拖动 <0 向上拖动
  553. CGFloat velocity = [pan velocityInView:scrollView].y;
  554. CGFloat offY = scrollView.contentOffset.y - _lastContentOffset;
  555. NSNotification *noti = [[NSNotification alloc] initWithName:ScrollDirectorNoti object:nil userInfo:@{@"director":@(velocity),@"offY":@(offY)}];
  556. [[NSNotificationCenter defaultCenter] postNotification:noti];
  557. _lastContentOffset = scrollView.contentOffset.y;
  558. }
  559. //- (UIProgressView *)progressView{
  560. // if (!_progressView) {
  561. // _progressView = [[UIProgressView alloc]initWithProgressViewStyle:UIProgressViewStyleDefault];
  562. // _progressView.frame = CGRectMake(0, NavBarHeight, self.view.frame.size.width, 2);
  563. // //设置进度条的色彩
  564. // [_progressView setTrackTintColor:[UIColor colorWithRed:240.0/255 green:240.0/255 blue:240.0/255 alpha:1.0]];
  565. // _progressView.progressTintColor = [UIColor homeRedColor];
  566. // }
  567. // return _progressView;
  568. //}
  569. //观察的移除
  570. - (void)dealloc{
  571. [self.webView removeObserver:self forKeyPath:NSStringFromSelector(@selector(estimatedProgress))];
  572. [[NSNotificationCenter defaultCenter] removeObserver:self];
  573. }
  574. /**
  575. json字符串转字典
  576. */
  577. - (NSDictionary *)dictionaryWithJsonString:(NSString *)jsonString
  578. {
  579. if (jsonString == nil) {
  580. return nil;
  581. }
  582. NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
  583. NSError *err;
  584. NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:jsonData
  585. options:NSJSONReadingMutableContainers
  586. error:&err];
  587. if(err)
  588. {
  589. NSLog(@"json解析失败:%@",err);
  590. return nil;
  591. }
  592. return dic;
  593. }
  594. /**
  595. 字典转json字符串
  596. */
  597. - (NSString *)convertToJsonData:(NSDictionary *)dict
  598. {
  599. NSError *error;
  600. NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dict options:NSJSONWritingPrettyPrinted error:&error];
  601. NSString *jsonString;
  602. if (!jsonData) {
  603. NSLog(@"%@",error);
  604. }else{
  605. jsonString = [[NSString alloc]initWithData:jsonData encoding:NSUTF8StringEncoding];
  606. }
  607. NSMutableString *mutStr = [NSMutableString stringWithString:jsonString];
  608. NSRange range = {0,jsonString.length};
  609. //去掉字符串中的空格
  610. [mutStr replaceOccurrencesOfString:@" " withString:@"" options:NSLiteralSearch range:range];
  611. NSRange range2 = {0,mutStr.length};
  612. //去掉字符串中的换行符
  613. [mutStr replaceOccurrencesOfString:@"\n" withString:@"" options:NSLiteralSearch range:range2];
  614. return mutStr;
  615. }
  616. - (void)didReceiveMemoryWarning {
  617. [super didReceiveMemoryWarning];
  618. // Dispose of any resources that can be recreated.
  619. }
  620. /*
  621. #pragma mark - Navigation
  622. // In a storyboard-based application, you will often want to do a little preparation before navigation
  623. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  624. // Get the new view controller using [segue destinationViewController].
  625. // Pass the selected object to the new view controller.
  626. }
  627. */
  628. @end