1

I have a textview that downloads the text from a .txt on my server. The only problem is that it will only do that once-- no matter if I update the .txt file or not, the text will not change.

Here is the code for the textview:

- (void)viewDidLoad

{NSError *error = nil;
NSURL *myurl = [NSURL URLWithString:@"http://www.myserver.com/test.txt"];
NSString *mystring = [NSString stringWithContentsOfURL:myurl encoding:NSUTF8StringEncoding error:&error]; 
newtext.text = mystring;
}

Can't seem to figure out how to make it check the server each time the app runs (and not just cache what it found the first time). This happens in the simulator and on a real iphone as well.

Thanks for any help!

Homeoftheben
  • 69
  • 1
  • 10

2 Answers2

0

HTTP caching is controlled by the Cache-Control HTTP header. If your text file is changing often, you should configure your server to emit suitable headers for this, for instance Cache-Control: no-cache. For more details, read Mark Nottingham's caching tutorial.

Jim
  • 72,985
  • 14
  • 101
  • 108
  • thanks for the quick reply! I edited the servers hta access file to include this: ` Header set Cache-Control "max-age=1, public, must-revalidate" ` but the textfield still will not load the new txt file's content. – Homeoftheben May 12 '12 at 20:45
  • Have you verified that the headers are in effect by performing a request manually with a tool like curl? Have you verified that your application isn't using its cached version from before with a proxy such as Charles? Why are you picking HTTP headers that *enable* caching? Have you tried it with headers that disable caching completely? – Jim May 12 '12 at 22:21
  • Hi! I did change the hta access file to disable caches completely: ` Header unset Cache-Control ` but I'm unsure how to check if it's working with Curl. I did Reset Content and Settings within the Simulator, but it still seems to display the old txt file's content. – Homeoftheben May 12 '12 at 23:04
  • `curl --head http://example.com/` should show you the headers, alternatively use the telnet method described in the tutorial I linked to. Unsetting `Cache-Control` altogether usually has the effect of enabling caching too. Please read the tutorial I linked to. – Jim May 13 '12 at 00:08
0

I do not believe you can set the cache policy with the convenience method stringWithContentsOfURL. Someone correct me if I am wrong.

The good news is that this is pretty simple to fix. Simply create your own request and set the cache policy to NSURLRequestReloadIgnoringLocalCacheData.

NSURL *myurl = [NSURL URLWithString:@"http://www.myserver.com/test.txt"];
NSURLRequest *request = [NSURLRequest requestWithURL:myurl
                                         cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
                                     timeoutInterval:60.0];

NSURLConnection *connection= [[NSURLConnection alloc] initWithRequest:request 
                                                             delegate:self];

Then set the text view in the delegate. At the end of the day, this is what the convenience method is doing anyway.

Joel
  • 15,654
  • 5
  • 37
  • 60
  • Thanks for the reply! I tried to incorporate your code, but I'm not sure I understand how to set the text view in the delegate? – Homeoftheben May 12 '12 at 23:08
  • See here for a sample NSURLConnection implementation. Just set your text view in the connectionDidFinishLoading method. http://stackoverflow.com/questions/2124421/are-there-complete-examples-that-make-use-of-all-the-nsurlconnection-delegate-me – Joel May 12 '12 at 23:11
  • OK! Riddle me this-- it all works BUT only if I first view the txt file in my computer's browser, and cntrl-click refresh (to clear the browser's cache of the page and force reload). Then (and only then) will the iphone also pull the new text. I feel as though I've lost my mind, but it works! Thanks again for all the help! – Homeoftheben May 12 '12 at 23:24
  • Use NSURLRequestReloadIgnoringLocalAndRemoteCacheData instead. I'll bet that fixes it. – Joel May 12 '12 at 23:42