我的登录API有问题。第一次调用运行良好,但随后的调用被缓存。这导致了一个问题,因为登录/注销功能实际上已经崩溃。
我尝试过许多方法,并且正在实现AFNetworking库。
在AppDelegate.m中
NSURLCache *sharedCache = [[NSURLCache alloc] initWithMemoryCapacity:0
diskCapacity:0
diskPath:nil];
[NSURLCache setSharedURLCache:sharedCache];在我的社交课上:
(AFHTTPRequestOperation *)createRequestOperationWithMethod:(NSString *) method andPath: (NSString *)path andParams:(NSDictionary *)params
{
GRAPIClient *httpClient = [GRAPIClient sharedClient];
[httpClient setParameterEncoding:AFFormURLParameterEncoding];
NSMutableURLRequest *request = [httpClient requestWithMethod:method
path:path
parameters:params];
[request setCachePolicy:NSURLRequestReloadIgnoringCacheData]
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
[httpClient registerHTTPOperationClass:[AFHTTPRequestOperation class]];
return operation;}
我甚至试图覆盖在AFHTTPClient中生成的请求。
在AFHTTPClient.m中:
[request setCachePolicy:NSURLRequestReloadIgnoringCacheData];
[request setTimeoutInterval:2.0];我的GRAPIClient实现:
@interface GRAPIClient : AFHTTPClient
+ (GRAPIClient *)sharedClient;
+ (BOOL) isInternetReachable;
@end
@implementation GRAPIClient
+ (BOOL) isInternetReachable
{
return reachable;
}
+ (GRAPIClient *)sharedClient {
static GRAPIClient *_sharedClient = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_sharedClient = [[GRAPIClient alloc] initWithBaseURL:[NSURL URLWithString:kAFAppDotNetAPIBaseURLString]];
});
[_sharedClient setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
if (status == AFNetworkReachabilityStatusReachableViaWWAN ||
status == AFNetworkReachabilityStatusReachableViaWiFi ) {
NSLog(@"Reachable on!");
reachable = YES;
}
else
{
NSLog(@"Reachable off!");
reachable = NO;
}
}];
return _sharedClient;
}
- (id)initWithBaseURL:(NSURL *)url {
self = [super initWithBaseURL:url];
if (!self) {
return nil;
}
[self registerHTTPOperationClass:[AFJSONRequestOperation class]];
// Accept HTTP Header; see http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.1
[self setDefaultHeader:@"Accept" value:@"application/json"];
return self;
}
@end我已经调试了来自服务器的响应,并同时对服务器进行了两个NSURLRequests的硬编码测试。一个用于用户A,另一个用于用户B,然后打印两个用户的响应数据。
在第一次登录时,用户A登录返回用户A凭据。用户B返回用户B凭据。在第二次登录时,用户A返回用户A凭据,用户B返回用户A凭据。我不知道如何完全禁用缓存。
发布于 2013-10-14 14:56:12
正如SixteenOtto所建议的那样,我遇到的问题是从服务器发送会话,并使用cookie自动发送AFNetworking。我以前没有考虑过这一点,因为我们使用的是基于auth令牌的不稳定API,因此会话的cookie没有意义。但是,使用Charles检查HTTP响应头可以让我看到这一点。
添加
[request setHTTPShouldHandleCookies:NO];我的operation生成器解决了这个问题。
发布于 2013-10-11 18:13:07
尝试:
[operation setCacheResponseBlock:^NSCachedURLResponse*(NSURLConnection* connection, NSCachedURLResponse* cachedResponse) {
return nil;
}];和:
[request setCachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData];https://stackoverflow.com/questions/19321043
复制相似问题