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
NSData, NSString, NSNumber, NSDate, NSArray, 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
- 浏览: 2488480 次
- 性别:
- 来自: 成都
文章分类
最新评论
-
nation:
你好,在部署Mesos+Spark的运行环境时,出现一个现象, ...
Spark(4)Deal with Mesos -
sillycat:
AMAZON Relatedhttps://www.godad ...
AMAZON API Gateway(2)Client Side SSL with NGINX -
sillycat:
sudo usermod -aG docker ec2-use ...
Docker and VirtualBox(1)Set up Shared Disk for Virtual Box -
sillycat:
Every Half an Hour30 * * * * /u ...
Build Home NAS(3)Data Redundancy -
sillycat:
3 List the Cron Job I Have>c ...
Build Home NAS(3)Data Redundancy
发表评论
-
ionic UI(5)UI and Backend
2016-12-02 03:22 565ionic UI(5)UI and Backend 1 Pr ... -
Stanford Cource(2)Demo App Caculator
2014-06-24 01:29 865Stanford Cource(2)Demo App Ca ... -
Mono on MAC
2014-06-04 03:27 947Mono on MACJust fine the tool f ... -
IOS7 App Development Essentials(4)IPhone5, IPhone5s, IPhone5c
2014-04-11 03:59 943IOS7 App Development Essentia ... -
IPhone and Location(2)Documents Region Monitoring and Region Sample
2013-10-18 05:10 1699IPhone and Location(2)Documents ... -
IPhone and Location(1)Documents User Location
2013-10-18 03:50 1278IPhone and Location(1)Documents ... -
Learn Objective C(6)Programming with Objective-C - Working with Blocks and Deali
2013-10-18 00:02 895Learn Objective C(6)Programming ... -
Learn Objective C(5)Programming with Objective-C - Working with Protocols and Va
2013-10-17 23:47 967Learn Objective C(5)Programming ... -
Learn Objective C(4)Programming with Objective-C - Encapsulating Data and Custom
2013-10-17 23:23 907Learn Objective C(4)Programming ... -
Learn Objective C(3)Programming with Objective-C - Defining Classes, Working wit
2013-10-17 23:09 992Learn Objective C(3)Programmi ... -
Learn Objective C(2)Learn Objective-C in Day 6 - 4 ~ 6
2013-10-17 00:30 926Learn Objective C(2)Learn Obj ... -
Learn Object C(1) Learn Objective-C in Day 6 - 1 ~ 3
2013-10-17 00:22 1076Learn Object C(1) Learn Objec ... -
APNS(4)Recall the Process and Learn Java APNS
2013-04-18 02:48 3430APNS(4)Recall the Process and L ... -
Build the iOS Things with J2Objc
2013-04-12 03:25 2434Build the iOS Things with J2Obj ... -
APNS(3)Write the Easy Client App
2013-01-15 07:23 1608APNS(3)Write the Easy Client Ap ... -
APNS(2)Try to Finish the first Example
2013-01-14 07:56 1501APNS(2)Try to Finish the first ... -
Stanford Cource(1)MVC and Object-C
2012-12-14 14:04 1267Stanford Cource(1)MVC and Objec ... -
Some VI Tips
2012-11-15 04:48 1062Some VI Tips Today, I need to c ... -
MAC Mini Setup
2012-09-25 18:45 1288MAC Mini Setup I am dealing wit ... -
Android Talker(1)MAC Environment
2012-09-01 00:16 1892Android Talker(1)MAC Environmen ...
相关推荐
iOS 10 App Development Essentials: Learn to Develop iOS 10 Apps with Xcode 8 and Swift 3 Author: Neil Smyth Length: 816 pages Edition: 1 Language: English Publisher: CreateSpace Independent Publishing...
iOS 11 App Development Essentials Learn to Develop iOS 11 Apps with Xcode 9 and Swift 4 英文epub 本资源转载自网络,如有侵权,请联系上传者或csdn删除 查看此书详细信息请在美国亚马逊官网搜索此书
iOS 11 App Development Essentials Learn to Develop iOS 11 Apps with Xcode 9 and Swift 4
iOS 8 应用开发基础 源码2 using Xcode 6.3 (Swift 1.2): iOS 8 App Development Essentials Source Code
iOS 11 App Development Essentials Learn to Develop iOS 11 Apps with Xcode 9 and Swift 4 英文mobi 本资源转载自网络,如有侵权,请联系上传者或csdn删除 查看此书详细信息请在美国亚马逊官网搜索此书
iOS 7 App Development Essentials 英文无水印pdf pdf所有页面使用FoxitReader和PDF-XChangeViewer测试都可以打开 本资源转载自网络,如有侵权,请联系上传者或csdn删除 本资源转载自网络,如有侵权,请联系...
iOS 11 App Development Essentials – First Edition 2018
iOS 10 App Development Essentials 英文mobi 本资源转载自网络,如有侵权,请联系上传者或csdn删除 本资源转载自网络,如有侵权,请联系上传者或csdn删除
iOS 9 App Development Essentials 英文无水印pdf pdf所有页面使用FoxitReader和PDF-XChangeViewer测试都可以打开 本资源转载自网络,如有侵权,请联系上传者或csdn删除 本资源转载自网络,如有侵权,请联系...
非常不错的一本学习iOS开发的基础书籍,最主要它是For iOS7和Xcode5的。
iOS 11 App Development Essentials Learn to Develop iOS 11 Apps with Xcode 9 and Swift 4 英文无水印转化版pdf pdf所有页面使用FoxitReader、PDF-XChangeViewer、SumatraPDF和Firefox测试都可以打开 本资源...
本书概述了iPhone硬件和iOS 4的体系结构。提供了Objective-C编程的简介,然后深入研究了iPhone应用程序和用户界面的设计。
介绍如何使用Xcode和Swift构建适用于iOS 8的应用程序。
本书旨在教授使用 iOS 16 SDK、UIKit、Xcode 14 和 Swift 编程语言创建 iOS 应用程序所需的技能。 本书从基础开始,概述了设置 iOS 开发环境所需的步骤。接下来,介绍了 iOS 16 的架构和 Swift 5.7 中的编程,然后...
Title: iOS 8 App Development Essentials Author: Neil Smyth Length: 824 pages Edition: 1 Language: English Publisher: CreateSpace Independent Publishing Platform Publication Date: 2014-12-16 ISBN-10: ...
提供开始构建iPad应用所需的知识。
Ios 9 Game Development Essentials
iOS 9 App Development Essentials is latest edition of this popular book series and has now been fully updated for the iOS 9 SDK, Xcode 7 and the Swift 2 programming language. Beginning with the ...
使用Eclipse IDE和Android 4.2 SDK的Android应用程序开发简介。 本书假定您具有Java编程经验。