123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902 |
- //
- // LDAdWebViewController.m
- // YouHuiProject
- //
- // Created by 小花 on 2018/2/5.
- // Copyright © 2018年 kuxuan. All rights reserved.
- //
- #import "LDAdWebViewController.h"
- #import <AlibcTradeSDK/AlibcTradeSDK.h>
- #import <AlibabaAuthSDK/ALBBSession.h>
- #import <AlibabaAuthSDK/ALBBSDK.h>
- #import <WebKit/WebKit.h>
- #import "WebviewProgressLine.h"
- #import <JavaScriptCore/JavaScriptCore.h>
- #import "LDFeedbackController.h"
- #import "WechatShareTool.h"
- #import "WYWeakScriptMessageDelegate.h"
- #import "LDLoginViewController.h"
- #import "LDChildAccountViewController.h"
- #import <MJRefresh/UIScrollView+MJRefresh.h>
- static NSString *app_h5_close = @"app_h5_close";
- static NSString *app_h5_get_token = @"app_h5_get_token";
- static NSString *app_h5_wx_share = @"app_h5_wx_share";
- static NSString *app_h5_get_wechat_info = @"app_h5_get_wechat_info";
- static NSString *refresh_h5 = @"refresh_h5";
- static NSString *app_h5_login = @"app_h5_login"; //h5唤起原生的登录
- static NSString *app_h5_wx_share_url = @"app_h5_wx_share_url"; //分享url
- static NSString *app_h5_wx_share_image = @"app_h5_wx_share_image"; //分享到微信image
- static NSString *app_h5_wx_share_video = @"app_h5_wx_share_video"; //分享到微信video
- static NSString *app_h5_wx_share_min = @"app_h5_wx_share_min"; //分享微信小程序(只支持微信好友)
- static NSString *app_h5_open_taobao_url = @"app_h5_open_taobao_url"; //打开淘宝url
- static NSString *app_h5_open_goodDetail =@"app_h5_open_goodDetail";//唤起原生的详情页
- static NSString *app_h5_open_url =@"app_h5_open_url";//h5打开URL
- static NSString *app_h5_isLogin =@"app_h5_isLogin";//是否登录
- static NSString *app_h5_withdraw =@"app_h5_withdraw";//提现页面
- static NSString *openWeiChat =@"openWeiChat";//打开微信
- static NSString *app_h5_getDeviceInfo=@"app_h5_getDeviceInfo";//h5获取手机信息
- @interface LDAdWebViewController ()<WKUIDelegate, WKNavigationDelegate,WKScriptMessageHandler>
- @property (nonatomic, strong) WKWebView *webView;
- /** 加载进度条 */
- @property (nonatomic, strong) UIProgressView *progressView;
- @property (nonatomic, strong) UIButton *backButton;
- @property (nonatomic, strong) UIButton *closeButton;
- @property (nonatomic, strong) UIButton *refreshButton;
- @end
- @implementation LDAdWebViewController
- - (void)viewDidAppear:(BOOL)animated {
- [super viewDidAppear:animated];
- if ([_redStr isEqualToString:@"红包"]) {
- [_backButton setImage:nil forState:UIControlStateNormal];
- self.navigationBar.backgroundColor = [UIColor changeColor];
- self.navigationBar.showNavigationBarBottomLine = NO;
- [self.navigationBar setNavTitle:@"红包"];
- self.navigationBar.navTitleLabel.textColor = [UIColor whiteColor];
- [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
-
-
- }else{
-
- [_backButton setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
- self.navigationBar.backgroundColor = [UIColor clearColor];
- [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault];
- }
-
- }
- -(void)requestTabbar
- {
-
- [LDHttp post:[NSString stringWithFormat:@"%@/api/v2/redactive/redNewUrl",BaseURL] params:nil success:^(id json) {
- if ([[PhoneLoginManager shareManager].showPhoneBtn boolValue]) {
- self.url =[NSString stringWithFormat:@"%@?speical=true",json[@"data"][@"url"]];
- }else{
- self.url=json[@"data"][@"url"];
- }
-
- [self loadRequest];
-
- } failure:^(NSError *error) {
- }];
- }
- - (void)viewWillAppear:(BOOL)animated {
- [super viewWillAppear:animated];
- self.navigationBar.navTitleLabel.textColor = [UIColor whiteColor];
- [MobClick event:RedTabbar label:@"红包tabbar"];
-
- }
- - (void)viewDidLoad {
- [super viewDidLoad];
- self.navigationController.navigationBar.hidden = YES;
- [self addNotification];
- [self configNavigationBar];
- [self addUserhandler];
- [self loadRequest];
- if ([_redStr isEqualToString:@"红包"]) {
- [self requestTabbar];
- }
-
- }
- - (void)addNotification {
- [[NSNotificationCenter defaultCenter] addObserver:self
- selector:@selector(refreshH5Action)
- name:UIApplicationDidBecomeActiveNotification object:nil];
- [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(endFullScreen) name:UIWindowDidBecomeHiddenNotification object:nil];
- }
- //处理web视频全屏播放时 状态栏消失
- - (void)endFullScreen {
- [[UIApplication sharedApplication]setStatusBarHidden:false animated:false];
- }
- - (void)configNavigationBar {
-
- self.view.backgroundColor = [UIColor whiteColor];
- [self.view addSubview:self.webView];
- [self.view addSubview:self.progressView];
-
- self.navigationBar.showNavigationBarBottomLine = YES;
- [self.navigationBar setCustomLeftButtons:@[self.backButton]];
- [self.navigationBar setCustomRightButtons:@[self.refreshButton]];
-
-
- if (@available(iOS 11.0, *)) {
- self.webView.scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;//UIScrollView也适用
- }else {
- self.automaticallyAdjustsScrollViewInsets = NO;
- }
- }
- - (void)loadRequest {
- if (@available(iOS 9.0, *)) {
- NSArray * types=@[WKWebsiteDataTypeCookies,WKWebsiteDataTypeLocalStorage];
-
- NSSet *websiteDataTypes= [NSSet setWithArray:types];
- NSDate *dateFrom = [NSDate dateWithTimeIntervalSince1970:0];
-
- [[WKWebsiteDataStore defaultDataStore] removeDataOfTypes:websiteDataTypes modifiedSince:dateFrom completionHandler:^{
-
- }];
- }
- // NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:self.url]];
- NSString *urlStr = [self.url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
- NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:urlStr]
- cachePolicy:NSURLRequestReloadIgnoringCacheData
- timeoutInterval:15.0];
- [self.webView loadRequest:theRequest];
- }
- - (void)addUserhandler {
- [[self.webView configuration].userContentController addScriptMessageHandler:[[WYWeakScriptMessageDelegate alloc] initWithDelegate:self] name:app_h5_close];
- [[self.webView configuration].userContentController addScriptMessageHandler:[[WYWeakScriptMessageDelegate alloc] initWithDelegate:self] name:app_h5_get_token];
- [[self.webView configuration].userContentController addScriptMessageHandler:[[WYWeakScriptMessageDelegate alloc] initWithDelegate:self] name:app_h5_wx_share];
- [[self.webView configuration].userContentController addScriptMessageHandler:[[WYWeakScriptMessageDelegate alloc] initWithDelegate:self] name:app_h5_get_wechat_info];
-
- [[self.webView configuration].userContentController addScriptMessageHandler:[[WYWeakScriptMessageDelegate alloc] initWithDelegate:self] name:app_h5_login];
- [[self.webView configuration].userContentController addScriptMessageHandler:[[WYWeakScriptMessageDelegate alloc] initWithDelegate:self] name:app_h5_wx_share_url];
- [[self.webView configuration].userContentController addScriptMessageHandler:[[WYWeakScriptMessageDelegate alloc] initWithDelegate:self] name:app_h5_wx_share_image];
- [[self.webView configuration].userContentController addScriptMessageHandler:[[WYWeakScriptMessageDelegate alloc] initWithDelegate:self] name:app_h5_wx_share_video];
- [[self.webView configuration].userContentController addScriptMessageHandler:[[WYWeakScriptMessageDelegate alloc] initWithDelegate:self] name:app_h5_wx_share_min];
- [[self.webView configuration].userContentController addScriptMessageHandler:[[WYWeakScriptMessageDelegate alloc] initWithDelegate:self] name:app_h5_open_taobao_url];
-
- [[self.webView configuration].userContentController addScriptMessageHandler:[[WYWeakScriptMessageDelegate alloc] initWithDelegate:self] name:app_h5_open_goodDetail];
- [[self.webView configuration].userContentController addScriptMessageHandler:[[WYWeakScriptMessageDelegate alloc] initWithDelegate:self] name:app_h5_open_url];
- [[self.webView configuration].userContentController addScriptMessageHandler:[[WYWeakScriptMessageDelegate alloc] initWithDelegate:self] name:app_h5_isLogin];
- [[self.webView configuration].userContentController addScriptMessageHandler:[[WYWeakScriptMessageDelegate alloc] initWithDelegate:self] name:app_h5_withdraw];
- [[self.webView configuration].userContentController addScriptMessageHandler:[[WYWeakScriptMessageDelegate alloc] initWithDelegate:self] name:openWeiChat];
- [[self.webView configuration].userContentController addScriptMessageHandler:[[WYWeakScriptMessageDelegate alloc] initWithDelegate:self] name:app_h5_getDeviceInfo];
- }
- #pragma mark --- WKScriptMessageHandler
- - (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message {
- if ([message.name isEqualToString:app_h5_close]) {
- //关闭页面
- [self.navigationController popViewControllerAnimated:YES];
- }
- else if ([message.name isEqualToString:app_h5_get_token]){
- //传token给js
- [self sendUserTokenToJS:message];
- }
- else if ([message.name isEqualToString:app_h5_wx_share]){
- //调起原生分享
- [self shareToNative:message];
- }
- else if ([message.name isEqualToString:app_h5_get_wechat_info]){
- //获取微信信息
- [self getWechatInfoWithMessage:message];
- }
- else if ([message.name isEqualToString:app_h5_wx_share_image]){
- //分享图片
- [self shareImage:message];
- }
- else if ([message.name isEqualToString:app_h5_wx_share_url]){
- //分享url
- [self shareURL:message];
- }
- else if ([message.name isEqualToString:app_h5_wx_share_video]){
- //分享video
- [self shareVideo:message];
- }
- else if ([message.name isEqualToString:app_h5_wx_share_min]){
- //分享小程序
- [self shareMinProgram:message];
-
- }else if ([message.name isEqualToString:app_h5_login]){
- //获取token
- [self gotoNativeLogin:message];
-
- }else if ([message.name isEqualToString:app_h5_open_taobao_url]){
- //打开淘宝
- [self openTaobao:message];
- }else if ([message.name isEqualToString:app_h5_open_goodDetail] )
- {
- [self goodDetail:message];
- }else if ([message.name isEqualToString:app_h5_open_url] )
- {//h5打开URL
- [self openUrl:message];
- }else if ([message.name isEqualToString:app_h5_isLogin] )
- {
- [self sendUserTokenISLoginToJS:message];
- }else if ([message.name isEqualToString:app_h5_withdraw] )//提现
- {
- [self withdrawal:message];
-
- }else if ([message.name isEqualToString:openWeiChat] )//打开微信
- {
- [self callWeChat];
- }else if ([message.name isEqualToString:app_h5_getDeviceInfo] )//获取手机信息
- {
- [self getDeviceInfo:message];
- }
- else {
-
- }
- }
- //调起微信
- - (void)callWeChat
- {
- NSString* WeChatUrl = [NSString stringWithFormat:@"weixin://"];
- UIWebView* webview = [[UIWebView alloc]initWithFrame:CGRectZero];
- [[UIApplication sharedApplication].keyWindow addSubview:webview];
- NSURL*url = [NSURL URLWithString:WeChatUrl];
- NSURLRequest *request = [NSURLRequest requestWithURL:url];
- [webview loadRequest:request];
-
- }
- //h5打开URL
- -(void)openUrl:(WKScriptMessage *)message
- {
- NSDictionary *dict = [self dictionaryWithJsonString:message.body];
- NSString *jsMethod = dict[@"push_url"];
- LDAdWebViewController *web =[[LDAdWebViewController alloc]init];
- web.url=jsMethod;
- [self.navigationController pushViewController:web animated:YES];
- }
- /**
- 传token判断登录给js
- */
- - (void)sendUserTokenISLoginToJS:(WKScriptMessage *)message {
-
- __block NSString *token = [AccountTool account].token;
-
-
- NSDictionary *dict = [self dictionaryWithJsonString:message.body];
- NSString *jsMethod = dict[@"js_callback"];
- if (!token) {
- token = @"";
- }
- NSString *islogin=@"false";
- if ([AccountTool isLogin] ) {
- islogin = @"true";
- }
- NSDictionary *tokenPara = @{@"token":token,
- @"isLogin":islogin
- };
- NSString *jsonDict = [self convertToJsonData:tokenPara];
- NSString *callBacKMethod = [NSString stringWithFormat:@"%@('%@')",jsMethod,jsonDict];
- [self.webView evaluateJavaScript:callBacKMethod completionHandler:^(id _Nullable value, NSError * _Nullable error) {
-
- }];
-
- }
- - (void)refreshH5Action {
-
- if (@available(iOS 9.0, *)) {
- NSArray * types=@[WKWebsiteDataTypeCookies,WKWebsiteDataTypeLocalStorage];
-
- NSSet *websiteDataTypes= [NSSet setWithArray:types];
- NSDate *dateFrom = [NSDate dateWithTimeIntervalSince1970:0];
-
- [[WKWebsiteDataStore defaultDataStore] removeDataOfTypes:websiteDataTypes modifiedSince:dateFrom completionHandler:^{
-
- }];
- }
-
-
- NSString *callBacKMethod = [NSString stringWithFormat:@"%@()",refresh_h5];
- [self.webView evaluateJavaScript:callBacKMethod completionHandler:^(id _Nullable value, NSError * _Nullable error) {
-
- }];
- }
- //提现页面
- -(void)withdrawal:(WKScriptMessage *)message
- {
- if (![AccountTool isLogin]) {
- LDNewLoginVC *login = [[LDNewLoginVC alloc] init];
- login.loginSucc = ^{
- NSDictionary *dict = [self dictionaryWithJsonString:message.body];
- NSString *jsMethod = dict[@"js_callback"];
-
- NSString *token = [AccountTool account].token;
- NSDictionary *param = @{@"token":token,
- };
- NSString *jsonDict = [self convertToJsonData:param];
- NSString *callBacKMethod = [NSString stringWithFormat:@"%@('%@')",jsMethod,jsonDict];
- [self.webView evaluateJavaScript:callBacKMethod completionHandler:^(id _Nullable value, NSError * _Nullable error) {
-
- }];
- };
- login.modalPresentationStyle = UIModalPresentationFullScreen;
- [self presentViewController:login animated:YES completion:nil];
- return;
- }
- LDChildAccountViewController *accountDetail = [[LDChildAccountViewController alloc] init];//提现
- [self.navigationController pushViewController:accountDetail animated:YES];
- }
- /**
- 传token给js
- */
- - (void)sendUserTokenToJS:(WKScriptMessage *)message {
-
- __block NSString *token = [AccountTool account].token;
- NSString *openid = [AccountTool account].openId;
- NSString *unionid = [AccountTool account].unionid;
- if (![AccountTool isLogin] || openid==nil || unionid ==nil) {
- LDNewLoginVC *login = [[LDNewLoginVC alloc] init];
- login.loginSucc = ^{
- NSDictionary *dict = [self dictionaryWithJsonString:message.body];
- NSString *jsMethod = dict[@"js_callback"];
- if (!token) {
- token = @"";
- }
- NSDictionary *tokenPara = @{@"token":token,
- @"openid":openid,
- @"unionid":unionid,
- };
- NSString *jsonDict = [self convertToJsonData:tokenPara];
- NSString *callBacKMethod = [NSString stringWithFormat:@"%@('%@')",jsMethod,jsonDict];
- [self.webView evaluateJavaScript:callBacKMethod completionHandler:^(id _Nullable value, NSError * _Nullable error) {
-
- }];
- };
- login.modalPresentationStyle = UIModalPresentationFullScreen;
- [self presentViewController:login animated:YES completion:nil];
- return;
- }
- NSDictionary *dict = [self dictionaryWithJsonString:message.body];
- NSString *jsMethod = dict[@"js_callback"];
- if (!token) {
- token = @"";
- }
- NSDictionary *tokenPara = @{@"token":token,
- @"openid":openid,
- @"unionid":unionid,
- };
- NSString *jsonDict = [self convertToJsonData:tokenPara];
- NSString *callBacKMethod = [NSString stringWithFormat:@"%@('%@')",jsMethod,jsonDict];
- [self.webView evaluateJavaScript:callBacKMethod completionHandler:^(id _Nullable value, NSError * _Nullable error) {
-
- }];
-
- }
- //商品详情页
- -(void)goodDetail:(WKScriptMessage *)message
- {
- NSDictionary *dict = [self dictionaryWithJsonString:message.body];
- DetailRequestModel *model =[[DetailRequestModel alloc]init];
- [model setValuesForKeysWithDictionary:dict];
-
- LDGoodDetailViewController *detail = [[LDGoodDetailViewController alloc] init];
- detail.requestModel = model;
-
- [self.navigationController pushViewController:detail animated:YES];
- }
- #pragma mark---获取手机信息
- -(void)getDeviceInfo:(WKScriptMessage *)message
- {
- NSDictionary *dict = [self dictionaryWithJsonString:message.body];
- NSString *jsMethod = dict[@"js_callback"];
-
- NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
- NSString *appVersion = [infoDictionary objectForKey:@"CFBundleShortVersionString"];
-
- // build版本号
- NSString *appBuild = [infoDictionary objectForKey:@"CFBundleVersion"];
-
- NSDictionary *param = @{
- @"versionCode":appVersion,//版本号
- @"versionName":appBuild,//版本名
- @"deviceWidth":[NSString stringWithFormat:@"%.2f",SCREEN_WIDTH] ,//屏幕宽
- @"deviceHeight":[NSString stringWithFormat:@"%.2f",SCREEN_HEIGHT] ,//屏幕高
- @"deviceBrand":@"iPhone", //品牌
- @"deviceModel":[LDDeviceTools getCurrentDeviceModel], //型号
- @"deviceVersion":[[UIDevice currentDevice] systemVersion],// 版本
- };
- NSString *jsonDict = [self convertToJsonData:param];
- NSString *callBacKMethod = [NSString stringWithFormat:@"%@('%@')",jsMethod,jsonDict];
- [self.webView evaluateJavaScript:callBacKMethod completionHandler:^(id _Nullable value, NSError * _Nullable error) {
-
- }];
- }
- /**
- 唤起登录
- */
- - (void)gotoNativeLogin:(WKScriptMessage *)message {
- if (![AccountTool isLogin]) {
- LDNewLoginVC *login = [[LDNewLoginVC alloc] init];
- login.loginSucc = ^{
- NSDictionary *dict = [self dictionaryWithJsonString:message.body];
- NSString *jsMethod = dict[@"js_callback"];
-
- NSString *token = [AccountTool account].token;
- NSDictionary *param = @{@"token":token,
- };
- NSString *jsonDict = [self convertToJsonData:param];
- NSString *callBacKMethod = [NSString stringWithFormat:@"%@('%@')",jsMethod,jsonDict];
- [self.webView evaluateJavaScript:callBacKMethod completionHandler:^(id _Nullable value, NSError * _Nullable error) {
-
- }];
- };
- login.modalPresentationStyle = UIModalPresentationFullScreen;
- [self presentViewController:login animated:YES completion:nil];
- return;
- }
- NSDictionary *dict = [self dictionaryWithJsonString:message.body];
- NSString *jsMethod = dict[@"js_callback"];
-
- NSString *token = [AccountTool account].token;
- NSDictionary *param = @{@"token":token,
- };
- NSString *jsonDict = [self convertToJsonData:param];
- NSString *callBacKMethod = [NSString stringWithFormat:@"%@('%@')",jsMethod,jsonDict];
- [self.webView evaluateJavaScript:callBacKMethod completionHandler:^(id _Nullable value, NSError * _Nullable error) {
-
- }];
- }
- /**
- 获取微信信息
- */
- - (void)getWechatInfoWithMessage:(WKScriptMessage *)message {
-
- NSString *openid = [AccountTool account].openId;
- NSString *unionid = [AccountTool account].unionid;
- if (![AccountTool isLogin] || openid==nil || unionid ==nil) {
- LDNewLoginVC *login = [[LDNewLoginVC alloc] init];
- login.loginSucc = ^{
- NSDictionary *dict = [self dictionaryWithJsonString:message.body];
- NSString *jsMethod = dict[@"js_callback"];
-
-
- NSDictionary *param = @{@"openid":openid,
- @"unionid":unionid
- };
- NSString *jsonDict = [self convertToJsonData:param];
- NSString *callBacKMethod = [NSString stringWithFormat:@"%@('%@')",jsMethod,jsonDict];
- [self.webView evaluateJavaScript:callBacKMethod completionHandler:^(id _Nullable value, NSError * _Nullable error) {
-
- }];
- };
- login.modalPresentationStyle = UIModalPresentationFullScreen;
- [self presentViewController:login animated:YES completion:nil];
- return;
- }
- NSDictionary *dict = [self dictionaryWithJsonString:message.body];
- NSString *jsMethod = dict[@"js_callback"];
-
-
- NSDictionary *param = @{@"openid":openid,
- @"unionid":unionid
- };
- NSString *jsonDict = [self convertToJsonData:param];
- NSString *callBacKMethod = [NSString stringWithFormat:@"%@('%@')",jsMethod,jsonDict];
- [self.webView evaluateJavaScript:callBacKMethod completionHandler:^(id _Nullable value, NSError * _Nullable error) {
-
- }];
- }
- /**
- 调起分享
- */
- - (void)shareToNative:(WKScriptMessage *)message {
- NSDictionary *dict = [self dictionaryWithJsonString:message.body];
- if ([dict[@"shareType"] isEqualToString:@"1"]) {
- [self shareImageWithplatformType:UMSocialPlatformType_WechatSession withDict:dict];
- }else {
- [self shareImageWithplatformType:UMSocialPlatformType_WechatTimeLine withDict:dict];
- }
- }
- - (void)shareImage:(WKScriptMessage *)message {
- NSDictionary *dict = [self dictionaryWithJsonString:message.body];
- if ([dict[@"shareType"] isEqualToString:@"1"]) {
- [WechatShareTool shareImageWithplatformType:UMSocialPlatformType_WechatSession withDict:dict];
- }else {
- [WechatShareTool shareImageWithplatformType:UMSocialPlatformType_WechatTimeLine withDict:dict];
- }
- }
- - (void)shareURL:(WKScriptMessage *)message {
- NSDictionary *dict = [self dictionaryWithJsonString:message.body];
- if ([dict[@"shareType"] isEqualToString:@"1"]) {
- [WechatShareTool shareURLWithplatformType:UMSocialPlatformType_WechatSession withDict:dict];
- }else {
- [WechatShareTool shareURLWithplatformType:UMSocialPlatformType_WechatTimeLine withDict:dict];
- }
- }
- - (void)shareVideo:(WKScriptMessage *)message {
- NSDictionary *dict = [self dictionaryWithJsonString:message.body];
- if ([dict[@"shareType"] isEqualToString:@"1"]) {
- [WechatShareTool shareVideoWithplatformType:UMSocialPlatformType_WechatSession withDict:dict];
- }else {
- [WechatShareTool shareVideoWithplatformType:UMSocialPlatformType_WechatTimeLine withDict:dict];
- }
- }
- - (void)shareMinProgram:(WKScriptMessage *)message {
- NSDictionary *dict = [self dictionaryWithJsonString:message.body];
- [WechatShareTool shareMinProgramWithplatformType:UMSocialPlatformType_WechatSession withDict:dict];
- }
- - (void)openTaobao:(WKScriptMessage *)message {
- NSDictionary *dict = [self dictionaryWithJsonString:message.body];
- NSString *url = dict[@"taobaoUrl"];
- [self jumpToTaobaoWithUrl:url];
- }
- - (void)jumpToTaobaoWithUrl:(NSString *)url {
-
- id<AlibcTradePage> page = [AlibcTradePageFactory page:url];
- //淘客信息
- AlibcTradeTaokeParams *taoKeParams=[[AlibcTradeTaokeParams alloc] init];
- taoKeParams.pid= ALTK_PID;
- //打开方式
- AlibcTradeShowParams* showParam = [[AlibcTradeShowParams alloc] init];
-
- showParam.openType = AlibcOpenTypeNative;
-
- [[AlibcTradeSDK sharedInstance].tradeService openByUrl:url identity:@"trade" webView:nil parentController:self.parentViewController showParams:showParam taoKeParams:taoKeParams trackParam:nil tradeProcessSuccessCallback:^(AlibcTradeResult * _Nullable result) {
-
- } tradeProcessFailedCallback:^(NSError * _Nullable error) {
-
- }];
-
- }
- -(void)shareImageWithplatformType:(UMSocialPlatformType)platformType withDict:(NSDictionary *)dict{
- //创建分享消息对象
- UMSocialMessageObject *messageObject = [UMSocialMessageObject messageObject];
- //创建图片内容对象
- UMShareWebpageObject *shareObject = [UMShareWebpageObject shareObjectWithTitle:dict[@"shareTitle"] descr:dict[@"shareDescirpt"] thumImage:dict[@"shareImageUrl"]];
- shareObject.webpageUrl = dict[@"shareUrl"];
- messageObject.shareObject = shareObject;
- [[UMSocialManager defaultManager] shareToPlatform:platformType messageObject:messageObject currentViewController:nil completion:^(id data, NSError *error) {
-
- }];
- }
- /**
- kvo监听进度条
-
- @param keyPath keyPath description
- @param object object description
- @param change change description
- @param context context description
- */
- - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context{
- if ([keyPath isEqualToString:NSStringFromSelector(@selector(estimatedProgress))] && object == self.webView ) {
- [self.progressView setAlpha:1.0];
-
- [self.progressView setProgress:self.webView.estimatedProgress ];
- if (self.webView.estimatedProgress >= 1.0) {
- [UIView animateWithDuration:0.7 animations:^{
- [self.progressView setProgress:1.0 animated:YES];
- [self.progressView setAlpha:0.0];
-
- }];
-
- }
- }else{
- [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
- }
- }
- #pragma mark-
- #pragma mark- WKNavigationDelegate delegate
- /**
- 开始加载web的时候
-
- @param webView webView description
- @param navigation navigation description
- */
- - (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation{
- //开始加载的时候,让进度条显示
- self.progressView.hidden = NO;
- self.progressView.progress = 0;
- self.progressView.alpha = 1.0;
- [UIView animateWithDuration:0.8 animations:^{
- self.progressView.progress = 0.6;
-
- }];
- }
- /**
- 当网页加载完成的时候调用
-
- @param webView web描述
- @param navigation 导航的描述
- */
- - (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation{
- if ([_redStr isEqualToString:@"红包"]) {
- [self.navigationBar setNavTitle:@"红包"];
- self.navigationBar.navTitleLabel.textColor = [UIColor whiteColor];
- }else{
- [self.navigationBar setNavTitle:self.webView.title];
- }
- [[UIApplication sharedApplication]setNetworkActivityIndicatorVisible:NO];
- [self updateNavigationBarButtons];
- [self.webView.scrollView.mj_header endRefreshing];
- }
- -(void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler
- {
- NSURL *url = navigationAction.request.URL;
- UIApplication *app = [UIApplication sharedApplication];
-
- // 打开appstore
- if ([url.absoluteString containsString:@"https://itunes.apple.com/cn/app/"])
- {
- if ([app canOpenURL:url])
- {
- //设备系统为IOS 10.0或者以上的
- if (@available(iOS 10.0, *)) {
- [[UIApplication sharedApplication] openURL:url options:@{} completionHandler:nil];
- } else {
- [[UIApplication sharedApplication] openURL:url];
- }
-
- decisionHandler(WKNavigationActionPolicyCancel);
- return;
- }
- }
- decisionHandler(WKNavigationActionPolicyAllow);
-
-
-
- }
- #pragma mark -----------
- - (void)updateNavigationBarButtons {
- if (self.webView.canGoBack) {
- [self.navigationBar setCustomLeftButtons:@[self.backButton,self.closeButton]];
- }else {
- [self.navigationBar setCustomLeftButtons:@[self.backButton]];
- }
- }
- - (void)backAction {
- NSArray *viewControllers = self.navigationController.viewControllers;
- if (viewControllers.count < 1){
- [self dismissViewControllerAnimated:YES completion:nil];
- }else {
- if (self.webView.canGoBack) {
- [self.webView goBack];
- }else{
- [self.navigationController popViewControllerAnimated:YES];
- }
- }
-
-
- }
- - (void)closeAction {
- [self.navigationController popViewControllerAnimated:YES];
- }
- -(void)redRuleClickAction
- {
- LDAdWebViewController *webV =[[LDAdWebViewController alloc]init];
- webV.url=redRulesUrl;
- [self.navigationController pushViewController:webV animated:YES];
- }
- - (void)refreshAction {
-
- [self.webView reload];
- }
- - (WKWebView *)webView{
- if (!_webView) {
-
- WKWebViewConfiguration *config = [[WKWebViewConfiguration alloc] init];
- config.allowsInlineMediaPlayback = NO;
- _webView = [[WKWebView alloc]initWithFrame:CGRectMake(0, NavBarHeight, SCREEN_WIDTH, SCREEN_HEIGHT-NavBarHeight) configuration:config];
- if ([_redStr isEqualToString:@"红包"]) {
- _webView.frame=CGRectMake(0, NavBarHeight, SCREEN_WIDTH, SCREEN_HEIGHT-NavBarHeight-TabbarHeight);
- }
-
-
- _webView.backgroundColor = [UIColor colorWithRed:240.0/255 green:240.0/255 blue:240.0/255 alpha:1.0];
- _webView.navigationDelegate = self;
- _webView.UIDelegate = self;
- //使用kvo监听进度
- [_webView addObserver:self forKeyPath:NSStringFromSelector(@selector(estimatedProgress)) options:0 context:nil];
- //手势触摸滑动
- _webView.allowsBackForwardNavigationGestures = YES;
- //自适应
- [_webView sizeToFit];
-
- self.webView.scrollView.mj_header =[MJRefreshNormalHeader headerWithRefreshingBlock:^{
- [self refreshAction];
- }];
- }
- return _webView;
- }
- #pragma mark-
- #pragma mark- SetupConstraints
- /** 加载配置以及视图添加*/
- - (void)loadSubViewsConfiguration{
-
- [self.view addSubview:self.webView];
- }
- - (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;
- }
- - (UIButton *)refreshButton {
- if (!_refreshButton) {
- _refreshButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
- [_refreshButton setImage:[UIImage imageNamed:@"icon_web_refresh"] forState:UIControlStateNormal];
- [_refreshButton addTarget:self action:@selector(refreshAction) forControlEvents:UIControlEventTouchUpInside];
- if ([_redStr isEqualToString:@"红包"]) {
- _refreshButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 90, 40)];
- [_refreshButton setImage:[UIImage imageNamed:@"Doubt"] forState:UIControlStateNormal];
- _refreshButton.titleLabel.font =[UIFont systemFontOfSize:13];
- [_refreshButton setTitle:@"关于红包" forState:UIControlStateNormal];
- [_refreshButton setButtonImageTitleStyle:ButtonImageTitleStyleRight padding:5];
- [_refreshButton addTarget:self action:@selector(redRuleClickAction) forControlEvents:UIControlEventTouchUpInside];
-
- }else{
-
- [self.refreshButton setImage:[UIImage imageNamed:@"icon_web_refresh"] forState:UIControlStateNormal];
-
- self.refreshButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
-
- }
- }
- return _refreshButton;
- }
- - (UIProgressView *)progressView{
- if (!_progressView) {
- _progressView = [[UIProgressView alloc]initWithProgressViewStyle:UIProgressViewStyleDefault];
- _progressView.frame = CGRectMake(0, NavBarHeight, self.view.frame.size.width, 2);
- //设置进度条的色彩
- [_progressView setTrackTintColor:[UIColor colorWithRed:240.0/255 green:240.0/255 blue:240.0/255 alpha:1.0]];
- _progressView.progressTintColor = [UIColor homeRedColor];
- }
- return _progressView;
- }
- //观察的移除
- - (void)dealloc{
- [self.webView removeObserver:self forKeyPath:NSStringFromSelector(@selector(estimatedProgress))];
- [[NSNotificationCenter defaultCenter] removeObserver:self];
- }
- /**
- json字符串转字典
- */
- - (NSDictionary *)dictionaryWithJsonString:(NSString *)jsonString
- {
- if (jsonString == nil) {
- return nil;
- }
-
- NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
- NSError *err;
- NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:jsonData
- options:NSJSONReadingMutableContainers
- error:&err];
- if(err)
- {
- NSLog(@"json解析失败:%@",err);
- return nil;
- }
- return dic;
- }
- /**
- 字典转json字符串
- */
- - (NSString *)convertToJsonData:(NSDictionary *)dict
- {
-
- NSError *error;
-
- NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dict options:NSJSONWritingPrettyPrinted error:&error];
-
- NSString *jsonString;
-
- if (!jsonData) {
-
- NSLog(@"%@",error);
-
- }else{
-
- jsonString = [[NSString alloc]initWithData:jsonData encoding:NSUTF8StringEncoding];
-
- }
-
- NSMutableString *mutStr = [NSMutableString stringWithString:jsonString];
-
- NSRange range = {0,jsonString.length};
-
- //去掉字符串中的空格
-
- [mutStr replaceOccurrencesOfString:@" " withString:@"" options:NSLiteralSearch range:range];
-
- NSRange range2 = {0,mutStr.length};
-
- //去掉字符串中的换行符
-
- [mutStr replaceOccurrencesOfString:@"\n" withString:@"" options:NSLiteralSearch range:range2];
-
- return mutStr;
-
- }
- - (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
|