0

我为 Instagram 创建了一个 AFHTTPClient 类,我想将访问令牌保存在 iOS 钥匙串中,这样我就不必在每次应用启动时都进行身份验证。但是,我只能在登录一次后验证并保存 accessToken,这是我的代码:

#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];
}
4

1 回答 1

1

因为@property (nonatomic, copy) NSString *accessToken;您已经定义了一个 getter 方法,该方法仅在已设置令牌时才从钥匙串中检索令牌。第一次运行代码时会出现问题,因为您在此处设置之前尝试访问 accessToken 属性的 getter:[Lockbox setString:token forKey:kAccessTokenInstagram];

在设置之前您不能使用 self.accessToken ,这就是您的块中正在发生的事情。

也许尝试:

NSRange accessTokenRange = [result rangeAtIndex:1];
NSString *token = [input substringWithRange:accessTokenRange];
NSLog(@"Access Token: %@", token);
[Lockbox setString:token forKey:kAccessTokenInstagram];

代替:

NSRange accessTokenRange = [result rangeAtIndex:1];
self.accessToken = [input substringWithRange:accessTokenRange];
NSLog(@"Access Token: %@", self.accessToken);
[Lockbox setString:token forKey:kAccessTokenInstagram];
于 2013-08-07T00:51:20.637 回答