nsuserdefaults

Swift: Insert Alert Box with Text Input (and Store Text Input )

こ雲淡風輕ζ 提交于 2019-12-04 17:04:08
问题 In one of my viewController , I want to make an alert box appear that prompts the user to type this information.Then, I want the user to store this input using NSUserDefaults . How can I achieve this? Thank you in advance! 回答1: Check this out: let alertController = UIAlertController(title: "Email?", message: "Please input your email:", preferredStyle: .alert) let confirmAction = UIAlertAction(title: "Confirm", style: .default) { (_) in guard let textFields = alertController.textFields,

NSUserDefaults refuse to save

空扰寡人 提交于 2019-12-04 16:53:05
Start a new single view project and update the main ViewController's viewDidLoad. The intention is to retrieve and increment a value stored in NSUserDefaults and save it. - (void)viewDidLoad { [super viewDidLoad]; NSString *key = @"kTheKey"; NSNumber *number = [[NSUserDefaults standardUserDefaults] objectForKey:key]; NSLog(@"current value is %@", number); NSNumber *incremented = @(number.integerValue + 1); NSLog(@"new value will be %@", incremented); [[NSUserDefaults standardUserDefaults] setObject:incremented forKey:key]; [[NSUserDefaults standardUserDefaults] synchronize]; NSLog(@"reboot");

Save Session Data in iPhone

被刻印的时光 ゝ 提交于 2019-12-04 16:50:41
I want to store data at different points in my app, that is accessible to objects across the app. Something similar to session in php or global variables. I know I can use NSUserDefaults but i am not sure how i would keep on adding values to it and then accessing it. Example, first i want to store the Username that is used during login, then on 3rd screen, i want to save the Company of that user that he selects from a list of companies. Then on the 5th screen i want to save the location that the user selects. And then I have different functionalities that the user can use depending on his

Reading preferences set by UIAutomation's UIAApplication.setPreferencesValueForKey() on the target device?

我的梦境 提交于 2019-12-04 15:47:54
Over the last few days I've been playing with Apple's UIAutomation framework in an attempt to try to put together a suite of acceptance tests to drive the development of an app I'm working on (in a BDD type way...). One thing I'm bumping up against is how to get the SUT into a given state so I can begin my tests if I need to set some internal state for that to happen. The problem is that Apple's Javascript environment doesn't provide any functionality I could use to communicate with the device other than through it's UI (I'm sure this is probably by design but sometimes this is just

What is the correct way to save NSUserDefaults when my app is terminated on iOS

微笑、不失礼 提交于 2019-12-04 12:26:04
问题 I need to save my NSUserDefault when my app is completely quit in iOS. not didenterbackground and foreground . Only when user kill my app with minus(-) sign in iOS. So i write following codes in applicationWillTerminate of AppDelegate . - (void)applicationWillTerminate:(UIApplication *)application { [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"iCloud"]; [[NSUserDefaults standardUserDefaults] synchronize]; } But it doesn't save to NSUserDefault . So where should i write to save

Way to persist MPMediaItemCollection objects? (selected from iPod)

◇◆丶佛笑我妖孽 提交于 2019-12-04 12:06:26
问题 I am making an app in which the user can select a song in a settings tab and have this played in a different view on demand. I want it so that this item can be stored if the user is to shut the app and reopen it another time. I have managed to allow the user to select and store a song in with: -(IBAction)showMediaPicker:(id)sender{ MPMediaPickerController *mediaPicker = [[MPMediaPickerController alloc] initWithMediaTypes: MPMediaTypeAny]; mediaPicker.delegate = self; mediaPicker

How to store an retrieve float from NSUserDefaults

你离开我真会死。 提交于 2019-12-04 11:30:58
I'm having trouble storing and retrieving a float in NSUserDefauts. I store the value, but when I retrieve it, it returns 0. here's what I tried and didn't work: [pref setFloat:3.0f forKey:@"key"]; float value = [pref floatForKey:@"key"]; //value=0 [pref setFloat:3 forKey:@"key"]; float value = [pref floatForKey:@"key"];//value=0 [pref setObject:[NSNumber numberWithFloat:3] forKey:@"key"]; float value = [[pref objectForKey:@"key"]floatValue];//value=0 [pref setObject:[NSNumber numberWithFloat:3.0f] forKey:@"key"]; float value = [[pref objectForKey:@"key"]floatValue];//value=0 What am I doing

Increment integer in NSUserDefaults

橙三吉。 提交于 2019-12-04 10:50:36
Is there a more elegant way to increment a counter stored in user defaults? let defaults = NSUserDefaults.standardUserDefaults() defaults.setInteger(defaults.integerForKey("counter")+1, forKey: "counter") No but if you do this a lot it might make a nice extension, something like this. extension NSUserDefaults { class func incrementIntegerForKey(key:String) { let defaults = standardUserDefaults() let int = defaults.integerForKey(key) defaults.setInteger(int+1, forKey:key) } } Usage like this NSUserDefaults.incrementIntegerForKey("counter") This is the same as the solution offered by Wezly, but

OSX NSUserDefaults not Working

ε祈祈猫儿з 提交于 2019-12-04 09:24:19
This code gives me always NO in my application. It does indeed work in any other project I copy it... so something must be messed up with my standardUserDefaults, but I absolutely don't know how this can happen and how to solve it! NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; [defaults setBool:YES forKey:@"myKey"]; BOOL test = [defaults boolForKey:@"myKey"]; // test is ALWAYS NO here! Can anybody hint me, where to start or how to get rid of this? It's a mixed project with swift and objective c and get the same behavior in me AppDelegate.swift, when I put this directly in

NSUserDefaults not present on first run on simulator

非 Y 不嫁゛ 提交于 2019-12-04 09:03:40
I've got some settings saved in my Settings.bundle, and I try to use them in application:didFinishLaunchingWithOptions , but on a first run on the simulator accessing objects by key always returns nil (or 0 in the case of ints). Once I go to the settings screen and then exit, they work fine for every run thereafter. What's going on? Isn't the point of using default values in the Settings.bundle to be able to use them without requiring the user to enter them first? If I got your question right, in your app delegate's - (void)applicationDidFinishLaunching:(UIApplication *)application, set the