// // KBWebPageViewController.m // YouHuiProject // // Created by 小花 on 2018/1/31. // Copyright © 2018年 kuxuan. All rights reserved. // #import "KBWebPageViewController.h" #import #import #import #import "KBTaobaoAuthorView.h" #import "KBShopPushWebViewController.h" #import "JsonTool.h" @interface KBWebPageViewController (){ ActivityIndicatorView *_indicatorView; } @property (nonatomic, copy) NSString *jsString; @property (nonatomic, strong) UIButton *backButton; @property (nonatomic, strong) UIButton *closeButton; @end @implementation KBWebPageViewController - (instancetype)init { self = [super init]; if (self) { } return self; } - (void)viewWillDisappear:(BOOL)animated { [super viewWillDisappear:animated]; [SVProgressHUD dismiss]; } - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; } - (void)viewDidLoad { [super viewDidLoad]; [self cleanCacheAndCookie]; [self addObserForAppWillEnterForeground]; [self configNavigationBar]; [self configWebView]; [self initHUD]; [self configTaobaoAuthorView]; } - (void)addObserForAppWillEnterForeground { [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reloadWebNoti) name:UIApplicationWillEnterForegroundNotification object:nil]; } /** 从后台进入前台刷新 */ - (void)reloadWebNoti { if ([[ALBBSession sharedInstance] isLogin]){ [self openAliMyOrderWebView]; } } /** 清除web缓存 */ - (void)cleanCacheAndCookie{ //清除cookies // NSHTTPCookie *cookie; // // NSHTTPCookieStorage *storage = [NSHTTPCookieStorage sharedHTTPCookieStorage]; // // for (cookie in [storage cookies]){ // // [storage deleteCookie:cookie]; // // } //清除UIWebView的缓存 [[NSURLCache sharedURLCache] removeAllCachedResponses]; NSURLCache * cache = [NSURLCache sharedURLCache]; [cache removeAllCachedResponses]; [cache setDiskCapacity:0]; [cache setMemoryCapacity:0]; } - (void)configTaobaoAuthorView { [self openAliMyOrderWebView]; } - (void)loadHTMLJSSting { NSString *url = [NSString stringWithFormat:@"%@/api/v2/goods/backJsStr",BaseURL]; [KBHttp post:url params:nil success:^(id json) { self.jsString = json[@"jsStr"]; [self getGoosList]; } failure:^(NSError *error) { }]; } - (void)initHUD { ActivityIndicatorView *indicatorView = [ActivityIndicatorView showInView:self.webView frame:self.webView.bounds]; _indicatorView = indicatorView; } - (void)configWebView { self.webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, NavBarHeight, SCREEN_WIDTH, SCREEN_HEIGHT-NavBarHeight-TabbarHeight)]; self.webView.delegate = self; [self.view addSubview:self.webView]; } - (void)configNavigationBar { self.view.backgroundColor = [UIColor whiteColor]; self.navigationBar.showNavigationBarBottomLine = YES; [self.navigationBar setCustomLeftButtons:@[self.backButton]]; } - (void)backAction { if (self.webView.canGoBack) { [self.webView goBack]; }else{ [self.navigationController popViewControllerAnimated:YES]; } } - (void)closeAction { [self.navigationController popViewControllerAnimated:YES]; } - (void)updateNavigationBarButtons { if (self.webView.canGoBack) { [self.navigationBar setCustomLeftButtons:@[self.backButton,self.closeButton]]; }else { [self.navigationBar setCustomLeftButtons:@[self.backButton]]; } } - (void)openAliMyOrderWebView { // [SVProgressHUD show]; switch (self.openPage) { case AlibcOpenPageShopCar: { id page = [AlibcTradePageFactory myCartsPage]; [self openWebViewByPage:page]; } break; case AlibcOpenPageMyOrder: { id page = [AlibcTradePageFactory myOrdersPage:0 isAllOrder:YES]; [self openWebViewByPage:page]; } break; default: { id page = [AlibcTradePageFactory myOrdersPage:0 isAllOrder:YES]; [self openWebViewByPage:page]; } break; } } - (void)openWebViewByPage:(id)page { //淘客信息 AlibcTradeTaokeParams *taoKeParams=[[AlibcTradeTaokeParams alloc] init]; taoKeParams.pid = ALTK_PID; //打开方式 AlibcTradeShowParams* showParam = [[AlibcTradeShowParams alloc] init]; showParam.openType = AlibcOpenTypeH5; [[AlibcTradeSDK sharedInstance].tradeService show:self webView:self.webView page:page showParams:showParam taoKeParams:taoKeParams trackParam:nil tradeProcessSuccessCallback:^(AlibcTradeResult * _Nullable result) { } tradeProcessFailedCallback:^(NSError * _Nullable error) { }]; self.webView.delegate = self; } #pragma mark -------------- UIWebView delegate -------- #pragma mark -- 拦截webview用户触击了一个链接 - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType { //判断是否是单击 if (navigationType == UIWebViewNavigationTypeLinkClicked) { NSString *url = [request.URL absoluteString]; //拦截链接跳转到货源圈的动态详情 if ([url rangeOfString:@"http"].location != NSNotFound) { //跳转到你想跳转的页面 KBShopPushWebViewController *web = [[KBShopPushWebViewController alloc] init]; web.url = url; [self.navigationController pushViewController:web animated:YES]; return NO; //返回NO,此页面的链接点击不会继续执行,只会执行跳转到你想跳转的页面 } } return YES; } - (void)webViewDidFinishLoad:(UIWebView *)webView { [self updateNavigationBarButtons]; // [SVProgressHUD dismiss]; NSString *jsToGetHTMLSource = @"document.getElementsByTagName('html')[0].innerHTML"; NSString *HTMLSource = [self.webView stringByEvaluatingJavaScriptFromString:jsToGetHTMLSource]; if (self.openPage == AlibcOpenPageShopCar) { [self loadHTMLJSSting]; } [_indicatorView stopAnimating]; } - (void)getGoosList { if (self.jsString.length == 0) { return; } [self.webView stringByEvaluatingJavaScriptFromString:self.jsString]; NSString *goodsStr = [self.webView stringByEvaluatingJavaScriptFromString:@"getShopCarGoods();"]; NSDictionary *dic = [JsonTool dictionaryWithJsonString:goodsStr]; NSArray *goods = dic[@"goods"]; if (goods.count > 0) { [self uploadGoodsId:goods]; }else { NSDictionary *userInfo = @{@"goodsIdStr":@[]}; NSNotification *noti = [[NSNotification alloc] initWithName:ShopCarCompleteKey object:nil userInfo:userInfo]; [[NSNotificationCenter defaultCenter] postNotification:noti]; } } - (void)uploadGoodsId:(NSArray *)goodArr { NSDictionary *userInfo = @{@"goodsIdStr":goodArr}; NSNotification *noti = [[NSNotification alloc] initWithName:ShopCarCompleteKey object:nil userInfo:userInfo]; [[NSNotificationCenter defaultCenter] postNotification:noti]; } - (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error { [SVProgressHUD dismiss]; } #pragma mark ------ - (UIButton *)backButton { if (!_backButton) { _backButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40, 40)]; [_backButton setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal]; [_backButton addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside]; } return _backButton; } - (UIButton *)closeButton { if (!_closeButton) { _closeButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40, 40)]; [_closeButton setImage:[UIImage imageNamed:@"close_web"] forState:UIControlStateNormal]; [_closeButton addTarget:self action:@selector(closeAction) forControlEvents:UIControlEventTouchUpInside]; } return _closeButton; } - (NSArray *)stringToJSON:(NSString *)jsonStr { if (jsonStr) { id tmp = [NSJSONSerialization JSONObjectWithData:[jsonStr dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingAllowFragments | NSJSONReadingMutableLeaves | NSJSONReadingMutableContainers error:nil]; if (tmp) { if([tmp isKindOfClass:[NSArray class]]) { return tmp; }else if([tmp isKindOfClass:[NSString class]]|| [tmp isKindOfClass:[NSDictionary class]]){ return [NSArray arrayWithObject:tmp]; } else { return nil; } }else { return nil; } } else { return nil; } } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } /* #pragma mark - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { // Get the new view controller using [segue destinationViewController]. // Pass the selected object to the new view controller. } */ @end