使用Xcode创建plist文件和使用

  1.  //读取plist  

  2.   

  3.     NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"plistdemo" ofType:@"plist"];  

  4.     NSMutableDictionary *data = [[NSMutableDictionary alloc] initWithContentsOfFile:plistPath];  

  5.     NSLog(@"%@", data);  

  6.       

  7.     //添加一项内容  

  8.     [data setObject:@"add some content" forKey:@"c_key"];  

  9.       

  10.     //获取应用程序沙盒的Documents目录  

  11.     NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);  

  12.     NSString *plistPath1 = [paths objectAtIndex:0];  

  13.       

  14.     //得到完整的文件名  

  15.     NSString *filename=[plistPath1 stringByAppendingPathComponent:@"test.plist"];  

  16.    //输入写入  

  17.     [data writeToFile:filename atomically:YES];  

  18.       

  19.     //那怎么证明我的数据写入了呢?读出来看看  

  20.     NSMutableDictionary *data1 = [[NSMutableDictionary alloc] initWithContentsOfFile:filename];  

  21.     NSLog(@"%@", data1);  


使用Xcode自带的创建pilst文件功能,创建ErrorCode.plist

 如图:

  

 

 2.在项目中使用plist文件

  NSString *strErrorCodePath = [[NSBundle mainBundle]pathForResource:@"ErrorCode"    ofType:@"plist"];

 

  NSDictionary *dictErrorCode = [NSDictionary dictionaryWithContentsOfURL:[NSURLfileURLWithPath:strErrorCodePath]];

 

  NSDictionary *dictErrorPromp = [dictErrorCode objectForKey:@"ErrorPromp"];

 

 if (![self.msgCode isEqualToString:@""] && self.msgCode != nil) {

  NSString *strPromp = [dictErrorPromp objectForKey:self.msgCode];

    if (![strPromp isEqualToString:@""] && strPromp != nil) {

     [LoadingView showLoadingView:strPromp];

  }

  }

 

创建 PS_CONFIG.plist 文件,截图如下:

将PS_CONFIG.plist的使用方法创建一个单独的类处理

PSConfig.h文件

#import <Foundation/Foundation.h>

 

@interface PSConfig : NSObject {

 

}

+ (NSString*)getConfigFilePath;

+ (NSString*)getDocumentConfigFilePath;

+ (void)setDeviceTokenValue:(NSString *)value;

+ (NSString *)getDeviceTokenValue;

+ (void)setIsFirstInstallValue:(BOOL)value;

+ (BOOL)getIsFirstInstallValue;

+ (void)setIsLookedCheckOrderGuideValue:(BOOL)value;

+ (BOOL)getIsLookedCheckOrderGuideValue;

+ (void)setIsLookedCheckOrderGuide2Value:(BOOL)value;

+ (BOOL)getIsLookedCheckOrderGuide2Value;

+ (void)setIsLookedUserAddressGuideValue:(BOOL)value;

+ (BOOL)getIsLookedUserAddressGuideValue;

+ (void)setLastUpdateTimeValue:(NSString *)value;

+ (NSString *)getLastUpdateTimeValue;

@end

 

PSConfig.m文件

 

#import "PSConfig.h"

 

#define kConfigFileName @"PS_CONFIG"

#define kConfigFileType @"plist"

 

@implementation PSConfig

 

// 原始文件目录

+ (NSString*)getConfigFilePath{

 

NSString *configFilePath = [[NSBundlemainBundle]pathForResource:kConfigFileNameofType:kConfigFileType];

return configFilePath;

}

 

// 用来作更改的目录

+ (NSString*)getDocumentConfigFilePath{

 

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMaskYES);

NSString *documentsDirectory = [paths objectAtIndex:0];

NSString *path = [documentsDirectorystringByAppendingPathComponent:@"PS_CONFIG.plist"];

NSFileManager *fileManager = [NSFileManagerdefaultManager];

BOOL isExist = [fileManager fileExistsAtPath:path];

if (!isExist) {

NSString *configFilePath = [self getConfigFilePath];

NSDictionary *configDic = [NSDictionarydictionaryWithContentsOfFile:configFilePath];

[configDic writeToFile:path atomically:YES];

}

return path;

}

 

