`
sillycat
  • 浏览: 2551315 次
  • 性别: 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


分享到:
评论

相关推荐

    iOS 10 App Development Essentials

    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 epub

    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

    3. 苹果开发者计划的加入:本书籍指导读者如何加入苹果开发者计划,以便更好地进行iOS应用的开发和测试。其中包括下载Xcode 9、加入开发者计划的时机和步骤,以及如何使用开发者ID进行开发和分发。 4. Xcode 9的...

    iOS 11 App Development Essentials Learn to Develop iOS 11 Apps with Xcode 9 mobi

    iOS 11 App Development Essentials Learn to Develop iOS 11 Apps with Xcode 9 and Swift 4 英文mobi 本资源转载自网络,如有侵权,请联系上传者或csdn删除 查看此书详细信息请在美国亚马逊官网搜索此书

    iOS 8 App Development Essentials Source Code

    《iOS 8应用开发基础》源码解析 本资源包含了使用Xcode 6.3(Swift 1.2)编写的iOS 8应用程序开发的基础源码。这些源代码旨在帮助开发者深入理解iOS 8应用开发的基本概念和技术,通过实际操作来学习Swift语言以及...

    iOS 10 App Development Essentials mobi

    iOS 10 App Development Essentials 英文mobi 本资源转载自网络,如有侵权,请联系上传者或csdn删除 本资源转载自网络,如有侵权,请联系上传者或csdn删除

    iOS7 App Development Essentials

    3. 创建基础iOS 7应用的步骤和遇到常见问题的处理方法。 4. 测试和适配应用在不同设备和iOS版本上的显示效果。 5. 监控应用性能,确保良好的用户体验。 6. 理解iOS 7的系统架构以及常用SDK框架的作用和使用方法。 7....

    iOS 9 App Development Essentials 无水印pdf

    iOS 9 App Development Essentials 英文无水印pdf pdf所有页面使用FoxitReader和PDF-XChangeViewer测试都可以打开 本资源转载自网络,如有侵权,请联系上传者或csdn删除 本资源转载自网络,如有侵权,请联系...

    iOS 11 App Development Essentials Learn to Develop iOS 11 Apps with 无水印转化版pdf

    本书《iOS 11 App Development Essentials》旨在帮助读者掌握开发 iOS 11 应用所需的基本技能。它覆盖了从安装开发环境到构建应用的全过程,并深入介绍了 Swift 4 编程语言的基础知识。对于新接触 iOS 开发的人来说...

    iOS 11 App Development Essentials

    3. 安装Xcode9和iOS 11 SDK 书中强调了区分Mac电脑是基于Intel还是PowerPC架构的重要性,因为这对于安装Xcode和iOS SDK有着直接的影响。书中还介绍了如何安装Xcode9,如何在Xcode偏好设置中添加Apple ID,以及如何...

    iPhone iOS 4 App开发要点iPhone iOS 4 App Development Essentials

    本书概述了iPhone硬件和iOS 4的体系结构。提供了Objective-C编程的简介,然后深入研究了iPhone应用程序和用户界面的设计。

    iOS 8 App开发要点iOS 8 App Development Essentials

    介绍如何使用Xcode和Swift构建适用于iOS 8的应用程序。

    iOS 16 App Development Essentials – UIKit Edition

    本书旨在教授使用 iOS 16 SDK、UIKit、Xcode 14 和 Swift 编程语言创建 iOS 应用程序所需的技能。 本书从基础开始,概述了设置 iOS 开发环境所需的步骤。接下来,介绍了 iOS 16 的架构和 Swift 5.7 中的编程,然后...

    iOS.8.App.Development.Essentials

    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 4 App开发要点-Xcode 4版iPad iOS 4 App development Essentials - Xcode 4 Edition

    ### iPad iOS 4 App开发要点-Xcode 4版 #### 引言 随着移动设备的普及,特别是苹果公司的iPad发布后,iOS平台的应用程序开发变得越来越重要。本篇内容将聚焦于iPad上运行的iOS 4应用程序开发的核心知识点,特别强调...

    iOS.9.App.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 ...

    Ios 9 Game Development Essentials

    总结来说,《iOS 9 Game Development Essentials》这本书覆盖了从基础的 Swift 编程语言到高级的游戏开发技术,适合想要入门 iOS 游戏开发的读者。通过本书的学习,你将能够掌握使用 Swift 和 SpriteKit/SceneKit ...

    Android 4.2 App开发要点Android 4.2 App Development Essentials

    使用Eclipse IDE和Android 4.2 SDK的Android应用程序开发简介。 本书假定您具有Java编程经验。

Global site tag (gtag.js) - Google Analytics