ios - 使用 AFNetworking 时,应用程序未下载最新的 plist。缓存?

标签 ios caching afnetworking

我的应用程序中有一个版本检查器,并且我将当前版本号存储在服务器上的 plist 中。由于某种原因,当我确认它是 1.21 而它一直显示 1.2 时,它会不断返回旧值。我相信这是某种缓存,因为我的浏览器也做了同样的事情。

如何防止这种情况发生?我将cachePolicy设置为NSURLRequestReloadIgnoringCacheData,但是当它是1.21时它仍然返回1.2

NSURL *url = [NSURL URLWithString:@"https://somewebsite.com/iosapp/CurrentVersion.plist"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
request.cachePolicy = NSURLRequestReloadIgnoringCacheData;

[request setHTTPMethod:@"GET"];

[AFPropertyListRequestOperation addAcceptableContentTypes:[NSSet setWithObject:@"text/xml"]];
AFPropertyListRequestOperation *operation = [AFPropertyListRequestOperation propertyListRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id propertyList) {
    NSString *currentVersion = [[[NSArray alloc] initWithArray:propertyList] objectAtIndex:0];
    NSLog(@"Current Version %@", currentVersion);
    NSString *deviceVersion = [[NSBundle mainBundle] objectForInfoDictionaryKey: @"CFBundleShortVersionString"];
    if (![currentVersion isEqualToString:deviceVersion]) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Update Available" message:[NSString stringWithFormat:@"App %@ is available. Please update.", currentVersion] delegate:self cancelButtonTitle:@"Later" otherButtonTitles:@"Update", nil];
        [alert show];
    }


    [self finalCheck];
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id propertyList) {
    [self finalCheck];
}];

最佳答案

implementation of AFPropertyListRequestOperation不做任何与缓存相关的事情。以下是一些选项:

  1. 代理或中介可能会缓存它。尝试使用 NSURLRequestReloadIgnoringLocalAndRemoteCacheData 来代替。
  2. [[NSBundle mainBundle] objectForInfoDictionaryKey: @"CFBundleShortVersionString"] 的响应可能不是您所期望的(我注意到您没有记录它。)

我还想推荐您查看两个库:

  • Harpy ,一个易于添加、低维护的库,它使用 iTunes API 检查最新版本,并按照您指定的时间间隔提醒您的用户。
  • Ground Control ,NSUserDefaults 上的一个简单类别,用于异步下载、读取和保存远程 plist 文件。

第一个将避免您编写任何此类代码的需要。如果您需要更多的控制/定制,第二个将使事情变得更容易。

关于ios - 使用 AFNetworking 时,应用程序未下载最新的 plist。缓存?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18496395/

相关文章:

haskell - Haskell/Frege 是否曾经重新计算惰性列表的元素?

ios - 快速缓存.. iOS FBApi

ios - AFNetworking setImageWithURLRequest 未设置远程图像

ios - 如何使用 AFNetworking 下载多张图片?

ios - 导航 Controller 中的 UIScrollView

ios - RequestImage 的结果处理程序调用了两次

ios - 使用 Objective-C 在 iPhone 上禁用 WIFI

ipad - iPad 应用程序缓存事件无法更新屏幕

ios - 如何提高 FPS?

ios - 删除 UITableViewCell 后应用卡住