Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save cciotti-ge/41dfa5bda945aea7b21f to your computer and use it in GitHub Desktop.
Save cciotti-ge/41dfa5bda945aea7b21f to your computer and use it in GitHub Desktop.
import Foundation
if let bundleURL = NSBundle.mainBundle().URLForResource("Settings", withExtension: "bundle") {
NSUserDefaults.registerDefaults(settingsBundleURL: bundleURL)
}
extension NSUserDefaults {
static func registerDefaults(#settingsBundleURL: NSURL) {
if let rootDict = NSDictionary(contentsOfURL: settingsBundleURL.URLByAppendingPathComponent("Root.plist")) {
var defaults: NSUserDefaults?
if let containerIdentifier = rootDict.valueForKey("ApplicationGroupContainerIdentifier") as? String {
defaults = NSUserDefaults(suiteName: containerIdentifier)
} else {
defaults = NSUserDefaults.standardUserDefaults()
}
if let defaults = defaults, preferences = rootDict.valueForKey("PreferenceSpecifiers") as? [[String:AnyObject]] {
var registrationDictionary: [NSObject:AnyObject] = [:]
for prefs in preferences {
if let type = prefs["Type"] as? String {
switch type {
case "PSToggleSwitchSpecifier":
if let boolValue = prefs["DefaultValue"] as? Bool, let key = prefs["Key"] as? String {
registrationDictionary[key] = boolValue
}
case "PSSliderSpecifier":
if let floatValue = prefs["DefaultValue"] as? Float, let key = prefs["Key"] as? String {
registrationDictionary[key] = floatValue
}
case "PSTextFieldSpecifier", "PSMultiValueSpecifier":
if let textValue = prefs["DefaultValue"] as? String, let key = prefs["Key"] as? String {
registrationDictionary[key] = textValue
}
default:
continue
}
}
}
defaults.registerDefaults(registrationDictionary)
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment