《省钱达人》与《猎豆优选》UI相同版。域名tbk

DRAdWebViewController.m 24KB

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