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

前端 未结 2 1254
悲哀的现实
悲哀的现实 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
        ....
    }
    

提交回复
热议问题