iOS 数据持久化解决方案总结(二)

           —— 使用Property lists 保存数据 

1. Proerty lists 简介

Property lists(属性列表,保存的文件格式为plist)使用几种特定的数据对象将数据组织为键值对或值列表的形式。文件以xml的格式存取,这就提供了高效地以有意义的结构,可传输,可存储,可访问的产生数据的形式。 Property lists 的存储方式在ios和mac os X中经常使用。

2. Property list 中的数据类型和数据的表示方式 

Abstract type

XML element

Cocoa class

Core Foundation type

array

<array>

NSArray

CFArray (CFArrayRef)

dictionary

<dict>

NSDictionary

CFDictionary (CFDictionaryRef)

string

<string>

NSString

CFString (CFStringRef)

data

<data>

NSData

CFData (CFDataRef)

date

<date>

NSDate

CFDate (CFDateRef)

number - integer

<integer>

NSNumber (intValue)

CFNumber (CFNumberRef, integer value)

number - floating point

<real>

NSNumber (floatValue)

CFNumber (CFNumberRef, floating-point value)

Boolean

<true/> or<false/>

NSNumber (boolValue == YES or boolValue== NO)

CFBoolean (CFBooleanRef ; kCFBooleanTrue orkCFBooleanFalse)

 

3. 一个简单的用plist数据保持的例子

现在有一个App,要通过输入的参数来连接webservice,输入查询SQL语句然后返回结果集。如图

 

如果不保持每次输入的参数,每次做一个简单的查询都要输入这么多个配置字段,相当的麻烦。

解决方案:

每次退出程序自动保存输入到plist,再次激活程序再从plist中读回上次的输入。

 

代码:

  • 宏定义保存文件名,方便使用
#define kFilename       @"inputData.plist"
  • 获取路径的方法:
-(NSString *)dataFilePath{

    NSArray *path=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

    NSString *documentsDirectory=[path objectAtIndex:0];

    return [documentsDirectory stringByAppendingPathComponent:kFilename];

}
  • 先在viewDidLoad中注册监听UIApplicationWillResignActiveNotification 通知:
UIApplication *app=[UIApplicationsharedApplication];

[[NSNotificationCenterdefaultCenter] addObserver:selfselector:@selector(applicationWillResignActive:) name:UIApplicationWillResignActiveNotificationobject:app];
  • 程序转为不活动时调用方法保存数据:
-(void)applicationWillResignActive:(NSNotification *)notification

{

    NSMutableArray *array=[[NSMutableArrayalloc]init];

    [array addObject:txtWebService.text];

    [array addObject:txtIntance.text];

    [array addObject:txtDatabase.text];

    [array addObject:txtUserName.text];

    [array addObject:txtPassword.text];

    [array addObject:txtQuery.text];

    [array writeToFile:[selfdataFilePath] atomically:YES];

}
  • 启动程序时加载plist中数据,呈现到控件中
if ([[NSFileManagerdefaultManager]fileExistsAtPath:filePath]) {

        NSArray *array=[[NSArrayalloc]initWithContentsOfFile:filePath];

        txtWebService.text=[array objectAtIndex:0];

        txtIntance.text=[array objectAtIndex:1];

        txtDatabase.text=[array objectAtIndex:2];

        txtUserName.text=[array objectAtIndex:3];

        txtPassword.text=[array objectAtIndex:4];

        txtQuery.text=[array objectAtIndex:5];

    }
  • 最后得到的plist的内容大概是这个样子
<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">

<plist version="1.0">

<array>

<string>http://202.80.***.***/********</string>

<string>***.***.***.***</string>

<string>MobileGPLS</string>

<string>sa</string>

<string>*********</string>

<string>select * from mvehicle where uid='demo'</string>

</array>

</plist>

敏感数据用*代

 

 

posted @ 2012-09-26 17:44  bohan  阅读(301)  评论(0编辑  收藏  举报