Magical User settings class for iOS
To start using WRUserSettings just subclass WRUserSettings
class and you are ready to go!!!
So every property you add to your subclass will be stored in NSUserDefaults. Default property is nil or filled by default system value for primitive types like NSInteger, BOOL, CGRect etc.
To simple usage you only need add properties to header file of subclass:
class MyUserSettings: WRUserSettings {
dynamic var shouldShowTutorial: Bool = true
dynamic var temperatureUnit: String = "C"
dynamic var notificationOn: Bool = false
}
From now every time you set property is automatically save it in NSUserDefaults for you.
Your class is singleton so you should use +shared
method to get instance of it:
MyUserSettings.shared.shouldShowTutorial = false
Class will automatically save value false
to NSUserDefaults
To get value just get instance of class and property:
let shouldItReallyShowTutorial = MyUserSettings.shared.shouldShowTutorial
To set default values just use default assigment as above. We are storing defaults in instance.
If you want reset settings call anywhere method reset()
on your singleton. This method iterate through all saved settings and delete it from NSUserDefaults and assign to properties default values.
To print description of stored values simply print your singleton. It prints only stored values so it don't show default values that you set.
To use suiteName
all you need to do is override method func suiteName() -> String?
in your subclass:
class MyUserSettings: WRUserSettings {
dynamic var shouldShowTutorial: Bool = true
override func suiteName() -> String? {
return "com.chillicoder.my_app_group"
}
}
CocoaPods is a dependency manager for Cocoa projects. You can install it with the following command:
$ gem install cocoapods
CocoaPods 1.1.0+ is required to build WRUserSettings 3.0.0+.
To integrate WRUserSettings into your Xcode project using CocoaPods, specify it in your Podfile
:
source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '8.0'
use_frameworks!
target '<Your Target Name>' do
pod 'WRUserSettings', '~> 3.0.0'
end
Then, run the following command:
$ pod install
- Rewrite to support Swift 5.0.0
- Added example in swift
- Tests with 71% coverage
- Added support for suiteName
- Auto migration from standard user defaults
- Refactored Swift 4.0 version
- Added support for suiteName
- Auto migration when you start using suiteName
- BUG: fix problem with not unregistred notifications after deinit
- Refactored Swift 3.0 version
- Swift version
- Add -resetSettings
method
- Add simple tests
- Example now use pod instead of imported WRUserSettings
files.
- Added support for structures like: CGPoint
, CGRect
, CGSize
etc.
- Modify example to show CGPoint
usage.
- Basic stuff working
- Make example
- Tests
- [] 100% test coverage
WRUserSettings requires either iOS 8.0 and above.
WRUserSettings is available under the MIT license. See the LICENSE file for more info.
WRUserSettings uses ARC.