Forked from cmittendorf/RegisterSettingsBundleDefaults.swift
Last active
August 29, 2015 14:24
-
-
Save cciotti-ge/41dfa5bda945aea7b21f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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