本地保存數(shù)據(jù)(輕量級(jí))
這里提供兩種方案:
1, 通過AppDelegate保存為全局變量,再獲取
2,使用NSUSerDefault
第一種 :通過AppDelegate方法:
定義全局變量
#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 內(nèi)賦值:
_globalAppThemeColor = appDetails.appThemeColor;
_globalAboutTag = appDetails.about;
在需要的VC頭部導(dǎo)入
#import "AppDelegate.h"
- (void)viewDidLoad {
[super viewDidLoad];
//創(chuàng)建
AppDelegate * appDelegate=(AppDelegate*)[[UIApplication sharedApplication]delegate];
}
獲得變量
NSString *about = appDelegate.globalAboutTag;
NSString *theme = appDelegate.globalAppThemeColor;
靈活運(yùn)用到代碼需求的地方
//navi設(shè)置為全局主題色
self.navigationController.navigationBar.barTintColor = [UIColor colorWithHexString:appDelegate.globalAppThemeColor alpha:1];
第二種 :通過NSUserDefaults方法:
一 ,NSUserDefaults 簡(jiǎn)單的運(yùn)用方法
NSUserDefaults一般可以存取一些短小的信息根暑,比如存入再讀出一個(gè)字符串到NSUserDefaults
注意 : key值必須要相同才能讀取出來(lái)哦!
NSUserDefaults只支持: NSString, NSNumber, NSDate, NSArray, NSDictionary, 不是所有數(shù)據(jù)都能往里放滴哦~
//存儲(chǔ)數(shù)據(jù)
NSString *string = [NSString stringWithString @"我想存儲(chǔ)的字符串內(nèi)容"];
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setObject:string forKey:@"theme"];
//在存儲(chǔ)數(shù)據(jù)的地方,別忘了這一句
[[NSUserDefaults standardUserDefaults] synchronize];
//在需要的地方獲取數(shù)據(jù)
NSString *getStringValue = [[NSUserDefaults standardUserDefaults] objectForKey:@"theme"];
二 , 如果需要保存比較多得數(shù)據(jù), 可以通過模型保存和讀取
- 模型代碼
#import <Foundation/Foundation.h>
@interface AreasModel : NSObject
/*
* about 模型
*/
@property (nonatomic,copy)NSString * about;
@property (nonatomic,copy)NSString * appThemeColor;
@end
記住必須要在 M 文件 里 寫這兩個(gè)方法
- (id) initWithCoder: (NSCoder *)coder
- (void) encodeWithCoder: (NSCoder *)coder
然后把該自定義的類對(duì)象編碼到 NSData中融师,再?gòu)腘SUserDefaults中進(jìn)行讀取。
//
// 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
- 存儲(chǔ)數(shù)據(jù)的代碼
//////////////////////////
以上省略...
//////////////////////////
//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 ====保存臨時(shí)主題和關(guān)于信息====
NSString *about = [dicData objectForKey:@"about"];
NSString *theme = [dicData objectForKey:@"theme"];
//創(chuàng)建模型
AreasModel *themeAndAbout = [[AreasModel alloc] init];
themeAndAbout.about = about;
themeAndAbout.appThemeColor = theme;
//保存數(shù)據(jù),用歸檔保存到NSUserDefault
NSData *themeAndAboutData = [NSKeyedArchiver archivedDataWithRootObject:themeAndAbout];
[[NSUserDefaults standardUserDefaults] setObject:themeAndAboutData forKey:@"themeAndAbout"];
[[NSUserDefaults standardUserDefaults] synchronize];
- 獲取數(shù)據(jù)代碼
//獲得保存數(shù)據(jù)
NSData *getthemeAndAboutData = [[NSUserDefaults standardUserDefaults] objectForKey:@"themeAndAbout"];
//轉(zhuǎn)成模型獲取數(shù)據(jù)
AreasModel *getThemeAndAbout = [NSKeyedUnarchiver unarchiveObjectWithData:getthemeAndAboutData];
NSLog(@"%@,%@",getThemeAndAbout.appThemeColor, getThemeAndAbout.about);
```