1.獲取沙盒路徑:? NSString *home = NSHomeDirectory();
2.plist文件存儲(chǔ):
Plist注意:不能存儲(chǔ)自定義對(duì)象
NSString *docPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
簡(jiǎn)潔版plist存儲(chǔ):
- (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);
}
復(fù)雜版plist:
//存入
- (IBAction)save:(id)sender {??
? // 獲取路徑:NSSearchPath 尋找路徑耘沼,F(xiàn)orDirectories 要找的文件夾塌衰,InDomains 在哪個(gè)地方找? ?
?// NSDocumentDirectory Documents? ??
NSString *docPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];? ??
// 拼接路徑? ? NSString *filePath = [docPath stringByAppendingPathComponent:@"xxxx.plist"];? ?
?//? ? NSArray *arr = @[@"老馬",@"唐帥"];? ??
// 存儲(chǔ)? ??
//? ? [arr writeToFile:filePath atomically:YES];??
? // 創(chuàng)建字典? ? NSDictionary *dict = @{@"name":@"laoma",@"age":@"18"};? ??
//? ? 存儲(chǔ)字典? ? [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"];//? ? // 讀取數(shù)組//? ? 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.系統(tǒng)偏好設(shè)置存儲(chǔ):
// 好處:
1.不需要關(guān)心文件名
// 2.快速做鍵值對(duì)存儲(chǔ)
// 底層:就是封裝了一個(gè)字典
//存入
- (IBAction)save:(id)sender {
?// 取得ud
//? ? NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
? // 設(shè)置//? ? [ud setObject:@"laoma" forKey:@"name"];
//? ? [ud setInteger:18 forKey:@"age"];
//? ? [ud setBool:YES forKey:@"isOn"];
//? ? [ud synchronize];
//同步 強(qiáng)制寫(xiě)入? !!!一定要寫(xiě)}
//讀取
- (IBAction)read:(id)sender {
? // 獲取ud//? ? NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
}
4寸宵、歸檔解檔存儲(chǔ)
// 解析文件都會(huì)調(diào)用這個(gè)方法惧财,注意xib和story文件的加載用的就是這種方法進(jìn)行的初始化:
- (id)initWithCoder:(NSCoder *)aDecoder
{
// 只要父類遵守了NSCoding,就調(diào)用initWithCoder
// 先初始化父類
if (self = [super initWithCoder:aDecoder]) {
NSLog(@"%s",__func__);
}
return self;
}
而代碼初始化的底層調(diào)用:
// 通過(guò)代碼初始化的時(shí)候恤磷,調(diào)用init方法紊册,底層就會(huì)調(diào)用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// 告訴系統(tǒng)歸檔哪些屬性- (void)encodeWithCoder:(NSCoder *)aCoder {? ?
?[aCoder encodeObject:_name forKey:@"name"];? ?
?[aCoder encodeInteger:_age forKey:@"age"];}
// 告訴系統(tǒng)解檔哪些屬性,如何解檔
- (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 {
// 創(chuàng)建對(duì)象
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、鑰匙串存儲(chǔ):{
#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.
//要保存的數(shù)據(jù)
//? ? NSString *username = @"zhangsan";
//? ? NSString *password = @"lisi";
//存儲(chǔ)到鑰匙串
//pasetPassword 相當(dāng)于要存的值, account 相當(dāng)于key
//forService 標(biāo)識(shí)
//? ? [SSKeychain setPassword:username forService:[NSBundle mainBundle].bundleIdentifier account:@"username"];
//從鑰匙串取數(shù)據(jù)
// 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燃逻、證書(shū)存儲(chǔ):
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];
//運(yùn)營(yíng)商的名字
NSLog(@"%@",services.allSystemInformation);
}