程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> ios-使用AFHttpClient驗證的問題

ios-使用AFHttpClient驗證的問題

編輯:編程綜合問答
使用AFHttpClient驗證的問題

在一個照片分享應用中用到AFHttpClient類。我要保存ios keychain 的訪問令牌,因此不需要在每一個應用啟動驗證。但是我目前只能在登錄一次之後進行驗證和保存訪問令牌。

#define kAccessTokenInstagram    @"Token"
#define INSTAGRAM_AUTH_URL_FORMAT @"https://instagram.com/oauth/authorize/?client_id=%@&redirect_uri=%@&response_type=token"

#import "InstagramClient.h"
#import "Lockbox.h"

@interface InstagramClient ()
@property (nonatomic, copy) NSString *accessToken;
@end

@implementation InstagramClient

+ (instancetype)sharedClient {
    static InstagramClient *_sharedClient = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        NSURL *baseURL = [NSURL URLWithString:@"https://api.instagram.com/v1/"];
        _sharedClient = [[InstagramClient alloc] initWithBaseURL:baseURL];
    });

    return _sharedClient;
}

- (id)initWithBaseURL:(NSURL *)url {
    self = [super initWithBaseURL:url];
    if (self) {
        [self registerHTTPOperationClass:[AFJSONRequestOperation class]];
        [self setDefaultHeader:@"Accept" value:@"application/json"];
    }
    return self;
}

- (void)authenticateWithClientID:(NSString *)clientId callbackURL:(NSString *)callbackUrl {
    NSString *urlString = [NSString stringWithFormat:INSTAGRAM_AUTH_URL_FORMAT, clientId, callbackUrl];
    NSURL *url = [NSURL URLWithString:urlString];
    [[UIApplication sharedApplication] openURL:url];
}

- (void)handleOAuthCallbackWithURL:(NSURL *)url {
    NSError *regexError = nil;
    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"^[^#]*#access_token=(.*)$"
                                                                           options:0
                                                                             error:&regexError];
    NSString *input = [url description];
    [regex enumerateMatchesInString:input
                            options:0
                              range:NSMakeRange(0, [input length])
                         usingBlock:^(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop) {
                             if ([result numberOfRanges] > 1) {
                                 NSRange accessTokenRange = [result rangeAtIndex:1];
                                 self.accessToken = [input substringWithRange:accessTokenRange];
                                 NSLog(@"Access Token: %@", self.accessToken);
                                 NSString *token = self.accessToken;
                                [Lockbox setString:token forKey:kAccessTokenInstagram];
                             }
                         }];
}

- (NSString *)accessToken {
    return [Lockbox stringForKey:kAccessTokenInstagram];
}

-(AFHTTPRequestOperation *)HTTPRequestOperationWithRequest:(NSURLRequest *)urlRequest success:(void (^)(AFHTTPRequestOperation *, id))success failure:(void (^)(AFHTTPRequestOperation *, NSError *))failure {
    NSMutableURLRequest *request = [urlRequest mutableCopy];
    NSString *separator = [request.URL query] ? @"&" : @"?";
    NSString *newURLString = [NSString stringWithFormat:@"%@%@access_token=%@", [request.URL absoluteString], separator, self.accessToken];
    NSURL *newURL = [[NSURL alloc] initWithString:newURLString];
    [request setURL:newURL];
    return [super HTTPRequestOperationWithRequest:request success:success failure:failure];
}
@end

這是出問題的代碼:

- (NSString *)accessToken {
    return [Lockbox stringForKey:kAccessTokenInstagram];
}

最佳回答:


改兩個地方試試
1.將存儲的方法

[Lockbox setString:token forKey:kAccessTokenInstagram];

改成

[Lockbox setValue:token forKey:kAccessTokenInstagram];

2.將get的方法

return [Lockbox stringForKey:kAccessTokenInstagram];

改成

return [Lockbox valueForKey:kAccessTokenInstagram];
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved