Chinaunix首页 | 论坛 | 博客
  • 博客访问: 306460
  • 博文数量: 53
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 325
  • 用 户 组: 普通用户
  • 注册时间: 2013-10-14 22:50
文章分类

全部博文(53)

文章存档

2014年(15)

2013年(38)

我的朋友

分类: iOS平台

2014-04-05 20:04:46

1.app目录

CCLOG(@"NSBundle:%@",[[NSBundle mainBundle] resourcePath]);

NSBundle:/Users/abc/Library/Application Support/iPhone Simulator/6.0/Applications/02D4F12B-3587-402B-A035-B9443BCCB541/abc.app


 CCLOG(@"respath:%@",respath);

respath:/Users/abc/Library/Application Support/iPhone Simulator/6.0/Applications/02D4F12B-3587-402B-A035-B9443BCCB541/abc.app/abc Resources-hd.plist



2.Documents

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

 CCLOG(@"沙盒路径:%@",paths);


沙盒路径:(

    "/Users/abc/Library/Application Support/iPhone Simulator/6.0/Applications/02D4F12B-3587-402B-A035-B9443BCCB541/Documents"

)

    NSString *pathstr = [paths objectAtIndex:0];

     CCLOG(@"Documents路径:%@",pathstr);

Documents路径:/Users/abc/Library/Application Support/iPhone Simulator/6.0/Applications/02D4F12B-3587-402B-A035-B9443BCCB541/Documents

 NSString *ret=[NSString stringWithFormat:@"%@/%@",pathstr,_filename];

     CCLOG(@"文件路径:%@",ret);

文件路径:/Users/abc/Library/Application Support/iPhone Simulator/6.0/Applications/02D4F12B-3587-402B-A035-B9443BCCB541/Documents/abc Resources-hd.plist

3.文件是否存在

[[NSFileManager defaultManager] fileExistsAtPath:respath]

//以下转载

默认情况下,每个沙盒含有3个文件夹:Documents, Library 和 tmp。

Documents:苹果建议将程序中建立的或在程序中浏览到的文件数据保存在该目录下;
Library:存储程序的默认设置或其它状态信息;
tmp:提供一个即时创建临时文件的地方。

iTunes在与iPhone同步时,备份所有的Documents和Library文件。
iPhone在重启时,会丢弃所有的tmp文件。

 

//取得Documents路径的方法:  

- (NSString *)documentFolder {  

   return [NSHomeDirectory()stringByAppendingPathComponent:@"Documents"];  

}  


//取得Documents中某个文件的路径  

NSString *path = [[self documentFolder] stringByAppendingPathComponent:@"image.png"]; 


//获取tmp目录

NSString *tempPath = NSTemporaryDirectory();


//补充:取得应用程序包(即bundle)的路径  

- (NSString *)bundleFolder {  

   return [[NSBundlemainBundle]bundlePath];  

}



一、沙盒(sandbox)
出于安全的目的,应用程序只能将自己的数据和偏好设置写入到几个特定的位置上。当应用程序被安装到设备上时,系统会为其创建一个家目录,这个家目录就是应用程序的沙盒。

家目录下共有四个子目录:

Documents 目录:您应该将所有的应用程序数据文件写入到这个目录下。这个目录用于存储用户数据或其它应该定期备份的信息。
AppName.app 目录:这是应用程序的程序包目录,包含应用程序的本身。由于应用程序必须经过签名,所以您在运行时不能对这个目录中的内容进行修改,否则可能会使应用程序无法启动。
Library 目录:这个目录下有两个子目录:Caches 和 Preferences
    Preferences 目录包含应用程序的偏好设置文件。您不应该直接创建偏好设置文件,而是应该使用NSUserDefaults类来取得和设置应用程序的偏好
    Caches 目录用于存放应用程序专用的支持文件,保存应用程序再次启动过程中需要的信息。
tmp 目录:这个目录用于存放临时文件,保存应用程序再次启动过程中不需要的信息。

获取这些目录路径的方法:

//1,获取家目录路径的函数:

NSString *homeDir = NSHomeDirectory(); 


//2,获取Documents目录路径的方法:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);

NSString *docDir = [paths objectAtIndex:0];


//3,获取Caches目录路径的方法:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask,YES);

NSString *cachesDir = [paths objectAtIndex:0];


//4,获取tmp目录路径的方法:

NSString *tmpDir = NSTemporaryDirectory();


//5,获取应用程序程序包中资源文件路径的方法:

//例如获取程序包中一个图片资源(apple.png)路径的方法:

NSString *imagePath = [[NSBundlemainBundle]pathForResource:@"apple"ofType:@"png"];

UIImage *appleImage = [[UIImagealloc]initWithContentsOfFile:imagePath];

//代码中的mainBundle类方法用于返回一个代表应用程序包的对象。




二、文件IO

1,将数据写到Documents目录:


- (BOOL)writeApplicationData:(NSData*)data toFile:(NSString*)fileName {

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);

    NSString *docDir = [paths objectAtIndex:0];

    if(!docDir) {

       NSLog(@"Documents directory not found!");        

        return NO;    

    }    

    NSString *filePath = [docDir stringByAppendingPathComponent:fileName];

    return [data writeToFile:filePath atomically:YES];

}



2,从Documents目录读取数据:

- (NSData *)applicationDataFromFile:(NSString *)fileName {

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);

    NSString *docDir = [paths objectAtIndex:0];

    NSString *filePath = [docDir stringByAppendingPathComponent:fileName];

    NSData *data = [[[NSData alloc]initWithContentsOfFile:filePath]autorelease];

    return data;

}

//userplist写入沙盒


    //获取沙盒中的路径

    NSArray *storeFilePath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMaskYES);

    NSString *doucumentsDirectiory = [storeFilePath objectAtIndex:0];

    NSString *plistPath =[doucumentsDirectiory stringByAppendingPathComponent:@"user.plist"];

    NSFileManager *file = [NSFileManager defaultManager];

    if ([file fileExistsAtPath:plistPath])

    {

        NSLog(@"you");

    }

    else //若沙盒中没有

    {

        NSError *error;

        NSFileManager *fileManager = [NSFileManager defaultManager];

        NSString *bundle = [[NSBundle mainBundlepathForResource:@"user" ofType:@"plist"];

        [fileManager copyItemAtPath:bundle toPath:plistPath error:&error];

        

        NSLog(@"写入没有%d",[fileManager copyItemAtPath:bundle toPath:plistPath error:&error]);

    }

阅读(579) | 评论(0) | 转发(0) |
0

上一篇:ios 图片压缩

下一篇:没有了

给主人留下些什么吧!~~