+ (void)setDeviceTokenValue:(NSString *)value{

 

NSString *configFilePath = [self getConfigFilePath];

NSString *path = [selfgetDocumentConfigFilePath];

 

NSFileManager *fileManager = [NSFileManagerdefaultManager];

BOOL isExist = [fileManager fileExistsAtPath:path];

 

NSError *error = nil;

if (isExist == NO) {

 

NSDictionary *configDic = [NSDictionarydictionaryWithContentsOfFile:configFilePath];

NSMutableDictionary *configMutabDic = [NSMutableDictionarydictionaryWithDictionary:configDic];

[configMutabDic setValue:value forKey:@"DeviceToken"];

[configMutabDic writeToFile:path atomically:YES];

}else {

NSDictionary *configDic = [NSDictionarydictionaryWithContentsOfFile:path];

BOOL isRemoved = [fileManager removeItemAtPath:path error:&error];

if (isRemoved) {

NSMutableDictionary *configMutabDic = [NSMutableDictionarydictionaryWithDictionary:configDic];

[configMutabDic setValue:value forKey:@"DeviceToken"];

[configMutabDic writeToFile:path atomically:YES];

}else {

NSLog(@"remove error %@\n",error);

}

}

}

 

+ (NSString *)getDeviceTokenValue{

 

NSString *path = [selfgetDocumentConfigFilePath];

NSFileManager *fileManager = [NSFileManagerdefaultManager];

BOOL isExist = [fileManager fileExistsAtPath:path];

 

if (isExist == YES) {

NSDictionary *configDic = [NSDictionarydictionaryWithContentsOfFile:path];

NSString *deviceToken = (NSString *)[configDic objectForKey:@"DeviceToken"];

return deviceToken;

}

return@"";

 

}

 

+ (void)setIsFirstInstallValue:(BOOL)value{

 

NSString *configFilePath = [self getConfigFilePath];

NSString *path = [selfgetDocumentConfigFilePath];

 

NSFileManager *fileManager = [NSFileManagerdefaultManager];

BOOL isExist = [fileManager fileExistsAtPath:path];

 

NSError *error = nil;

if (isExist == NO) {

 

NSDictionary *configDic = [NSDictionarydictionaryWithContentsOfFile:configFilePath];

NSMutableDictionary *configMutabDic = [NSMutableDictionarydictionaryWithDictionary:configDic];

[configMutabDic setValue:[NSNumber numberWithBool:value]forKey:@"IsFirstInstall"];

[configMutabDic writeToFile:path atomically:YES];

}else {

NSDictionary *configDic = [NSDictionarydictionaryWithContentsOfFile:path];

BOOL isRemoved = [fileManager removeItemAtPath:path error:&error];

if (isRemoved) {

NSMutableDictionary *configMutabDic = [NSMutableDictionarydictionaryWithDictionary:configDic];

[configMutabDic setValue:[NSNumber numberWithBool:value]forKey:@"IsFirstInstall"];

[configMutabDic writeToFile:path atomically:YES];

}else {

NSLog(@"remove error %@\n",error);

}

}

 

}

 

