美文网首页
runtime 自动刷新token

runtime 自动刷新token

作者: Sendom | 来源:发表于2018-05-17 14:12 被阅读0次
    //
    //  AFURLSessionManager+AutoRefreshToken.m
    //  NWBQuotationBusinessModule_Example
    //
    //  Created by Sendom on 15/5/18.
    //  Copyright © 2018年 lilonglong. All rights reserved.
    //
    
    #import "AFURLSessionManager+AutoRefreshToken.h"
    
    #import <objc/runtime.h>
    
    @implementation AFURLSessionManager (AutoRefreshToken)
    
    + (void)load {
        static dispatch_once_t onceToken;
        dispatch_once(&onceToken, ^{
            Method originMethod = class_getInstanceMethod(self, @selector(dataTaskWithRequest:uploadProgress:downloadProgress:completionHandler:));
            Method newMethod = class_getInstanceMethod(self, @selector(autoRefreshTokenDataTaskWithRequest:uploadProgress:downloadProgress:completionHandler:));
            method_exchangeImplementations(originMethod, newMethod);
        });
    }
    
    - (NSURLSessionDataTask *)autoRefreshTokenDataTaskWithRequest:(NSURLRequest *)request
                                   uploadProgress:(nullable void (^)(NSProgress *uploadProgress)) uploadProgressBlock
                                 downloadProgress:(nullable void (^)(NSProgress *downloadProgress)) downloadProgressBlock
                                completionHandler:(nullable void (^)(NSURLResponse *response, id _Nullable responseObject,  NSError * _Nullable error))completionHandler {
        void (^authFailBlock)(NSURLResponse *response, id responseObject, NSError *error) = ^(NSURLResponse *response, id responseObject, NSError *error)
        {
            NSHTTPURLResponse* httpResponse = (NSHTTPURLResponse*)response;
            if([httpResponse statusCode] == 401){
                //since there was an error, call you refresh method and then redo the original task
                dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
                    
                    //call your method for refreshing OAuth tokens.  This is an example:
                    [self refreshAccessToken:^(id responseObject) {
                        //store your new token
    //
                        //now, queue up and execute the original task
                    NSURLSessionDataTask *originalTask = [self autoRefreshTokenDataTaskWithRequest:request uploadProgress:uploadProgressBlock downloadProgress:downloadProgressBlock completionHandler:completionHandler];
                        [originalTask resume];
                    }];
                });
            }else{
                NSLog(@"no auth error");
                completionHandler(response, responseObject, error);
            }
        };
        
        return [self autoRefreshTokenDataTaskWithRequest:request uploadProgress:uploadProgressBlock downloadProgress:downloadProgressBlock completionHandler:authFailBlock];
    }
    
    - (void)refreshAccessToken:(void (^) (id responseObject))task {
        
    }
    
    @end
    

    相关文章

      网友评论

          本文标题:runtime 自动刷新token

          本文链接:https://www.haomeiwen.com/subject/eeybdftx.html