Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS Equivalent For Android Shared Preferences

People also ask

What can I use instead of shared preferences in Android?

Jetpack DataStore is a new and improved data storage solution aimed at replacing SharedPreferences. Built on Kotlin coroutines and Flow, DataStore provides two different implementations: Data is stored asynchronously, consistently, and transactionally, overcoming most of the drawbacks of SharedPreferences.

How can I see shared preferences in Android?

Open the device monitor by clicking it. Then you need to select the File Explorer tab in the device monitor. Find the data folder and find another data folder inside it. It will contain a folder having the name of your application package and there will be the desired SharedPreferences.

Where is shared preference stored in Android?

SharedPreferences are stored in an xml file in the app data folder, i.e. SharedPreferences added during runtime are not stored in the Eclipse project. The default shared preferences file would actually be: /data/data/<package>/shared_prefs/<package>_preferences. xml .


Use NSUserDefaults: - note that this is for small bits of data, such as the current level like you mentioned. Don't abuse this and use it as a large database, because it is loaded into memory every time you open your app, whether you need something from it or not (other parts of your app will also use this).

Objective-C:

Reading:

NSUserDefaults *preferences = [NSUserDefaults standardUserDefaults];

NSString *currentLevelKey = @"currentlevel";

if ([preferences objectForKey:currentLevelKey] == nil)
{
    //  Doesn't exist.
}
else
{
    //  Get current level
    const NSInteger currentLevel = [preferences integerForKey:currentLevelKey];
}

Writing:

NSUserDefaults *preferences = [NSUserDefaults standardUserDefaults];

NSString *currentLevelKey = @"currentlevel";

const NSInteger currentLevel = ...;
[preferences setInteger:currentLevel forKey:currentLevelKey];

//  Save to disk
const BOOL didSave = [preferences synchronize];

if (!didSave)
{
    //  Couldn't save (I've never seen this happen in real world testing)
}

.

Swift:

Reading:

let preferences = NSUserDefaults.standardUserDefaults()

let currentLevelKey = "currentLevel"

if preferences.objectForKey(currentLevelKey) == nil {
    //  Doesn't exist
} else {
    let currentLevel = preferences.integerForKey(currentLevelKey)
}

Writing:

let preferences = NSUserDefaults.standardUserDefaults()

let currentLevelKey = "currentLevel"

let currentLevel = ...
preferences.setInteger(currentLevel, forKey: currentLevelKey)

//  Save to disk
let didSave = preferences.synchronize()

if !didSave {
    //  Couldn't save (I've never seen this happen in real world testing)
}

Here is an update for Swift 3

Reading

let preferences = UserDefaults.standard

let currentLevelKey = "currentLevel"
if preferences.object(forKey: currentLevelKey) == nil {
    //  Doesn't exist
} else {
    let currentLevel = preferences.integer(forKey: currentLevelKey)
}

Writing

let preferences = UserDefaults.standard

let currentLevel = ...
let currentLevelKey = "currentLevel"
preferences.set(currentLevel, forKey: currentLevelKey)

Update

From UserDefaults documentation

synchronize() waits for any pending asynchronous updates to the defaults database and returns; this method is now unnecessary and shouldn't be used.


class Configuration {

    static func value<T>(defaultValue: T, forKey key: String) -> T{

        let preferences = UserDefaults.standard
        return preferences.object(forKey: key) == nil ? defaultValue : preferences.object(forKey: key) as! T
    }

    static func value(value: Any, forKey key: String){

        UserDefaults.standard.set(value, forKey: key)
    }

}

Example

//set
Configuration.value(value: "my_value", forKey: "key_1")

//get
let myValue = Configuration.value(defaultValue: "default_value", forKey: "key_1")