How to use NSURLCache to return cached API responses when offline (iOS App)

前端 未结 2 1196
悲哀的现实
悲哀的现实 2021-02-04 09:09

I\'m hoping someone can shed some light on a few things I\'ve been researching but not making much progress on.

I\'d like to take advantage of NSURLCache to return cache

相关标签:
2条回答
  • 2021-02-04 09:30

    Did you see this post?

    AFNetworking (AFHttpClient) offline mode not working with NSURLRequestReturnCacheDataDontLoad policy

    Looks like it might be a bug with iOS 6. Here is what Robert Mao had to say in the post:

    A summarized work around is read the response from cache, instead of use NSURLRequestReturnCacheDataDontLoad policy:

    NSCachedURLResponse *cachedResponse = [[NSURLCache sharedURLCache]     cachedResponseForRequest:request];
    if (cachedResponse != nil &&
        [[cachedResponse data] length] > 0)
    {
        // Get cached data
        ....
    }
    
    0 讨论(0)
  • 2021-02-04 09:49

    Unless all of your calls are 100% GET and free of side effects or time dependency then this is dangerous.

    0 讨论(0)
提交回复
热议问题