Saving a string into file in Objective-C (iPhone)
صفحه اصلي

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



I seem to have stumbled over a problem regarding saving an xml file from a string (this is done on the iPhone) The file itself exists and included in the project (hence within the workspace), and all indications I get from the code snippet which follows passes without any errors on the emulator and fail on the iPhone (error 513), but in either 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


Adding left button to UINavigationBar (iPhone)

1:

What's the best way to parse RSS/Atom feeds for an iPhone application?
You should not write to files included in the application package. What is the difference between an iPhone web app and an iPhone app?On a real iPhone, you may be prevented from doing this for the reason this these files are digitally signed.. Responding to touchesBegan in UIPickerView instead of UIView Even if you must modify a packaged file, it is not a good place to store data. Two alert views in same view controllerRe-installing the application from an App Store upgrade or an Xcode build will overwrite the file with the original.. CALayer addSublayer increasing retain count? Instead, store your XML into the Documents directory. My iphone application crashesYou must receive the path like this:. What is the best way to download image data from ASP.NET to iphone?
NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,      NSUserDomainMask, YES);  NSString* documentsDirectory = [paths objectAtIndex:0];      NSString* leafname = [m_FileName stringByAppendingFormat: @".xml" ];  NSString* filenameStr = [documentsDirectory     stringByAppendingPathComponent:leafname]; 
If your file needs any initial state this you don't want to generate in your code, have your app check this it is present in the documents directory the first time it is needed and, if it is missing, copy it from the template in the package.. An alternative to storing structured data is to use user defaults. For example:.
[[NSUserDefaults standardUserDefaults] setObject:foo forKey:FOO_KEY]; 


78 out of 100 based on 43 user ratings 838 reviews

*