我正在使用AFNetworking
库创建一个在离线模式下工作的iOS应用程序。
要管理HTTP请求,我已定义了AFHTTPRequestOperationManager
- (AFHTTPRequestOperationManager *)requestOperationManager
{
if (!_requestOperationManager)
{
_requestOperationManager = [[AFHTTPRequestOperationManager alloc] initWithBaseURL:[NSURL URLWithString:SERVICE_BASE_URL]];
}
return _requestOperationManager;
}
加载HTTP请求我定义了一个方法:
- (void)loadRequest
{
if (!self.internetConnectionAvaliable)
{
self.requestOperationManager.requestSerializer.cachePolicy = NSURLRequestReturnCacheDataElseLoad;
}
NSString *url = SERVICE_METHOD_URL;
NSDictionary *parameters = @{@"password" : USER_PASSWORD, @"username" : USER_NAME};
[self.requestOperationManager POST:url parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject)
{
NSString *htmlString = [NSString stringWithFormat:@"%@", [responseObject objectForKey:@"view"]];
[self.webView loadHTMLString:htmlString baseURL:nil];
}
failure:^(AFHTTPRequestOperation *operation, NSError *error)
{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"ERROR!!!" message:@"ERROR!!!" delegate:nil cancelButtonTitle:@"CANCEL" otherButtonTitles:nil];
[alertView show];
}];
}
启用互联网连接后,AFNetworking
可以完美地加载所需的数据。
禁用互联网连接时,AFNetworking
不会加载任何数据。这让我觉得,数据缓存并不像它应该的那样工作!
有谁知道为什么? 如何解决?