Saving a string to a file in Objective-C (iPhone)

It seems that I came across the problem of saving the XML file from a string (this is done on the iPhone) The file itself exists and is included in the project (therefore, within the workspace), and all the instructions that I get from the code fragment that follows passes without any Any errors in the emulator and crash on the iPhone (error 513), but in any case the file is not saved!

{
Hits = config->Hits;

NSString*   filenameStr = [m_FileName stringByAppendingFormat: @".xml" ]; 
NSString*   pData = [self getDataString];  // write xml format - checked out ok
NSError     *error;

/* option 2 - does not work as well
 NSBundle        *mainBundle = [NSBundle mainBundle];
 NSURL           *xmlURL = [NSURL fileURLWithPath:[mainBundle pathForResource: m_FileName ofType: @"xml"]];

 if(![pData writeToURL: xmlURL atomically: true encoding:NSUTF8StringEncoding error:&error]) 
 {
 NSLog(@"Houston - we have a problem %s@\n",[error localizedFailureReason]);
 return false;
 }
 */

if(![pData writeToFile: filenameStr atomically: FALSE encoding:NSUTF8StringEncoding error:&error]) 
{
    NSLog(@"Houston - we have a problem %s@\n",[error localizedFailureReason]);
    return false;
}
return true;

}

Any help would be appreciated, -A

+3
source share
1 answer

You should not write files included in the application package. On a real iPhone, you may be denied this because these files are digitally signed.

, . App Store Xcode .

XML Documents. :

NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, 
    NSUserDomainMask, YES); 
NSString* documentsDirectory = [paths objectAtIndex:0];     
NSString* leafname = [m_FileName stringByAppendingFormat: @".xml" ]; 
NSString* filenameStr = [documentsDirectory
    stringByAppendingPathComponent:leafname];

- , , , , , , .

. :

[[NSUserDefaults standardUserDefaults] setObject:foo forKey:FOO_KEY];
+19

Source: https://habr.com/ru/post/1715262/


All Articles