iOS沙盒路徑的查看和使用

iOS沙盒路徑的查看和使用

1、模擬器沙盒目錄
文件都在個人用戶名文件夾下的一個隱藏文件夾裏,中文叫資源庫,他的目錄其實是Library。

因爲應用是在沙箱(sandbox)中的,在文件讀寫權限上受到限制,只能在幾個目錄下讀寫文件:
Documents:應用中用戶數據可以放在這裏,iTunes備份和恢復的時候會包括此目錄
tmp:存放臨時文件,iTunes不會備份和恢復此目錄,此目錄下文件可能會在應用退出後刪除
Library/Caches:存放緩存文件,iTunes不會備份此目錄,此目錄下文件不會在應用退出刪除

iTunes在與iPhone同步時,備份所有的Documents和Library文件。
iPhone在重啓時,會丟棄所有的tmp文件。


查看方法:
方法1、可以設置顯示隱藏文件,然後在Finder下直接打開。設置查看隱藏文件的方法如下:打開終端,輸入命名
(1)顯示Mac隱藏文件的命令:defaults write com.apple.finder AppleShowAllFiles -bool true
(2)隱藏Mac隱藏文件的命令:defaults write com.apple.finder AppleShowAllFiles -bool false
(3)輸完單擊Enter鍵,退出終端,重新啓動Finder就可以了 重啓Finder:鼠標單擊窗口左上角的蘋果標誌-->強制退出-->Finder-->
現在能看到資源庫文件夾了。 
打開資源庫後找到/Application Support/iPhone Simulator/文件夾。這裏面就是模擬器的各個程序的沙盒目錄了。
方法2、這種方法更方便,在Finder上點->前往->前往文件夾,輸入/Users/username/Library/Application Support/iPhone Simulator/  前往。
username這裏寫用戶名。 

 

代碼查看目錄:

複製代碼
    NSString *path = NSHomeDirectory();//主目錄
    NSLog(@"NSHomeDirectory:%@",path);
    NSString *userName = NSUserName();//與上面相同
    NSString *rootPath = NSHomeDirectoryForUser(userName);
    NSLog(@"NSHomeDirectoryForUser:%@",rootPath);
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory=[paths objectAtIndex:0];//Documents目錄
    NSLog(@"NSDocumentDirectory:%@",documentsDirectory);
複製代碼

結果如下:

2013-09-03 20:31:27.210 ios那啥[8383:c07] NSHomeDirectory:/Users/wmm/Library/Application Support/iPhone Simulator/6.1/Applications/D803DBD2-9CB2-4D18-9152-6E9398EFF5DB
2013-09-03 20:31:27.210 ios那啥[8383:c07] NSHomeDirectoryForUser:/Users/wmm/Library/Application Support/iPhone Simulator/6.1/Applications/D803DBD2-9CB2-4D18-9152-6E9398EFF5DB
2013-09-03 20:31:27.211 ios那啥[8383:c07] NSDocumentDirectory:/Users/wmm/Library/Application Support/iPhone Simulator/6.1/Applications/D803DBD2-9CB2-4D18-9152-6E9398EFF5DB/Documents
 
 
自定義類返回各目錄路徑:
複製代碼
#import <Foundation/Foundation.h>

@interface ICSandboxHelper : NSObject

+ (NSString *)homePath;     // 程序主目錄,可見子目錄(3個):Documents、Library、tmp
+ (NSString *)appPath;        // 程序目錄,不能存任何東西
+ (NSString *)docPath;        // 文檔目錄,需要ITUNES同步備份的數據存這裏,可存放用戶數據
+ (NSString *)libPrefPath;    // 配置目錄,配置文件存這裏
+ (NSString *)libCachePath;    // 緩存目錄,系統永遠不會刪除這裏的文件,ITUNES會刪除
+ (NSString *)tmpPath;        // 臨時緩存目錄,APP退出後,系統可能會刪除這裏的內容
+ (BOOL)hasLive:(NSString *)path; //判斷目錄是否存在,不存在則創建
複製代碼
複製代碼
#import "ICSandboxHelper.h"

@implementation ICSandboxHelper

+ (NSString *)homePath{
    return NSHomeDirectory();
}

+ (NSString *)appPath
{
    NSArray * paths = NSSearchPathForDirectoriesInDomains(NSApplicationDirectory, NSUserDomainMask, YES);
    return [paths objectAtIndex:0];
}

+ (NSString *)docPath
{
    NSArray * paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    return [paths objectAtIndex:0];
}

+ (NSString *)libPrefPath
{
    NSArray * paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
    return [[paths objectAtIndex:0] stringByAppendingFormat:@"/Preference"];
}

+ (NSString *)libCachePath
{
    NSArray * paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
    return [[paths objectAtIndex:0] stringByAppendingFormat:@"/Caches"];
}

+ (NSString *)tmpPath
{return [NSHomeDirectory() stringByAppendingFormat:@"/tmp"];
}

+ (BOOL)hasLive:(NSString *)path
{
    if ( NO == [[NSFileManager defaultManager] fileExistsAtPath:path] )
    {
        return [[NSFileManager defaultManager] createDirectoryAtPath:path
                                         withIntermediateDirectories:YES
                                                          attributes:nil
                                                               error:NULL];
    }
    
    return NO;
}
複製代碼

 


發佈了62 篇原創文章 · 獲贊 3 · 訪問量 28萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章