-2

txt file on my domain and I can't manage to save into it, here is my code:

NSString *countstring = [NSString stringWithContentsOfURL:[NSURL URLWithString:@"http://www.DomainName.com/MyAppName/count.txt"] encoding:NSUTF8StringEncoding error:nil];
        int countInt = [countstring intValue];
        NSLog(@"count string is:%i",countInt);
        int newCount = countInt + 1;
        NSLog(@"new count is: %i",newCount);
        NSString *newString = [NSString stringWithFormat:@"%i",newCount];
        [newString writeToURL:[NSURL URLWithString:@"http://www.DomainName.com/MyAppName/count.txt"] atomically:NO encoding:NSUTF8StringEncoding error:nil];

(I censored my domain name and app name)
The logs are:

count string is:0
new count is: 1

which is right! But when I open the file on the domain it is still 0. How can I fix it? Thanks!

user1321887
  • 167
  • 1
  • 2
  • 7
  • 3
    Just imagine the impossibility of having *anything* on the internet if this worked. Everyone's content would constantly be getting hacked. =) – J. Steen Nov 06 '12 at 19:39
  • well its just for debugging purposes :) I'm not storing any personal data... – user1321887 Nov 06 '12 at 20:14

3 Answers3

1

You cannot save something to a file on the Internet from an app. You have to use server-side code to update the file.

Try using the answer here to update the file using a web service:

iOS: how to perform a HTTP POST request?

This also requires that you write some kind of web service for you app to call, which will update the file. This is a lot more complex than what you are trying.

Community
  • 1
  • 1
woz
  • 10,888
  • 3
  • 34
  • 64
1

This doesn't work like this. You can't write to this and that URL you randomly find on the web? The usual approach to this is issuing a HTTP POST request with the data and then using some server-side script for actually updating the file.

0

@H2CO3 is right that usual approach is a HTTP post. I haven't tried this but writing strings to file directly should work but your URL has to be in form of file:// In your case it's http which will not work!

Paresh Masani
  • 7,474
  • 12
  • 73
  • 139