口袋优选

KBShopPushWebViewController.m 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. //
  2. // KBShopPushWebViewController.m
  3. // YouHuiProject
  4. //
  5. // Created by 小花 on 2018/11/5.
  6. // Copyright © 2018年 kuxuan. All rights reserved.
  7. //
  8. #import "KBShopPushWebViewController.h"
  9. #import <AlibcTradeSDK/AlibcTradeSDK.h>
  10. #import <AlibabaAuthSDK/ALBBSession.h>
  11. #import <AlibabaAuthSDK/ALBBSDK.h>
  12. #import "WebviewProgressLine.h"
  13. @interface KBShopPushWebViewController ()<UIWebViewDelegate>
  14. @property (nonatomic, strong) UIWebView *webView;
  15. @property (nonatomic, strong) UIButton *backButton;
  16. /** 加载进度条 */
  17. @property (nonatomic,strong) WebviewProgressLine *progressLine;
  18. @end
  19. @implementation KBShopPushWebViewController
  20. - (void)viewWillAppear:(BOOL)animated {
  21. [super viewWillAppear:animated];
  22. [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;
  23. }
  24. - (void)viewDidLoad {
  25. [super viewDidLoad];
  26. [self configWebView];
  27. [self configNavigationBar];
  28. [self openUrl];
  29. }
  30. - (void)configWebView {
  31. self.webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, NavBarHeight, SCREEN_WIDTH, SCREEN_HEIGHT-NavBarHeight)];
  32. self.webView.delegate = self;
  33. [self.view addSubview:self.webView];
  34. }
  35. - (void)configNavigationBar {
  36. self.view.backgroundColor = [UIColor whiteColor];
  37. self.navigationBar.showNavigationBarBottomLine = YES;
  38. [self.navigationBar setCustomLeftButtons:@[self.backButton]];
  39. self.progressLine = [[WebviewProgressLine alloc] initWithFrame:CGRectMake(0, NavBarHeight, SCREEN_WIDTH, 3)];
  40. self.progressLine.lineColor = [UIColor YHColorWithHex:0xCC0E0E];
  41. [self.view addSubview:self.progressLine];
  42. }
  43. - (void)backAction {
  44. [self.navigationController popViewControllerAnimated:YES];
  45. }
  46. - (void)openUrl {
  47. id<AlibcTradePage> page = [AlibcTradePageFactory page:self.url];
  48. [self openWebViewByPage:page];
  49. }
  50. - (void)openWebViewByPage:(id<AlibcTradePage>)page {
  51. //淘客信息
  52. AlibcTradeTaokeParams *taoKeParams=[[AlibcTradeTaokeParams alloc] init];
  53. taoKeParams.pid = ALTK_PID;
  54. //打开方式
  55. AlibcTradeShowParams* showParam = [[AlibcTradeShowParams alloc] init];
  56. showParam.openType = AlibcOpenTypeH5;
  57. [[AlibcTradeSDK sharedInstance].tradeService show:self webView:self.webView page:page showParams:showParam taoKeParams:taoKeParams trackParam:nil tradeProcessSuccessCallback:^(AlibcTradeResult * _Nullable result) {
  58. } tradeProcessFailedCallback:^(NSError * _Nullable error) {
  59. }];
  60. self.webView.delegate = self;
  61. }
  62. #pragma mark -------------- UIWebView delegate --------
  63. - (void)webViewDidStartLoad:(UIWebView *)webView {
  64. [self.progressLine startLoadingAnimation];
  65. }
  66. #pragma mark -- 拦截webview用户触击了一个链接
  67. - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
  68. {
  69. //判断是否是单击
  70. if (navigationType == UIWebViewNavigationTypeLinkClicked)
  71. {
  72. NSString *url = [request.URL absoluteString];
  73. //拦截链接跳转到动态详情
  74. if ([url rangeOfString:@"http"].location != NSNotFound)
  75. {
  76. //跳转到你想跳转的页面
  77. KBShopPushWebViewController *web = [[KBShopPushWebViewController alloc] init];
  78. web.url = url;
  79. [self.navigationController pushViewController:web animated:YES];
  80. return NO; //返回NO,此页面的链接点击不会继续执行,只会执行跳转到你想跳转的页面
  81. }
  82. }
  83. return YES;
  84. }
  85. - (void)webViewDidFinishLoad:(UIWebView *)webView {
  86. NSString *title = [webView stringByEvaluatingJavaScriptFromString:@"document.title"];
  87. [self.navigationBar setNavTitle:title];
  88. [self.progressLine endLoadingAnimation];
  89. }
  90. - (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
  91. [self.progressLine endLoadingAnimation];
  92. }
  93. #pragma mark ------
  94. - (UIButton *)backButton {
  95. if (!_backButton) {
  96. _backButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
  97. [_backButton setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
  98. [_backButton addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
  99. }
  100. return _backButton;
  101. }
  102. @end