猎豆优选

LDTaobaoWebAuthorController.m 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. //
  2. // LDTaobaoWebAuthorController.m
  3. // YouHuiProject
  4. //
  5. // Created by 小花 on 2019/1/24.
  6. // Copyright © 2019年 kuxuan. All rights reserved.
  7. //
  8. #import "LDTaobaoWebAuthorController.h"
  9. #import <WebKit/WebKit.h>
  10. #import "WYWeakScriptMessageDelegate.h"
  11. #import "WebviewProgressLine.h"
  12. #import <AlibcTradeSDK/AlibcTradeSDK.h>
  13. #import <AlibabaAuthSDK/ALBBSession.h>
  14. #import <AlibabaAuthSDK/ALBBSDK.h>
  15. @interface LDTaobaoWebAuthorController ()<UIWebViewDelegate>
  16. @property (nonatomic, strong) UIWebView *webView;
  17. @property (nonatomic, strong) WebviewProgressLine *progress;
  18. @property (nonatomic, strong) UIButton *backButton;
  19. @property (nonatomic, strong) UIButton *closeButton;
  20. @end
  21. @implementation LDTaobaoWebAuthorController
  22. - (void)viewDidLoad {
  23. [super viewDidLoad];
  24. [self configNavigationBar];
  25. [self loadRequest];
  26. }
  27. - (void)configNavigationBar {
  28. self.view.backgroundColor = [UIColor whiteColor];
  29. [self.view addSubview:self.webView];
  30. [self.view addSubview:self.progress];
  31. self.navigationBar.backgroundColor = [UIColor whiteColor];
  32. self.navigationBar.showNavigationBarBottomLine = YES;
  33. [self.navigationBar setCustomLeftButtons:@[self.backButton]];
  34. self.navigationBar.backgroundColor = [UIColor clearColor];
  35. if (@available(iOS 11.0, *)) {
  36. self.webView.scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;//UIScrollView也适用
  37. }else {
  38. self.automaticallyAdjustsScrollViewInsets = NO;
  39. }
  40. }
  41. - (void)loadRequest {
  42. NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:self.url]];
  43. [self.webView loadRequest:request];
  44. }
  45. #pragma mark-
  46. #pragma mark- WKNavigationDelegate delegate
  47. - (void)webViewDidStartLoad:(UIWebView *)webView {
  48. [self.progress startLoadingAnimation];
  49. }
  50. - (void)webViewDidFinishLoad:(UIWebView *)webView {
  51. [self.progress endLoadingAnimation];
  52. NSString *title = [webView stringByEvaluatingJavaScriptFromString:@"document.title"];//获取当前页面的title
  53. [self.navigationBar setNavTitle:title];
  54. [self updateNavigationBarButtons];
  55. }
  56. - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
  57. NSString *strRequest = request.URL.absoluteString;
  58. if ([strRequest containsString:@"code="]) {
  59. NSString *str = [[strRequest componentsSeparatedByString:@"code="] lastObject];
  60. NSString *codeStr = [[str componentsSeparatedByString:@"&"] firstObject];
  61. if (codeStr) {
  62. [self authorRequest:codeStr];
  63. }
  64. }
  65. return YES;
  66. }
  67. - (void)authorRequest:(NSString *)codeStr {
  68. NSString *url = [NSString stringWithFormat:@"%@/api/v2/relationAuth/tbkUserToAuth",BaseURL];
  69. if (!self.authorType) {
  70. self.authorType = @"2";
  71. }
  72. NSDictionary *para = @{@"code":codeStr,@"type":self.authorType};
  73. [LDHttp post:url params:para success:^(id json) {
  74. [XHToast showCenterWithText:json[@"msg"]];
  75. [self.navigationController popViewControllerAnimated:YES];
  76. } failure:^(NSError *error) {
  77. if (error) {
  78. [XHToast showCenterWithText:@"加载失败"];
  79. }
  80. }];
  81. }
  82. #pragma mark --- WKScriptMessageHandler
  83. #pragma mark -----------
  84. - (void)updateNavigationBarButtons {
  85. if (self.webView.canGoBack) {
  86. [self.navigationBar setCustomLeftButtons:@[self.backButton,self.closeButton]];
  87. }else {
  88. [self.navigationBar setCustomLeftButtons:@[self.backButton]];
  89. }
  90. }
  91. - (void)backAction {
  92. if (self.webView.canGoBack) {
  93. [self.webView goBack];
  94. }else{
  95. [self.navigationController popViewControllerAnimated:YES];
  96. }
  97. }
  98. - (void)closeAction {
  99. [self.navigationController popViewControllerAnimated:YES];
  100. }
  101. - (UIWebView *)webView{
  102. if (!_webView) {
  103. _webView = [[UIWebView alloc]initWithFrame:CGRectMake(0, NavBarHeight, SCREEN_WIDTH, SCREEN_HEIGHT-NavBarHeight)];
  104. _webView.backgroundColor = [UIColor colorWithRed:240.0/255 green:240.0/255 blue:240.0/255 alpha:1.0];
  105. _webView.delegate = self;
  106. //使用kvo监听进度
  107. //自适应
  108. [_webView sizeToFit];
  109. }
  110. return _webView;
  111. }
  112. #pragma mark-
  113. #pragma mark- SetupConstraints
  114. /** 加载配置以及视图添加*/
  115. - (void)loadSubViewsConfiguration{
  116. [self.view addSubview:self.webView];
  117. }
  118. - (UIButton *)backButton {
  119. if (!_backButton) {
  120. _backButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
  121. [_backButton setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
  122. [_backButton addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
  123. }
  124. return _backButton;
  125. }
  126. - (UIButton *)closeButton {
  127. if (!_closeButton) {
  128. _closeButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
  129. [_closeButton setImage:[UIImage imageNamed:@"close_web"] forState:UIControlStateNormal];
  130. [_closeButton addTarget:self action:@selector(closeAction) forControlEvents:UIControlEventTouchUpInside];
  131. }
  132. return _closeButton;
  133. }
  134. - (WebviewProgressLine *)progress {
  135. if (!_progress) {
  136. _progress = [[WebviewProgressLine alloc] initWithFrame:CGRectMake(0, NavBarHeight, SCREEN_WIDTH, 2)];
  137. _progress.lineColor = [UIColor homeRedColor];
  138. }
  139. return _progress;
  140. }
  141. /*
  142. #pragma mark - Navigation
  143. // In a storyboard-based application, you will often want to do a little preparation before navigation
  144. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  145. // Get the new view controller using [segue destinationViewController].
  146. // Pass the selected object to the new view controller.
  147. }
  148. */
  149. @end