0

I am trying to detect if the iOS device is locked or unlocked using this method (https://stackoverflow.com/a/31430517/6348190), so it is necessary for me to set an NSDate to NSUserDefaults.

When I am trying to get this NSDate by key - I get nil.

Setting NSDate to NSUserDefaults:

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *keyForLocking = @"deviceLocked";
NSDate *dateNow = [[NSDate alloc] init];

[defaults setObject:dateNow forKey:keyForLocking];
[defaults synchronize];
NSLog(@"date is %@", [defaults objectForKey:keyForLocking]);

Getting it:

NSDate *oldDate = (NSDate *) [defaults objectForKey:keyForLocking];
NSLog(@"oldDate is %@", oldDate);

In console I get:

ProjectName[649:107036] date is 2016-05-24 22:06:36 +0000
ProjectName[649:107036] oldDate is (null)

What am I doing wrong?

I tried to update my NSLog with the key. And it's really common.

ProjectName[671:116804] oldDate is (null)
2016-05-25 01:58:03.601
ProjectName[671:116804] keyForLocking is deviceLocked
Community
  • 1
  • 1
Egor
  • 5
  • 1
  • 6
  • Please don't post your code or console output as an image. Please edit your question with the actual code and console output using copy and paste. (and *please* format it properly). – rmaddy May 24 '16 at 22:36
  • @rmaddy oh, I am sorry. I edited my question. – Egor May 24 '16 at 22:41
  • When do you call the "getting it" code? Are you sure that `keyForLocking` has the same value when getting the date as it had when setting the date? – rmaddy May 24 '16 at 22:43
  • I am calling it when device is unlocking. Yes, I am sure, this is a common variable – Egor May 24 '16 at 22:51
  • To be sure, update your `NSLog` statements to also log `keyForLocking` (and put it in quotes too). Update your question with the updated output. – rmaddy May 24 '16 at 22:53
  • @rmaddy Don't u know the solution of my problem? – Egor May 25 '16 at 15:14

0 Answers0