This issue has been cause of lots of frustration , as NSUserDefaults stopped working.
i am saving facebook information in userdefaults for later use , but it is returning nil.
Note : i get nil when i try to slide in side navigation (using SWRevealViewController) bar.
here is my code :
[[NSUserDefaults standardUserDefaults] setValue:[NSString stringWithFormat:#"%#",[RespArray valueForKey:#"image_url"]] forKey:#"profilePic"];
[[NSUserDefaults standardUserDefaults] synchronize];
What i have tried :
- add/remove synchronize
- clearing all NSUserDefaults from app
- using setObject , SetValue for NSUserDefault key
- using NSString Stringwithformat , for retrieving strings.
i tried each and every solution available on SO and web.
Related
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).
i have been working on a app, and needed to store a string. I used this code to set the default:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setValue:CertificateKey.stringValue forKey:#"SavedKey"];
[[NSUserDefaults standardUserDefaults] synchronize];
NSLog(#"%#",[defaults objectForKey:#"SavedKey"]);
I loged it, so i know it saved...well, it showed me the value.
When i open my application, I use this to retrieve the default:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[CertificateKey setStringValue:[defaults objectForKey:#"SavedKey"]];
[CertificateKey setTitleWithMnemonic:[defaults objectForKey:#"SavedKey"]];
[[NSUserDefaults standardUserDefaults] synchronize];
Why will it not get the default value? Did i not completely save it?
Don't quit the application by pressing the stop button in xcode. Instead, quit it by right clicking on the application icon and selecting "Quit".
Edit
Maybe the first time that you execute the application, you want to save some defaults but you don't want to set them the second+ time that the application runs.
For this purpose in some class initialize method register the defaults, like this:
+ (void) initialize
{
NSUserDefaults* defaults= [NSUserDefaults standardUserDefaults];
[defaults registerDefaults: #{} ];
// This case it's an empty dictionary, but you can put whatever you want inside it.
// just convert it to data if it's not an object storable to a plist file.
}
Also, you're using setValue:forKey: , that method is inherited from NSObject. Use setObject:forKey: .
And use finalize if you want to save the defaults at the end:
- (void) finalize
{
// Save the defaults here
}
You might have a problem because you are creating an instance of NSUserDefaults. From what I understand you are supposed to access it like so: [[NSUserDefaults standardUserDefaults] setValue:#"Pikachu" forKey:#"Best Pokemon Ever"]; [[NSUserDefaults standardDefaults] objectForKey:#"Best Pokemon Ever"]; Rather than actually creating an instance of it.
Tweetbot and Clear show's on the first start of the app a small tutorial screen how the app works. The screen with the small tutorial only pops up on the first start up of the app (1 time)
How and with what can i make a similar thing? Can anyone push me in the right direction?
View i mean:
I'm assuming by Xcode you actually mean iOS.
What you need to do is use the NSUserDefaults class to store a flag indicating whether the user has seen the tutorial screen before.
When your app first loads (or at the point you want to decide whether or not to show the tutorial screen), do something like this:
if(![[NSUserDefaults standardUserDefaults] boolForKey:#"hasSeenTutorial"])
[self displayTutorial];
This checks the saved NSUserDefaults for the current user for a value named "hasSeenTutorial", which won't exist yet. Since it doesn't exist, it will call displayTutorial. displayTutorial refers to your method for creating the tutorial view. You can figure out that part.
Then, once the user closes the tutorial screen:
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:#"hasSeenTutorial"];
That value will be saved for your user profile, meaning the next time it checks it, it will be true, so displayTutorial won't be called.
In your viewDidLoad:
if (![#"1" isEqualToString:[[NSUserDefaults standardUserDefaults]
objectForKey:#"aValue"]]) {
[[NSUserDefaults standardUserDefaults] setValue:#"1" forKey:#"aValue"];
[[NSUserDefaults standardUserDefaults] synchronize];
//Action here
}
Certainly, if we would like to tell user something about features after update (not only app launched first time), the solution below could be suitable.
In your viewDidLoad:
NSString *currentBundleVersion = [[NSBundle mainBundle] objectForInfoDictionaryKey:#"CFBundleVersion"];
NSString *previousBundleVersion = [[NSUserDefaults standardUserDefaults] objectForKey:#"PreviousBundleVersion"];
if (![currentBundleVersion isEqualToString:previousBundleVersion] ) {
// Here you can initialize your introduction view and present it!
}
Once the user closes the intro:
NSString *currentBundleVersion = [[NSBundle mainBundle] objectForInfoDictionaryKey:#"CFBundleVersion"];
NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults];
if (standardUserDefaults) {
[standardUserDefaults setObject:currentBundleVersion forKey:#"PreviousBundleVersion"];
[standardUserDefaults synchronize];
}
In this case app bundle version stored in your standardUserDefaults will be differ from current bundle version only after update and shown only once as well as at first launch.
Initialise your user defaults with a BOOL, something called instructionsSeen (or whatever you want) and set it to NO in your App delegate's initialize method.. In your app, test this value and if it is NO display your tutorial screen. As part of showing and displaying this screen, set the instructionsSeen to YES and store it in your defaults.
This way the demo screen will only show on first launch, unless the user uninstalls and installs the app again.
You could also show the demo for a small number of launches (say 3). In this case, don't use BOOL use a number and increment it instead.
Xamarin.iOS Version within AppDelegate:
UIStoryboard storyboard = UIStoryboard.FromName("Main", null);
if (NSUserDefaults.StandardUserDefaults.BoolForKey ("hasSeenTutorial") == false) {
UIViewController vc = storyboard.InstantiateViewController ("StartPageViewController");
this.Window.RootViewController = vc;
} else {
UIViewController vc = storyboard.InstantiateViewController ("NonStartPageViewController");
this.Window.RootViewController = vc;
}
this.Window.MakeKeyAndVisible ();
In my StartPageViewController, I have a button which sets NSUserDefaults to true, so the next time it runs, it will start off with the NonStartPageViewController:
partial void RegisterButton_TouchUpInside (UIButton sender)
{
NSUserDefaults.StandardUserDefaults.SetBool(true,"hasSeenTutorial");
NSUserDefaults.StandardUserDefaults.Synchronize();
}
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if (![defaults objectForKey:#"firstRun"]) {
[defaults setObject:[NSDate date] forKey:#"firstRun"];
[self displayTutorial];
}
Swift version :
if !(NSUserDefaults.standardUserDefaults().boolForKey("seenTutorial")) {
//Tutorial part
NSUserDefaults.standardUserDefaults().setBool(true, forKey: "seenTutorial")
}
Everyone is making this more complex and vauge than it needs to be... Simple complete solution.
In the ViewDidLoad:
if ([[NSUserDefaults standardUserDefaults] boolForKey:#"FirstLoadKey"]) {
self.imageView.hidden = YES;
}else{
self.imageView.hidden = NO;
[[NSUserDefaults standardUserDefaults]setBool:YES forKey:#"FirstLoadKey"];
[[NSUserDefaults standardUserDefaults]synchronize];
}
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...
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) ...