本地保存数据(轻量级)

本地保存数据(轻量级)

这里提供两种方案:
1, 通过AppDelegate保存为全局变量,再获取
2,使用NSUSerDefault

第一种 :通过AppDelegate方法:

定义全局变量

#import 
 

@interface AppDelegate : UIResponder 


@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) NSString *globalAppThemeColor;
@property (strong, nonatomic) NSString *globalAboutTag;
 
@end
 
在AppDelegate.m 内赋值:
_globalAppThemeColor = appDetails.appThemeColor;
_globalAboutTag = appDetails.about;
 
在需要的VC头部导入
#import "AppDelegate.h"
 
- (void)viewDidLoad {
    [super viewDidLoad];
    
   //创建 
  AppDelegate * appDelegate=(AppDelegate*)[[UIApplication sharedApplication]delegate];
 
}  
 
获得变量
NSString *about = appDelegate.globalAboutTag;
NSString *theme = appDelegate.globalAppThemeColor;
灵活运用到代码需求的地方
//navi设置为全局主题色
self.navigationController.navigationBar.barTintColor = [UIColor colorWithHexString:appDelegate.globalAppThemeColor alpha:1];

第二种 :通过NSUserDefaults方法:

一 ,NSUserDefaults 简单的运用方法
NSUserDefaults一般可以存取一些短小的信息,比如存入再读出一个字符串到NSUserDefaults

注意 : key值必须要相同才能读取出来哦!
 NSUserDefaults只支持: NSString, NSNumber, NSDate, NSArray, NSDictionary,  不是所有数据都能往里放滴哦~
//存储数据

NSString *string = [NSString stringWithString @"我想存储的字符串内容"];     
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];     
[userDefaults setObject:string forKey:@"theme"]; 
//在存储数据的地方,别忘了这一句 
[[NSUserDefaults standardUserDefaults] synchronize];


//在需要的地方获取数据
NSString *getStringValue  = [[NSUserDefaults standardUserDefaults] objectForKey:@"theme"];

二 , 如果需要保存比较多得数据, 可以通过模型保存和读取

  1. 模型代码

#import 

@interface AreasModel : NSObject

 

/*
 * about 模型
 */
@property (nonatomic,copy)NSString * about;
@property (nonatomic,copy)NSString * appThemeColor;

 
@end

 
记住必须要在 M 文件 里 写这两个方法
- (id) initWithCoder: (NSCoder *)coder
- (void) encodeWithCoder: (NSCoder *)coder
然后把该自定义的类对象编码到 NSData中,再从NSUserDefaults中进行读取。
 
//
//  AreasModel.m
////
//  Created by MISSAJJ on 15/5/7.
//  Copyright (c) 2015年 MISSAJJ. All rights reserved.
//

#import "AreasModel.h"

@implementation AreasModel


- (id)initWithCoder:(NSCoder *)aDecoder
{
    if(self = [super init])
    {
        self.about       = [aDecoder decodeObjectForKey:@"about"];
        self.appThemeColor           = [aDecoder decodeObjectForKey:@"appThemeColor"];
        
    }
    return self;
}

- (void)encodeWithCoder:(NSCoder *)aCoder
{
    [aCoder encodeObject:self.about forKey:@"about"];
    [aCoder encodeObject:self.appThemeColor forKey:@"appThemeColor"];
  
}

@end

  1. 存储数据的代码
//////////////////////////   
  以上省略...
//////////////////////////

//URL编码成UTF8
   dirPath = [dirPath stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
   NSURL * dirUrl = [NSURL URLWithString:dirPath];
   NSMutableURLRequest * dirRequest = [NSMutableURLRequest requestWithURL:dirUrl];
   NSData *dirJsonData = [NSURLConnection sendSynchronousRequest:dirRequest returningResponse:nil error:nil];
   NSDictionary *dirListJsonData = [NSJSONSerialization JSONObjectWithData:dirJsonData options:0 error:nil];
   NSDictionary* dicData = [dirListJsonData objectForKey:@"data"];
    
  #pragma mark ====保存临时主题和关于信息====
   NSString *about = [dicData objectForKey:@"about"];
   NSString *theme = [dicData objectForKey:@"theme"];
   
   //创建模型
   AreasModel *themeAndAbout = [[AreasModel alloc] init];
   themeAndAbout.about = about;
   themeAndAbout.appThemeColor = theme;
  
   //保存数据,用归档保存到NSUserDefault
   NSData *themeAndAboutData = [NSKeyedArchiver archivedDataWithRootObject:themeAndAbout];
   [[NSUserDefaults standardUserDefaults] setObject:themeAndAboutData forKey:@"themeAndAbout"];
   [[NSUserDefaults standardUserDefaults] synchronize];

  1. 获取数据代码
//获得保存数据
   NSData *getthemeAndAboutData = [[NSUserDefaults standardUserDefaults] objectForKey:@"themeAndAbout"];
   
   //转成模型获取数据
  AreasModel *getThemeAndAbout =   [NSKeyedUnarchiver unarchiveObjectWithData:getthemeAndAboutData];
   
   NSLog(@"%@,%@",getThemeAndAbout.appThemeColor, getThemeAndAbout.about);
   
   ```

你可能感兴趣的:(本地保存数据(轻量级))