关于ios存储方式的汇总(原创):

1.获取沙盒路径:  NSString *home = NSHomeDirectory();

2.plist文件存储:

Plist注意:不能存储自定义对象

NSString *docPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];

简洁版plist存储:

- (IBAction)save:(id)sender {

// 获取docpath

NSString *docpath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];

// 拼接路径

NSString *filepath = [docpath stringByAppendingPathComponent:@"x.plist"];

NSString *str = @"chuanzhi";

[str writeToFile:filepath atomically:YES encoding:NSUTF8StringEncoding error:nil];

}

- (IBAction)read:(id)sender {

// 获取docpath

NSString *docpath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];

// 拼接路径

NSString *filepath = [docpath stringByAppendingPathComponent:@"x.plist"];

//  获取字符串

NSString *str = [NSString stringWithContentsOfFile:filepath encoding:NSUTF8StringEncoding error:nil];

NSLog(@"%@",str);

}

复杂版plist:

//存入

- (IBAction)save:(id)sender {  

  // 获取路径:NSSearchPath 寻找路径,ForDirectories 要找的文件夹,InDomains 在哪个地方找   

 // NSDocumentDirectory Documents    

NSString *docPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];    

// 拼接路径    NSString *filePath = [docPath stringByAppendingPathComponent:@"xxxx.plist"];   

 //    NSArray *arr = @[@"老马",@"唐帅"];    

// 存储    

//    [arr writeToFile:filePath atomically:YES];  

  // 创建字典    NSDictionary *dict = @{@"name":@"laoma",@"age":@"18"};    

//    存储字典    [dict writeToFile:filePath atomically:YES];}

// 读取- (IBAction)read:(id)sender {//  

  // 获取沙盒路径//    NSString *home = NSHomeDirectory();//   

 // 拼接doc 路径//    NSString *docPath = [home stringByAppendingString:@"/Documents"];//    

// 拼接plist路径//    NSString *filePath = [docPath stringByAppendingString:@"/xx.plist"];//    // 读取数组//    NSArray *arr = [NSArray arrayWithContentsOfFile:filePath];  

  // 获取doc路径    NSString *docPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];   

 // 拼接plist路径    NSString *filePath = [docPath stringByAppendingPathComponent:@"xxxx.plist"];    

NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:filePath];    NSLog(@"%@",dict[@"name"]);}

3.系统偏好设置存储:

// 好处:

1.不需要关心文件名

// 2.快速做键值对存储

// 底层:就是封装了一个字典

//存入

- (IBAction)save:(id)sender {

 // 取得ud

//    NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];

  // 设置//    [ud setObject:@"laoma" forKey:@"name"];

//    [ud setInteger:18 forKey:@"age"];

//    [ud setBool:YES forKey:@"isOn"];

//    [ud synchronize];

//同步 强制写入  !!!一定要写}

//读取

- (IBAction)read:(id)sender {

  // 获取ud//    NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];

}

4、归档解档存储

// 解析文件都会调用这个方法,注意xib和story文件的加载用的就是这种方法进行的初始化:

- (id)initWithCoder:(NSCoder *)aDecoder

{

// 只要父类遵守了NSCoding,就调用initWithCoder

// 先初始化父类

if (self = [super initWithCoder:aDecoder]) {

NSLog(@"%s",__func__);

}

return self;

}

而代码初始化的底层调用:

// 通过代码初始化的时候,调用init方法,底层就会调用initWithFrame

- (instancetype)initWithFrame:(CGRect)frame

{

if (self = [super initWithFrame:frame]) {

NSLog(@"%s",__func__);

}

return self;

}

@end

{#import

@interface Person : NSObject@property (nonatomic, copy) NSString *name;@property (nonatomic ,assign) NSInteger age;@end

#import "Person.h"@implementation Person// 告诉系统归档哪些属性- (void)encodeWithCoder:(NSCoder *)aCoder {   

 [aCoder encodeObject:_name forKey:@"name"];   

 [aCoder encodeInteger:_age forKey:@"age"];}

// 告诉系统解档哪些属性,如何解档

- (instancetype)initWithCoder:(NSCoder *)aDecoder 

{    if (self = [super init]) {       

 _name = [aDecoder decodeObjectForKey:@"name"];     

   _age = [aDecoder decodeIntegerForKey:@"age"];   

 }    

return self;

}@end

#import@interface ViewController : UIViewController

@end

#import "ViewController.h"

#import "Person.h"

@interface ViewController ()

@end

@implementation ViewController

// 归档

- (IBAction)save:(id)sender {

// 创建对象

Person *p = [[Person alloc] init];

p.name = @"zhangsan";

p.age = 18;

// docpath

NSString *docPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];

//  拼接路径

NSString *fliePath = [docPath stringByAppendingPathComponent:@"person.data"];

// 归档

[NSKeyedArchiver archiveRootObject:p toFile:fliePath];

}

// 解档

- (IBAction)read:(id)sender {

// docpath

NSString *docPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];

//  拼接路径

NSString *fliePath = [docPath stringByAppendingPathComponent:@"person.data"];

// 解档

Person *p = [NSKeyedUnarchiver unarchiveObjectWithFile:fliePath];

NSLog(@"%@",p.name);

}

- (void)viewDidLoad {

[super viewDidLoad];

// Do any additional setup after loading the view, typically from a nib.

}

}

5、钥匙串存储:{

#import "ViewController.h"

#import "SSKeychain.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {

[super viewDidLoad];

// Do any additional setup after loading the view, typically from a nib.

//要保存的数据

//    NSString *username = @"zhangsan";

//    NSString *password = @"lisi";

//存储到钥匙串

//pasetPassword 相当于要存的值, account 相当于key

//forService 标识

//    [SSKeychain setPassword:username forService:[NSBundle mainBundle].bundleIdentifier account:@"username"];

//从钥匙串取数据

// NSString *username = [SSKeychain passwordForService:[NSBundle mainBundle].bundleIdentifier account:@"username"];

// NSLog(@"%@",username);

NSString * username = @"zhangsan";

NSString * password = @"lisi";

[SSKeychain setPassword:username forService:[NSBundle mainBundle].bundleIdentifier account:@"username"];

NSString * username = [SSKeychain passwordForService:[NSBundle mainBundle].bundleIdentifier account:@"username"];

}

}

6、证书存储:

NSURL *url = [NSURL URLWithString:@"http://www.example.com"];

self.protectionSpace = [[NSURLProtectionSpace alloc] initWithHost:url.host

port:[url.port integerValue]

protocol:url.scheme

realm:nil

authenticationMethod:NSURLAuthenticationMethodHTTPDigest];

// 存

- (IBAction)save:(id)sender {

NSURLCredential *credential;

credential = [NSURLCredential credentialWithUser:@"houyuan" password:@"315213" persistence:NSURLCredentialPersistencePermanent];

[[NSURLCredentialStorage sharedCredentialStorage] setCredential:credential forProtectionSpace:self.protectionSpace];

}

// 取

- (IBAction)read:(id)sender {

NSURLCredential *credential;

NSDictionary *credentials;

credentials = [[NSURLCredentialStorage sharedCredentialStorage] credentialsForProtectionSpace:self.protectionSpace];

NSLog(@"User %@ already connected with password %@", credential.user, credential.password);

}

7、读取硬件信息:

//  ViewController.m

#import "ViewController.h"

#import "SystemServices.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {

[super viewDidLoad];

//获取硬件信息

SystemServices *services = [SystemServices sharedServices];

//运营商的名字

NSLog(@"%@",services.allSystemInformation);

}

你可能感兴趣的:(关于ios存储方式的汇总(原创):)