NSString writeToFile operation couldn't be completed

Posted by Chonch on Stack Overflow See other posts from Stack Overflow or by Chonch
Published on 2010-05-13T12:11:16Z Indexed on 2010/05/13 12:14 UTC
Read the original article Hit count: 281

Filed under:
|
|

Hey,

I have an xml file in my application bundle. I want to copy it to the documents folder at installation and then, every time the app launches, get the newest version of this file from the Internet. I use this code:

   // Check if the file exists in the documents folder
    NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    if (![[NSFileManager defaultManager] fileExistsAtPath:[documentsPath stringByAppendingPathComponent:@"fileName.xml"]])
    // If not, copy it there (from the bundle)
          [[NSFileManager defaultManager] copyItemAtPath:[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"OriginalFile.xml"] 
                    toPath:[documentsPath stringByAppendingPathComponent:@"fileName.xml"] error:nil];

         // Get the newest version of the file from the server
         NSURL *url=[[NSURL alloc] initWithString:@"http://www.sitename.com/webservice.asmx/webserviceName"];
         NSString *results = [[NSString alloc] initWithContentsOfURL:url];

         // Replace the current version with the newest one, only if it is valid
         if (results != nil)
          [results writeToFile:[documentsPath stringByAppendingPathComponent:@"fileName.xml"] atomically:NO encoding:NSStringEncodingConversionAllowLossy error:nil];

The problem is that the writeToFile command always returns NO and the file's contents remain identical to the original file I included in my app bundle. I checked the value of results and it's correct. I also made sure that the app does perform the writeToString command, but still, it always returns NO.

Can anybody tell me what I'm doing wrong?

Thanks,

© Stack Overflow or respective owner

Related posts about iphone

Related posts about Xml