IOS沙盒Files目录说明和常用操作

Posted 阳光1787354782

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了IOS沙盒Files目录说明和常用操作相关的知识,希望对你有一定的参考价值。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

 

 

    // 读取Documents目录代码

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

    NSString *pathDocuments=[pathsDocuments objectAtIndex:0];

//    NSLog(@"pathDocuments~~~~~~%@    pathsDocuments----%@",pathDocuments,pathsDocuments);

  /*

   pathDocuments~~~~~~/Users/yhj/Library/Developer/CoreSimulator/Devices/65FC33FE-6234-427B-A4F2-4F22ACE36842/data/Containers/Data/Application/12EA0817-D2C0-40E5-B3CC-1EB642FCDF9F/Documents    pathsDocuments----(

   "/Users/yhj/Library/Developer/CoreSimulator/Devices/65FC33FE-6234-427B-A4F2-4F22ACE36842/data/Containers/Data/Application/12EA0817-D2C0-40E5-B3CC-1EB642FCDF9F/Documents"

   )

   */

 

 

   // 读取Cache目录代码

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

    NSString *pathCache=[pathsCache objectAtIndex:0];

//    NSLog(@"pathCache~~~~~~%@    pathsCache----%@",pathsCache,pathCache);

    /*

     pathCache~~~~~~(

     "/Users/yhj/Library/Developer/CoreSimulator/Devices/65FC33FE-6234-427B-A4F2-4F22ACE36842/data/Containers/Data/Application/823489C8-00DA-403B-95AB-76C8FBE8AAAA/Library/Caches"

     )    pathsCache----/Users/yhj/Library/Developer/CoreSimulator/Devices/65FC33FE-6234-427B-A4F2-4F22ACE36842/data/Containers/Data/Application/823489C8-00DA-403B-95AB-76C8FBE8AAAA/Library/Caches

     */

 

 

    // 读取Library目录

    NSArray *pathsLibrary=NSSearchPathForDirectoriesInDomains(NSLibraryDirectory,NSUserDomainMask, YES);

    NSString *pathLibrary=[pathsLibrary objectAtIndex:0];

//    NSLog(@"pathsLibrary~~~~~~%@    pathLibrary----%@",pathsLibrary,pathLibrary);

    /*

     pathsLibrary~~~~~~(

     "/Users/yhj/Library/Developer/CoreSimulator/Devices/65FC33FE-6234-427B-A4F2-4F22ACE36842/data/Containers/Data/Application/05969A2D-E6BD-4F4B-A57E-4414D987D240/Library"

     )    pathLibrary----/Users/yhj/Library/Developer/CoreSimulator/Devices/65FC33FE-6234-427B-A4F2-4F22ACE36842/data/Containers/Data/Application/05969A2D-E6BD-4F4B-A57E-4414D987D240/Library

 

     */

 

     // 读取temp文件夹

    NSString *tempDir=NSTemporaryDirectory();

//    NSLog(@"tempDir~~~%@",tempDir);

   /*

    tempDir~~~/Users/yhj/Library/Developer/CoreSimulator/Devices/65FC33FE-6234-427B-A4F2-4F22ACE36842/data/Containers/Data/Application/CEF70855-6285-46D9-B7BF-B4C9AAB1816B/tmp/

 

    */

 

    // 项目自带的.bundle资源库放在.bundle中这些资源只读不能写

    NSString *defaultDBPath=[[NSBundle mainBundle] resourcePath];

    NSLog(@"defaultDBPath~~%@",defaultDBPath);

  /*

   defaultDBPath~~/Users/yhj/Library/Developer/CoreSimulator/Devices/65FC33FE-6234-427B-A4F2-4F22ACE36842/data/Containers/Bundle/Application/D94DD7F5-0CE9-4EEF-8E47-37C4737EF2CF/ios沙盒Files目录说明和常用操作.app

   */

 

 

 

    // 可以保证appdocuments文件内容不备份到icloud

    [self addSkipBackupAttributeToItemAtURL:[NSURL fileURLWithPath:pathDocuments]];

    return YES;

}

 

// NSURLIsExcludedFromBackupKey kCFURLIsExcludedFromBackupKey 文件属性来防止文件被备份。这些API是通过通过旧的,弃用的方式的直接设置额外属性。所有运行在iOS5.1的都应该使用这些API包防止文件被备份。在iOS5 .1上防止文件被备份

-(BOOL)addSkipBackupAttributeToItemAtURL:(NSURL *)URL

{

    assert([[NSFileManager defaultManager] fileExistsAtPath:[URL path]]);

    NSError *error=nil;

    BOOL success=[URL setResourceValue:[NSNumber numberWithBool:YES] forKey:NSURLIsExcludedFromBackupKey error:&error];

    if (!success) {

        NSLog(@"Error excluding %@ from backup %@",[URL lastPathComponent],error);

    }

    return success;

}

以上是关于IOS沙盒Files目录说明和常用操作的主要内容,如果未能解决你的问题,请参考以下文章

iOS学习之iOS沙盒(sandbox)机制和文件操作

iOS学习之iOS沙盒(sandbox)机制和文件操作1

IOS开发-UI学习-沙盒机制&文件操作

iOS开发之--沙盒的操作

iOS开发之获取沙盒路径

iOS边练边学--应用数据存储的常用方式(plist,Preference,NSKeyedArchiver)其中的三种