2

Id like to "update" a local file from a server (save it to a directory). I tried EVERYTHING! NOTHING WORKS! That's my last attempt:

    NSURL *url = [NSURL URLWithString:@"http://www.doothie.com/QAFrameworks/QAUpdater.php"];
    NSData *urlData = [NSData dataWithContentsOfURL:url];


    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"general.qa"];

    NSString *str = [[NSString alloc] initWithData:urlData encoding:NSUTF8StringEncoding];

    [str writeToFile:filePath atomically:TRUE encoding:NSUTF8StringEncoding error:NULL];
Smoothie
  • 261
  • 4
  • 11
  • what's the problem? Does the file not appear? Are you receiving an error? – Max MacLeod Dec 01 '11 at 17:54
  • the file doesn't appear... actually the file does already exist. I just want to overwrite the file – Smoothie Dec 01 '11 at 17:55
  • the file is in directory "Ressources/QStandards". – Smoothie Dec 01 '11 at 18:03
  • 1
    A couple of observations: 1) "Ressources/QStandards" looks like a localized name - `filePath` would be not be localized. 2) it is a relative path name, which I don't think `NSSearchPathForDirectoriesInDomains` returns. 3) have you tried to pass in a real error object to see if you get any error messages? – Monolo Dec 01 '11 at 18:30
  • sure that shouldn't be "Resources"? – Max MacLeod Dec 02 '11 at 16:32

1 Answers1

3

Not sure if that's what you're trying to do, but you shouldn't try to download and replace a file that's in your Application bundle. Instead, download the file into the user's ~/Library/Application Support/<yourApplicationName>/<yourFiles>. Before using the file within your application bundle, check to see if the one with more current data is in the Application Support. If you application is installed for all users (within /Applications/), non-admin users wouldn't have the authority to change files within the app bundle. (And one should never assume every user runs with admin rights.)

Perhaps that's what's happening.

Additionally, you're loading everything into NSString. Does the file actually contain text?! If not, you might want to use NSData instead :

[[NSData dataWithContentsOfURL:url] writeToFile:filePath atomically:TRUE];