【MISSAJJ原创】AppDelegate存储全局变量和 NSUserDefaults通过模型保存和读取数据,存储自定义的对象

由于app开发的需求,需要从api接口获得json格式数据并保存临时的 app的主题颜色 和 相关url

 

方案有很多种:

1, 通过AppDelegate保存为全局变量,再获取

2,使用NSUSerDefault

 

第一种 :通过AppDelegate方法:

 

定义全局变量

//
//  AppDelegate.h
//
//  Created by MISSAJJ on 15/5/5.
//  Copyright (c) 2015年 MISSAJJ. All rights reserved.
//

#import <UIKit/UIKit.h>
 

@interface AppDelegate : UIResponder <UIApplicationDelegate >


@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];

 

 ====== I am 华丽丽的分割线 ^_^=======

 

 

第二种 :通过NSUserDefaults方法:

查找了相关资料,自己整理了一份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.模型代码

 

//
//  AreasModel.h
////
//  Created by MISSAJJ on 15/5/7.
//  Copyright (c) 2015年 MISSAJJ. All rights reserved.
//

#import <Foundation/Foundation.h>

@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

 

 

2. 存储数据的代码

 

//////////////////////////   
   以上省略........
//////////////////////////

 //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];

 

3. 获取数据代码 

 

//获得保存数据
    NSData *getthemeAndAboutData = [[NSUserDefaults standardUserDefaults] objectForKey:@"themeAndAbout"];
 

//转成模型获取数据
   AreasModel *getThemeAndAbout =   [NSKeyedUnarchiver unarchiveObjectWithData:getthemeAndAboutData];
    
    NSLog(@"%@,%@",getThemeAndAbout.appThemeColor, getThemeAndAbout.about);

 

 

 ====心灵和身体,总有一个在路上===
 ====只要还在路上,就不至于后退===