`
sillycat
  • 浏览: 2488480 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

IOS7 App Development Essentials(3)NSUserDefaults

 
阅读更多

IOS7 App Development Essentials(3)NSUserDefaults

In Android world, we have SharedPreferences, in iOS world, we should use NSUserDetails.

The NSUserDefaults class provides a interface for interacting with the defaults system.

Applications record such preferences by assigning values to a set of parameters in a user’s defaults databases. NSUserDefaults caches the information to avoid having to open the user’s defaults database.

It will support these classes
NSDataNSStringNSNumberNSDateNSArray, or NSDictionary

It support NSDictionary.

Here is How I use NSUserDefaults

Define the interface
@interface SLUserDefaults : NSObject + (NSDictionary *)getBeaconDataDictionary; + (void)setBeaconDataDictionary:(NSDictionary *)beacons;

+(void)setBeaconRangingOutside:(NSString *)identifier; +(NSString *)getBeaconRangingOutside;

+ (void)setBeaconRangingBeacons:(NSSet *)beacons; + (NSSet *)getBeaconRangingBeacons;

+ (BOOL)isBeaconRangeRunning; + (void)setBeaconRangeRunning:(BOOL) status;

Here is the implementation
#import “SLUserDefaults.h"staticNSString *const DEVICE_REGISTERED_KEY = @“SL_isRegistered"; staticNSString *const IS_STARTED_KEY = @“SL_isStarted";

@implementation LPUserDefaults

+ (NSDictionary *)getBeaconDataDictionary{    @synchronized(self){        if([[NSUserDefaultsstandardUserDefaults] objectForKey:BEACON_DATA_DIC]){            return [NSKeyedUnarchiverunarchiveObjectWithData:[[NSUserDefaultsstandardUserDefaults] objectForKey:BEACON_DATA_DIC]];        }else{            return [[NSDictionaryalloc] init];        }    } } + (void)setBeaconDataDictionary:(NSDictionary *)beacons {    @synchronized(self){        if(beacons != nil && [beacons count] > 0){            NSData *encodedObjects = [NSKeyedArchiverarchivedDataWithRootObject:beacons];            [[NSUserDefaultsstandardUserDefaults] setObject:encodedObjects forKey:BEACON_DATA_DIC];        }    } }

+ (void)setBeaconDataDictionary:(NSDictionary *)beacons {    @synchronized(self){        if(beacons != nil && [beacons count] > 0){            NSData *encodedObjects = [NSKeyedArchiverarchivedDataWithRootObject:beacons];            [[NSUserDefaultsstandardUserDefaults] setObject:encodedObjects forKey:BEACON_DATA_DIC];        }    } } +(void)setBeaconRangingOutside:(NSString *)identifier{    @synchronized(self){        [[NSUserDefaultsstandardUserDefaults] setValue:identifier forKey:BEACON_RANGING_OUTSIDE];    } }

+ (void)setBeaconMonitoringBeacons:(NSSet *)beacons {    @synchronized(self){        if(beacons != nil && [beacons count] > 0){            NSData *encodedObjects = [NSKeyedArchiverarchivedDataWithRootObject:beacons];            [[NSUserDefaultsstandardUserDefaults] setObject:encodedObjects forKey:BEACON_MONITORING_BEACONS];        }    } } + (NSSet *)getBeaconMonitoringBeacons{    @synchronized(self){        if([[NSUserDefaultsstandardUserDefaults] objectForKey:BEACON_MONITORING_BEACONS]){            return [NSKeyedUnarchiverunarchiveObjectWithData:[[NSUserDefaultsstandardUserDefaults] objectForKey:BEACON_MONITORING_BEACONS]];        }else{            return [[NSSetalloc] init ];        }    } }

+ (BOOL)isRegistered {    @synchronized(self) {        return [[NSUserDefaultsstandardUserDefaults] boolForKey:DEVICE_REGISTERED_KEY];    } } + (void)setRegistered {    @synchronized(self) {        [[NSUserDefaultsstandardUserDefaults] setBool:YESforKey:DEVICE_REGISTERED_KEY];    } }


References:
http://sillycat.iteye.com/blog/1841920

Store Object
http://stackoverflow.com/questions/2315948/how-to-store-custom-objects-in-nsuserdefaults


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics