0

I've got this weird issue with my apps that uses internet connection. The app doesn't seem to update the data from internet in the right way. I've got a UIWebView with a simple chat. When I write a message in the chat with safari or in my web browser everything works fine. When writing something in the chat in the app, the data doesn't update, even when reloading the page.

It updates totally un-logically as well. Even when restarting the app the new data isn't shown, then all of a sudden it shows.

Had the same issue with another app too, which was not handled by a UIWebView. Any idea what it might be?

Example code:

- (void) viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];

NSString *username = [[Database singletonDataBase] currentUserName];
NSString *urlAddress = [NSString stringWithFormat:@"%@?name=%@", @"http://webapi.com/index.php", username];
NSURL *url = [NSURL URLWithString:urlAddress];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];

[self.webView loadRequest:requestObj];

}

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
sold
  • 314
  • 1
  • 3
  • 11

2 Answers2

0

In your app delegates, didFinishLoading method, set the NSURLCache to not cache the data:

// Remove and disable all URL Cache, but doesn't seem to affect the memory
[[NSURLCache sharedURLCache] removeAllCachedResponses];
[[NSURLCache sharedURLCache] setDiskCapacity:0];
[[NSURLCache sharedURLCache] setMemoryCapacity:0];

If that doesn't work, you can also try setting the cache policy on the NSURLRequest:

NSURL *url = [NSURL URLWithString:urlAddress];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url 
                                        cachePolicy:NSURLRequestReloadIgnoringCacheData
                                    timeoutInterval: 10.0]; 
wottle
  • 13,095
  • 4
  • 27
  • 68
  • Thanks for the answer. I tried both of your suggestions, and all types of cache policies, and none of them seemed to work. It could'nt be some setting in Xcode or something? :/ – sold Aug 06 '14 at 21:40
0

I finally found the answer. Its nothing wrong with my code, as I thought. The problem lies on the server side. And, as they say in this thread:

NSURLConnection is returning old data

It seems to be badly configured server-side or proxy. Which makes the cache act all wierd. I tried another server and everything worked just fine!

Hope this helps someone with the same issue, cause it sure wasted me ALOT of time.

Community
  • 1
  • 1
sold
  • 314
  • 1
  • 3
  • 11