- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
//直接获取start这个key对应的值,如果获取到说明程序已经启动过了,如果没有获取到说明是第一次启动
NSString *result = [[NSUserDefaults standardUserDefaults] objectForKey:@"start"];
if ([result isEqualToString: @"启动过了"]) {
//不是第一次启动
NSLog(@"不是第一次");
} else {
//是第一次启动
NSLog(@"第一次");
[[NSUserDefaults standardUserDefaults] setObject:@"启动过了" forKey:@"start"];
}
RootViewController *root =[[RootViewController alloc] init];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController: root];
nav.navigationBar.barTintColor = [UIColor purpleColor];
nav.navigationBar.translucent = NO;
self.window.rootViewController = nav;
return YES;
}
//*******************************************************************************************//
#import <UIKit/UIKit.h>
@interface RootViewController : UIViewController
@end
//*****************************************************************************************//
#import "RootViewController.h"
@interface RootViewController ()
@end
@implementation RootViewController
- (void)viewDidLoad {
[super viewDidLoad];
//获取沙盒的根路径
NSLog(@"%@",NSHomeDirectory());
//获取documents文件夹路径
NSString *douPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSLog(@"%@",douPath);
//获取缓存文件夹路径
NSString *cachePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
NSLog(@"%@",cachePath);
//获取临时文件夹路径
NSString *tmpPath = NSTemporaryDirectory();
NSLog(@"%@",tmpPath);
//1、NSUserDefaults
[[NSUserDefaults standardUserDefaults] setObject:@"看着快播流着泪" forKey:@"kuaibo"];
//读
NSString *result = [[NSUserDefaults standardUserDefaults] objectForKey:@"kuaibo"];
NSLog(@"%@",result);
//借助NSUserDefaults实现判断程序是不是第一次启动
//在docouments文件夹里创建一个金瓶梅.txt文件,并写入一句话
//1、创建文件的路径
NSString *filePath = [douPath stringByAppendingPathComponent:@"金瓶梅.txt"];
//2、创建文件管理者
NSFileManager *manager = [NSFileManager defaultManager];
//判断文件是否存在,如果存在写入,如果不存在先创建文件
if ([manager fileExistsAtPath: filePath]) {
//文件存在
} else {
//文件不存在
[manager createFileAtPath:filePath contents:nil attributes:nil];
}
//写入内容
NSString *content = @"这是一个新的章节";
[content writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:nil];
//读出来
NSString *readResult = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error: nil];
NSLog(@"%@",readResult);
//文件的复制(在documents文件夹里面复制一份金瓶梅.txt)
NSString *copyFilePath = [douPath stringByAppendingPathComponent:@"金瓶梅复制版.txt"];
[manager copyItemAtPath:filePath toPath:copyFilePath error: nil];
//文件的移动(将金瓶梅.txt移动到Library文件夹中)
//获取Library文件夹路径
NSString *libraryPath = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES)lastObject];
NSString *moveFilePath = [libraryPath stringByAppendingPathComponent: @"金瓶梅.txt"];
[manager moveItemAtPath:filePath toPath:moveFilePath error:nil];
// 文件的删除(将金瓶梅复制版.txt删除)
if ([manager fileExistsAtPath: copyFilePath]) {
// 删除
[manager removeItemAtPath:copyFilePath error: nil];
}
//查看文件夹下的所有文件
NSArray *array = [manager contentsOfDirectoryAtPath:douPath error: nil];
NSLog(@"%ld",[array count]);
NSLog(@"%@",array);
//总结
//对文件的操作实际上是对文件路径的操作,需要借助文件管理者NSFileManager
//读写文件只能是NSString,NSArray,NSDictionary,NSDtata,自定义类是无法直接写入文件的
//目前开发过程中主流的文件处理NSUserDefaults,数据库,coredata
//要把一个自定义的类写入文件,需要借助归档操作,读出来是反归档
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end