123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203 |
- //
- // LDTaobaoWebAuthorController.m
- // YouHuiProject
- //
- // Created by 小花 on 2019/1/24.
- // Copyright © 2019年 kuxuan. All rights reserved.
- //
- #import "LDTaobaoWebAuthorController.h"
- #import <WebKit/WebKit.h>
- #import "WYWeakScriptMessageDelegate.h"
- #import "WebviewProgressLine.h"
- #import <AlibcTradeSDK/AlibcTradeSDK.h>
- #import <AlibabaAuthSDK/ALBBSession.h>
- #import <AlibabaAuthSDK/ALBBSDK.h>
- @interface LDTaobaoWebAuthorController ()<UIWebViewDelegate>
- @property (nonatomic, strong) UIWebView *webView;
- @property (nonatomic, strong) WebviewProgressLine *progress;
- @property (nonatomic, strong) UIButton *backButton;
- @property (nonatomic, strong) UIButton *closeButton;
- @end
- @implementation LDTaobaoWebAuthorController
- - (void)viewDidLoad {
- [super viewDidLoad];
-
- [self configNavigationBar];
- [self loadRequest];
- }
- - (void)configNavigationBar {
-
- self.view.backgroundColor = [UIColor whiteColor];
- [self.view addSubview:self.webView];
- [self.view addSubview:self.progress];
- self.navigationBar.backgroundColor = [UIColor whiteColor];
- self.navigationBar.showNavigationBarBottomLine = YES;
- [self.navigationBar setCustomLeftButtons:@[self.backButton]];
- self.navigationBar.backgroundColor = [UIColor clearColor];
-
- if (@available(iOS 11.0, *)) {
- self.webView.scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;//UIScrollView也适用
- }else {
- self.automaticallyAdjustsScrollViewInsets = NO;
- }
- }
- - (void)loadRequest {
- NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:self.url]];
- [self.webView loadRequest:request];
- }
- #pragma mark-
- #pragma mark- WKNavigationDelegate delegate
- - (void)webViewDidStartLoad:(UIWebView *)webView {
- [self.progress startLoadingAnimation];
- }
- - (void)webViewDidFinishLoad:(UIWebView *)webView {
- [self.progress endLoadingAnimation];
- NSString *title = [webView stringByEvaluatingJavaScriptFromString:@"document.title"];//获取当前页面的title
- [self.navigationBar setNavTitle:title];
- [self updateNavigationBarButtons];
- }
- - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
-
- NSString *strRequest = request.URL.absoluteString;
- if ([strRequest containsString:@"code="]) {
- NSString *str = [[strRequest componentsSeparatedByString:@"code="] lastObject];
- NSString *codeStr = [[str componentsSeparatedByString:@"&"] firstObject];
- if (codeStr) {
- [self authorRequest:codeStr];
- }
- }
-
- return YES;
- }
- - (void)authorRequest:(NSString *)codeStr {
- NSString *url = [NSString stringWithFormat:@"%@/api/v2/relationAuth/tbkUserToAuth",BaseURL];
- if (!self.authorType) {
- self.authorType = @"2";
- }
- NSDictionary *para = @{@"code":codeStr,@"type":self.authorType};
- [LDHttp post:url params:para success:^(id json) {
- [XHToast showCenterWithText:json[@"msg"]];
- [self.navigationController popViewControllerAnimated:YES];
- } failure:^(NSError *error) {
- if (error) {
- [XHToast showCenterWithText:@"加载失败"];
- }
- }];
- }
- #pragma mark --- WKScriptMessageHandler
- #pragma mark -----------
- - (void)updateNavigationBarButtons {
- if (self.webView.canGoBack) {
- [self.navigationBar setCustomLeftButtons:@[self.backButton,self.closeButton]];
- }else {
- [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];
- }
- - (UIWebView *)webView{
- if (!_webView) {
-
- _webView = [[UIWebView alloc]initWithFrame:CGRectMake(0, NavBarHeight, SCREEN_WIDTH, SCREEN_HEIGHT-NavBarHeight)];
- _webView.backgroundColor = [UIColor colorWithRed:240.0/255 green:240.0/255 blue:240.0/255 alpha:1.0];
- _webView.delegate = self;
- //使用kvo监听进度
- //自适应
- [_webView sizeToFit];
- }
- 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;
- }
- - (WebviewProgressLine *)progress {
- if (!_progress) {
- _progress = [[WebviewProgressLine alloc] initWithFrame:CGRectMake(0, NavBarHeight, SCREEN_WIDTH, 2)];
- _progress.lineColor = [UIColor homeRedColor];
- }
- return _progress;
- }
- /*
- #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
|