+ (BOOL)getIsFirstInstallValue{

 

NSString *path = [selfgetDocumentConfigFilePath];

NSFileManager *fileManager = [NSFileManagerdefaultManager];

BOOL isExist = [fileManager fileExistsAtPath:path];

 

if (isExist == YES) {

NSDictionary *configDic = [NSDictionarydictionaryWithContentsOfFile:path];

BOOL isFirstInstall = [[configDic objectForKey:@"IsFirstInstall"boolValue];

return isFirstInstall;

}

returnYES;

}

 

+ (void)setIsLookedCheckOrderGuideValue:(BOOL)value{

 

NSString *configFilePath = [self getConfigFilePath];

NSString *path = [selfgetDocumentConfigFilePath];

 

NSFileManager *fileManager = [NSFileManagerdefaultManager];

BOOL isExist = [fileManager fileExistsAtPath:path];

 

NSError *error = nil;

if (isExist == NO) {

 

NSDictionary *configDic = [NSDictionarydictionaryWithContentsOfFile:configFilePath];

NSMutableDictionary *configMutabDic = [NSMutableDictionarydictionaryWithDictionary:configDic];

[configMutabDic setValue:[NSNumber numberWithBool:value]forKey:@"isLookedCheckOrderGuide"];

[configMutabDic writeToFile:path atomically:YES];

 

}else {

NSDictionary *configDic = [NSDictionarydictionaryWithContentsOfFile:path];

BOOL isRemoved = [fileManager removeItemAtPath:path error:&error];

if (isRemoved) {

NSMutableDictionary *configMutabDic = [NSMutableDictionarydictionaryWithDictionary:configDic];

[configMutabDic setValue:[NSNumber numberWithBool:value]forKey:@"isLookedCheckOrderGuide"];

[configMutabDic writeToFile:path atomically:YES];

}else {

NSLog(@"remove error %@\n",error);

}

}

 

}

 

+ (BOOL)getIsLookedCheckOrderGuideValue{

 

NSString *path = [selfgetDocumentConfigFilePath];

NSFileManager *fileManager = [NSFileManagerdefaultManager];

BOOL isExist = [fileManager fileExistsAtPath:path];

 

if (isExist == YES) {

NSDictionary *configDic = [NSDictionarydictionaryWithContentsOfFile:path];

BOOL isLookedCheckOrderGuide = [[configDicobjectForKey:@"isLookedCheckOrderGuide"boolValue];

return isLookedCheckOrderGuide;

}

returnNO;

}

+ (void)setIsLookedCheckOrderGuide2Value:(BOOL)value{

 

NSString *configFilePath = [self getConfigFilePath];

NSString *path = [selfgetDocumentConfigFilePath];

 

NSFileManager *fileManager = [NSFileManagerdefaultManager];

BOOL isExist = [fileManager fileExistsAtPath:path];

 

NSError *error = nil;

if (isExist == NO) {

 

NSDictionary *configDic = [NSDictionarydictionaryWithContentsOfFile:configFilePath];

NSMutableDictionary *configMutabDic = [NSMutableDictionarydictionaryWithDictionary:configDic];

[configMutabDic setValue:[NSNumber numberWithBool:value]forKey:@"isLookedCheckOrderGuide2"];

[configMutabDic writeToFile:path atomically:YES];

 

}else {

NSDictionary *configDic = [NSDictionarydictionaryWithContentsOfFile:path];

BOOL isRemoved = [fileManager removeItemAtPath:path error:&error];

if (isRemoved) {

NSMutableDictionary *configMutabDic = [NSMutableDictionarydictionaryWithDictionary:configDic];

[configMutabDic setValue:[NSNumber numberWithBool:value]forKey:@"isLookedCheckOrderGuide2"];

[configMutabDic writeToFile:path atomically:YES];

}else {

NSLog(@"remove error %@\n",error);

}

}

 

}

 

+ (BOOL)getIsLookedCheckOrderGuide2Value{

 

NSString *path = [selfgetDocumentConfigFilePath];

NSFileManager *fileManager = [NSFileManagerdefaultManager];

BOOL isExist = [fileManager fileExistsAtPath:path];

 

if (isExist == YES) {

NSDictionary *configDic = [NSDictionarydictionaryWithContentsOfFile:path];

BOOL isLookedCheckOrderGuide2 = [[configDicobjectForKey:@"isLookedCheckOrderGuide2"boolValue];

return isLookedCheckOrderGuide2;

}

returnNO;

}

 

+ (void)setIsLookedUserAddressGuideValue:(BOOL)value{

 

NSString *configFilePath = [self getConfigFilePath];

NSString *path = [selfgetDocumentConfigFilePath];

 

NSFileManager *fileManager = [NSFileManagerdefaultManager];

BOOL isExist = [fileManager fileExistsAtPath:path];

 

NSError *error = nil;

if (isExist == NO) {

 

NSDictionary *configDic = [NSDictionarydictionaryWithContentsOfFile:configFilePath];

NSMutableDictionary *configMutabDic = [NSMutableDictionarydictionaryWithDictionary:configDic];

[configMutabDic setValue:[NSNumber numberWithBool:value]forKey:@"isLookedUserAddressGuide"];

[configMutabDic writeToFile:path atomically:YES];

 

}else {

NSDictionary *configDic = [NSDictionarydictionaryWithContentsOfFile:path];

BOOL isRemoved = [fileManager removeItemAtPath:path error:&error];

if (isRemoved) {

NSMutableDictionary *configMutabDic = [NSMutableDictionarydictionaryWithDictionary:configDic];

[configMutabDic setValue:[NSNumber numberWithBool:value]forKey:@"isLookedUserAddressGuide"];

[configMutabDic writeToFile:path atomically:YES];

}else {

NSLog(@"remove error %@\n",error);

}

}

 

}

 

+ (BOOL)getIsLookedUserAddressGuideValue{

 

NSString *path = [selfgetDocumentConfigFilePath];

NSFileManager *fileManager = [NSFileManagerdefaultManager];

BOOL isExist = [fileManager fileExistsAtPath:path];

 

if (isExist == YES) {

NSDictionary *configDic = [NSDictionarydictionaryWithContentsOfFile:path];

BOOL isLookedUserAddressGuide = [[configDicobjectForKey:@"isLookedUserAddressGuide"boolValue];

return isLookedUserAddressGuide;

}

returnNO;

}

 

+ (void)setLastUpdateTimeValue:(NSString *)value{

 

if (value == nil || [value isEqualToString:@""]) {

return ;

}

 

NSString *configFilePath = [self getConfigFilePath];

NSString *path = [selfgetDocumentConfigFilePath];

 

NSFileManager *fileManager = [NSFileManagerdefaultManager];

BOOL isExist = [fileManager fileExistsAtPath:path];

 

NSError *error = nil;

if (isExist == NO) {

 

NSDictionary *configDic = [NSDictionarydictionaryWithContentsOfFile:configFilePath];

NSMutableDictionary *configMutabDic = [NSMutableDictionarydictionaryWithDictionary:configDic];

[configMutabDic setValue:value forKey:@"LastUpdateTime"];

[configMutabDic writeToFile:path atomically:YES];

}else {

NSDictionary *configDic = [NSDictionarydictionaryWithContentsOfFile:path];

BOOL isRemoved = [fileManager removeItemAtPath:path error:&error];

if (isRemoved) {

NSMutableDictionary *configMutabDic = [NSMutableDictionarydictionaryWithDictionary:configDic];

[configMutabDic setValue:value forKey:@"LastUpdateTime"];

[configMutabDic writeToFile:path atomically:YES];

}else {

NSLog(@"remove error %@\n",error);

}

}

 

}

 

+ (NSString *)getLastUpdateTimeValue{

 

NSString *path = [selfgetDocumentConfigFilePath];

NSFileManager *fileManager = [NSFileManagerdefaultManager];

BOOL isExist = [fileManager fileExistsAtPath:path];

 

if (isExist == YES) {

NSDictionary *configDic = [NSDictionarydictionaryWithContentsOfFile:path];

NSString *lastUpdateTime = (NSString *)[configDicobjectForKey:@"LastUpdateTime"];

return lastUpdateTime;

}

return@"";

 

}

@end

 

// 获取原始文件目录

+ (NSString*)getConfigFilePath{

 

NSString *configFilePath = [[NSBundlemainBundle]pathForResource:kConfigFileNameofType:kConfigFileType];

return configFilePath;

}

 

// 用来作更改的目录

+ (NSString*)getDocumentConfigFilePath{

 

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMaskYES);

NSString *documentsDirectory = [paths objectAtIndex:0];

NSString *path = [documentsDirectorystringByAppendingPathComponent:@"PS_CONFIG.plist"];

NSFileManager *fileManager = [NSFileManagerdefaultManager];

BOOL isExist = [fileManager fileExistsAtPath:path];

if (!isExist) {

NSString *configFilePath = [self getConfigFilePath];

NSDictionary *configDic = [NSDictionarydictionaryWithContentsOfFile:configFilePath];

[configDic writeToFile:path atomically:YES];

}

return path;

}

 通过PSConfig类来操作plist文件

if (self.personInformationOk && [PSConfiggetIsLookedCheckOrderGuideValue] ==NO) {

self.help_bgview = [[[UIImageViewalloc]initWithImage:[UIImageimageNamed:@"help_bg.png"]]autorelease];

self.help_bgview.frame = CGRectMake(00320750); 

[self.bgScrollViewaddSubview:help_bgview];

self.help_newcustomer2view = [[[UIImageViewalloc]initWithImage:[UIImageimageNamed:@"help_newcustomer2.png"]]autorelease];

self.help_newcustomer2view.frame = CGRectMake(-5, -60320480); 

self.help_newcustomer2view.userInteractionEnabled = YES;

UITapGestureRecognizer *singleTouch=[[UITapGestureRecognizeralloc]initWithTarget:selfaction:@selector(MakehelpViewUnDisappear)];

[self.help_newcustomer2viewaddGestureRecognizer:singleTouch];

[singleTouch release];

[self.bgScrollViewaddSubview:self.help_newcustomer2view];

self.ifneedHelp=!self.ifneedHelp;

}

if (self.personInformationOk == 0 && [PSConfiggetIsLookedCheckOrderGuide2Value] == NO) {

self.help_bgview = [[[UIImageViewalloc]initWithImage:[UIImageimageNamed:@"help_bg.png"]]autorelease];

self.help_bgview.frame = CGRectMake(00320750); 

[self.bgScrollViewaddSubview:help_bgview];

self.help_newcustomer1view = [[[UIImageViewalloc]initWithImage:[UIImageimageNamed:@"help_newcustomer1.png"]]autorelease];

self.help_newcustomer1view.frame = CGRectMake(-5, -68320750); 

self.help_newcustomer1view.userInteractionEnabled = YES;

UITapGestureRecognizer *singleTouch=[[UITapGestureRecognizeralloc]initWithTarget:selfaction:@selector(MakehelpViewUnDisappear2)];

[self.help_newcustomer1viewaddGestureRecognizer:singleTouch];

[singleTouch release];

[self.bgScrollViewaddSubview:self.help_newcustomer1view];

self.ifneedHelp =! self.ifneedHelp;

}

 

- (void)MakehelpViewUnDisappear {

[PSConfigsetIsLookedCheckOrderGuideValue:YES];

self.ifneedHelp=!self.ifneedHelp;

[self.help_newcustomer2viewsetHidden:YES];

[self.help_newcustomer1viewsetHidden:YES];

[self.help_bgviewsetHidden:YES];

[self.help_bgviewremoveFromSuperview];

[self.help_newcustomer1viewremoveFromSuperview];

[self.help_newcustomer2viewremoveFromSuperview];

}

- (void)MakehelpViewUnDisappear2 {

[PSConfigsetIsLookedCheckOrderGuide2Value:YES];

self.ifneedHelp=!self.ifneedHelp;

[self.help_newcustomer2viewsetHidden:YES];

[self.help_newcustomer1viewsetHidden:YES];

[self.help_bgviewsetHidden:YES];

[self.help_bgviewremoveFromSuperview];

[self.help_newcustomer1viewremoveFromSuperview];

[self.help_newcustomer2viewremoveFromSuperview];

}

- (void)TakehelpViewUnDisappear {

[PSConfigsetIsLookedCheckOrderGuideValue:YES];

[self.help_newcustomer2viewsetHidden:YES];

[self.help_newcustomer1viewsetHidden:YES];

[self.help_bgviewsetHidden:YES];

[self.help_bgviewremoveFromSuperview];

[self.help_newcustomer1viewremoveFromSuperview];

[self.help_newcustomer2viewremoveFromSuperview];

}

- (void)TakehelpViewUnDisappear2 {

[PSConfigsetIsLookedCheckOrderGuide2Value:YES];

[self.help_newcustomer2viewsetHidden:YES];

[self.help_newcustomer1viewsetHidden:YES];

[self.help_bgviewsetHidden:YES];

[self.help_bgviewremoveFromSuperview];

[self.help_newcustomer1viewremoveFromSuperview];

[self.help_newcustomer2viewremoveFromSuperview];

}

 

- (void)applicationWillResignActive:(UIApplication *)application {

    /*

     Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.

     Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.

     */

NSInteger iconBadgeNumber = [[UIApplicationsharedApplication]applicationIconBadgeNumber];

if (iconBadgeNumber != 0) {

[[UIApplicationsharedApplicationsetApplicationIconBadgeNumber:0];

}

 

BOOL isFirst = [PSConfiggetIsFirstInstallValue];

if (isFirst) {

[PSConfigsetIsFirstInstallValue:NO];

}

 

}

你可能感兴趣的:(xcode,plist)