How to write data to an xml file - objective-c

I want to store data (preferences) into an xml file , but I can't achieve it. I took a look at appledeveloper web but it seems not clear to me .
I want to store the xml file on disk . My preferences that I created are some checkboxes (ih the user cheks the login box for example and I add this preference login to the xml file) , radio group, textfields
can you give me a good tutorial or an example that shows that please?

You usually store preferences using the NSUserDefaults class.
Example:
[[NSUserDefaults standardUserDefaults] setObject:#"Option1" forKey:#"PreferenceName1"];
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:#"PreferenceName2"];
And then read like this:
NSString *pref1 = [[NSUserDefaults standardUserDefaults] objectForKey:#"PreferenceName1"];
BOOL pref2 = [[NSUserDefaults standardUserDefaults] boolForKey:#"PreferenceName2"];

Related

NSUserDefaults is not saving any value in cocoa [duplicate]

This question already has answers here:
Mac sandbox created but no NSUserDefaults plist
(5 answers)
Closed 5 years ago.
I am trying to save BOOL value and some simple string using NSUserDefaults. But NSUserdefaults is not saving any value. I have also used synchronize after saving the value into NSUserDefaults.The way I am using to save the BOOL into NSUserDefaults is as below.
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:#"loadfirsttimewindow"];
[[NSUserDefaults standardUserDefaults] synchronize];
And the way I am using to save the string into NSUserDefaults is as below.
[[NSUserDefaults standardUserDefaults] setValue:title1 forKey:#"item1"];
[[NSUserDefaults standardUserDefaults] synchronize];
So please let me know that where my NSUserDefaults value has been stored and why it is not saving any value into NSUserDefaults.
Simply I just want to save value which will remain saved even after the application has been closed.
The sample of code I have used for saving the value into NSUserDefaults in given below:
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
NSLog(#"%hhd",[[NSUserDefaults standardUserDefaults] boolForKey:#"loadfirsttimewindow"]);
if (![[NSUserDefaults standardUserDefaults] boolForKey:#"loadfirsttimewindow"])
{
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:#"loadfirsttimewindow"];
[[NSUserDefaults standardUserDefaults] synchronize];
*I have written code for the thing I have to do only when the application is launched for the first time.*
}
else
{
*I have written code for the thing I have to after the application is launched for the first time.*
}
}
I have found my solution from the link given below:
Mac sandbox created but no NSUserDefaults plist
When you move the container while testing / debugging to the trash, the cfprefsd (see Activity Monitor) still keeps a link to the .plist. Empty the trash and force quit both cfprefsd (user and root).

How to remove all NSUser Defaults except two objects?

I am using a lot NSUserDefaults and i want every time the app starts to remove them except two objects is this somehow possible?
Any help appreciated
I assume you know which two objects you want to keep? If that's the case use this code:
id obj1 = [[NSUserDefaults standardUserDefaults] objectForKey#"keyForObj1"];
id obj2 = [[NSUserDefaults standardUserDefaults] objectForKey#"keyForObj2"];
NSString *appDomain = [[NSBundle mainBundle] bundleIdentifier];
[[NSUserDefaults standardUserDefaults] removePersistentDomainForName:appDomain];
[[NSUserDefaults standardUserDefaults] setObject:obj1 forKey:#"keyForObj1"];
[[NSUserDefaults standardUserDefaults] setObject:obj2 forKey:#"keyForObj2"];
You would need to keep track of the keys yourself and remove the objects associated with them on launch. More importantly, though: why are you storing data in NSUserDefaults that you only want to persist through a single run of the app? That sort of data should probably be kept in memory.
You can save Those two items under separate a key and Add into the Code during ViewDidLoad.
Let me know the Actual Problem.

Cocoa How to know if a file was edited/closed

I open a file programmatically with the default application taking into account the file extension.
When the file is open, I need to know when the file is modified and when is no longer in use.
I used FSEventStreamCreate to create the event.
How can I know if the file was closed?
Thanks,
Ana
You can check the modified date of the file.
Here is the solution to see if my file was modified:
NSString * modified = [NSString stringWithFormat:#"%#",[fileAttributes objectForKey:#"NSFileModificationDate"]];
NSString * savedmod = [[NSUserDefaults standardUserDefaults] objectForKey:#"kFeedLastModified"];
[[NSUserDefaults standardUserDefaults] setObject:modified forKey:#"kFeedLastModified"];
[[NSUserDefaults standardUserDefaults] synchronize];
if (![savedmod isEqualToString:modified])
{
NSlog(#"File modified");
}

How to fetch the value saved in NSUserdefault

I am trying to use NSUser Deafault in my application user have tp fillup 4 UITextFields. I am trying to save the fields so that on subsequent runs of the program that whatever they previously put will already be displayed in those UITextFields so the wont have to re-input it in - unless they want to edit something in which case they still have that option. I think that I have figured out a good way to save the strings using NSUserDefaults but now I am trying to figure out how to have those fields populate a UITextField - it doesnt seem as easy as if they were UILabels. This is the route I am attempting:
Now my value is being saved in nsuserDefalut,but hot how to fetch dat value next time(when i re start application (null appear on the uitext field)) ..I am trying this cods
NSUserDefaults *userData = [NSUserDefaults standardUserDefaults]; //Hooks.
NSString *placeHolderName = [userData stringForKey:#"name"];
urltextFieldNormal.text = placeHolderName;
Do you remember to sync NSUserDefaults when you save the strings?
[[NSUserDefaults standardUserDefaults] setObject:#"Hello mister nick" forKey:#"alarmsArray"];
[[NSUserDefaults standardUserDefaults] synchronize];
Cheers mate...

Save Username & Pass Objective C iPhone

i have a textfield,
Number and Password
i have no clue how to save these settings and then read them as the app starts to check if they have been set or not.
Thanks
Mason
Sensitive information should be stored in the keychain, a cryptographically secure location on the device. If you save the username and/or password in NSUserDefaults, you're saving them as plaintext, which is inherently insecure.
There're are plenty of examples on the internet of how to use the keychain on the iPhone, include simple wrappers to use in your code. For example, here's some pretty good code on Github that makes it quite easy:
http://github.com/ldandersen/scifihifi-iphone/tree/master/security
To Save:
[[NSUserDefaults standardUserDefaults] setValue:_email forKey:#"email"];
[[NSUserDefaults standardUserDefaults] setValue:_password forKey:#"password"];
[[NSUserDefaults standardUserDefaults] synchronize];
To Read:
_email = [[NSUserDefaults standardUserDefaults] stringForKey:#"email"];
_password = [[NSUserDefaults standardUserDefaults] stringForKey:#"password"];
In your case:
[[NSUserDefaults standardUserDefaults] setValue:Number.text forKey:#"Number"];
And:
NSString * number = [[NSUserDefaults standardUserDefaults] stringForKey:#"Number"];
The "Key" is usually hard-coded and works like a "variable name" for things in storage (typical name/value pairs like a dictionary).
To check if the value has been set or not; after you read it:
if (number) ...