nsuserdefaults

Setting bundle default value won't set

三世轮回 提交于 2019-12-18 18:58:12
问题 I have an iOS application with a settings.bundle that handles various settings for my application with Switch toggle . I set default values in my root.plist file (using the DefaultValue property) to YES but any time the application launches on a device or the iOS simulator all values go NO . It worked well only on the first launch. I am retrieving the defaults with this code (am I doing something wrong here?): NSUserDefaults *localeDefaults = [NSUserDefaults standardUserDefaults]; BOOL

Is there a better way to save a custom class to NSUserDefaults than encoding and decoding everything with NSCoder?

别等时光非礼了梦想. 提交于 2019-12-18 18:31:30
问题 My current class has around 50 lines just encoding and decoding variables in order for my class to be NSUserDefaults compatible. Is there a better way to handle this? Example: init(coder aDecoder: NSCoder!) { lightEnabled = aDecoder.decodeBoolForKey("lightEnabled") soundEnabled = aDecoder.decodeBoolForKey("soundEnabled") vibrateEnabled = aDecoder.decodeBoolForKey("vibrateEnabled") pulseEnabled = aDecoder.decodeBoolForKey("pulseEnabled") songs = aDecoder.decodeObjectForKey("songs") as! [Song]

Delete all my NSUserDefaults that start with a certain word

删除回忆录丶 提交于 2019-12-18 16:25:32
问题 Is there a way for me to "walk" through a list of all the NSUserDefault s in my iPhone app, and only delete certain ones? For example, I'd like to get all the key names that start with a certain word. Something like this: [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"Dog*"]; 回答1: You can look through the dictionaryRepresentation. Here's an implementation which uses NSPredicate as a generic matcher for greater flexibility. @interface NSUserDefaults (JRAdditions) - (void

NSInvalidUnarchiveOperationException cannot decode object error in Apple Watch extension

微笑、不失礼 提交于 2019-12-18 15:52:35
问题 I've got a user object that I need to store in NSUserDefaults and share with an iOS 8 extension app (Watchkit). In the main container app, I can encode and decode the object without any problem. However, when I try to retrieve the stored user object in the Extension, I get a "'NSInvalidUnarchiveOperationException', reason: '*** -[NSKeyedUnarchiver decodeObjectForKey:]: cannot decode object of class" error. As far as I can see NSCoding has been implemented correctly in the object (and I'm able

Persist a MPMediaItemCollection Object Using NSUserDefaults

自闭症网瘾萝莉.ら 提交于 2019-12-18 13:37:12
问题 How do you persist a MPMediaItemCollection object using NSUserDefaults? The MPMediaItemCollection object is a single song selected from the iPod. I have been struggling with this for hours! Anyone have any ideas or alternatives to NSUserDefaults or a conversion from MPMediaItemCollection, or anything?! Thanks... 回答1: First convert/encode the MPMediaItemCollection to an NSData Object and store it using NSUserDefaults using: NSData *data = [NSKeyedArchiver archivedDataWithRootObject

Save struct in class to NSUserDefaults using Swift

蓝咒 提交于 2019-12-18 12:29:45
问题 I have a class and inside the class is a (swift) array, based on a global struct. I want to save an array with this class to NSUserDefaults. This is my code: struct mystruct { var start : NSDate = NSDate() var stop : NSDate = NSDate() } class MyClass : NSObject { var mystructs : [mystruct] init(mystructs : [mystruct]) { self.mystructs = mystructs super.init() } func encodeWithCoder(encoder: NSCoder) { //let val = mystructs.map { $0 as NSObject } //this also doesn't work let objctvtmrec =

How can I store Push Notification alert message in UserDefault?

白昼怎懂夜的黑 提交于 2019-12-18 08:10:23
问题 I want to build an app that I can receive push notification and save inside client device for limit 25 Notification. If application running and may not running. How can I stored PushNotification alert message?. If app running that time arrived Notification alert message stored in UserDefault but when app in background or Inactive state that time not stored Notification alert message in UserDefault. I want to know that I need to use UserDefault or CoreData to store the Push Notification

NSUserDefaults, do something on first load only

笑着哭i 提交于 2019-12-18 07:04:49
问题 I'm trying to do show an alert the first time the app is launched. I'd also like to show this alert if they go into a settings page later on and want to see it again. I figured I could set a boolean in the NSUserDefaults . I'm not quite sure how to do that though (assuming this approach is right). I thought in applicationDidFinishLaunching:options , I could get the value for key @"FirstTimeLaunched" , and in applicationWillTerminate , I could set the object for that key to NO . If this is the

Save and Load Data on Today Extensions (iOS 8)

纵饮孤独 提交于 2019-12-18 05:54:16
问题 Is it possible to save and load data on Today Extension using NSUserDefaults? After closing the Notification Center, the widget behaves like an app which is terminated, so any data results lost. How could I solve this issue? This is my code: NSUserDefaults *defaults; - (void)viewDidLoad { [super viewDidLoad]; defaults = [NSUserDefaults standardUserDefaults]; NSArray *loadStrings = [defaults stringArrayForKey:@"savedStrings"]; if ([loadStrings objectAtIndex:0] != nil) { [display setText:

settings bundle not working on watchOS 2

筅森魡賤 提交于 2019-12-18 05:54:09
问题 This was NOT a problem on watchOS 1, but now on watchOS 2 I cannot read the values on the watch extension. According to Apple docs, it is possible. According to some people on this thread, it is possible. According to an Apple employee on this thread, it is possible. I'm setting everything up correctly as far as I can tell: I enabled App Groups on both iOS app and watch extension with the same identifier. I added Settings-Watch.bundle to the iOS app and added the