iOS沙盒路径的查看和使用
1、模擬器沙盒目錄 文件都在個(gè)人用戶(hù)名文件夾下的一個(gè)隱藏文件夾里,中文叫資源庫(kù),他的目錄其實(shí)是Library。
因?yàn)閼?yīng)用是在沙箱(sandbox)中的,在文件讀寫(xiě)權(quán)限上受到限制,只能在幾個(gè)目錄下讀寫(xiě)文件:
Documents:應(yīng)用中用戶(hù)數(shù)據(jù)可以放在這里,iTunes備份和恢復(fù)的時(shí)候會(huì)包括此目錄
tmp:存放臨時(shí)文件,iTunes不會(huì)備份和恢復(fù)此目錄,此目錄下文件可能會(huì)在應(yīng)用退出后刪除
Library/Caches:存放緩存文件,iTunes不會(huì)備份此目錄,此目錄下文件不會(huì)在應(yīng)用退出刪除
iTunes在與iPhone同步時(shí),備份所有的Documents和Library文件。 iPhone在重啟時(shí),會(huì)丟棄所有的tmp文件。
查看方法: 方法1、可以設(shè)置顯示隱藏文件,然后在Finder下直接打開(kāi)。設(shè)置查看隱藏文件的方法如下:打開(kāi)終端,輸入命名 (1)顯示Mac隱藏文件的命令:defaults write com.apple.finder AppleShowAllFiles -bool true (2)隱藏Mac隱藏文件的命令:defaults write com.apple.finder AppleShowAllFiles -bool false (3)輸完單擊Enter鍵,退出終端,重新啟動(dòng)Finder就可以了 重啟Finder:鼠標(biāo)單擊窗口左上角的蘋(píng)果標(biāo)志-->強(qiáng)制退出-->Finder--> 現(xiàn)在能看到資源庫(kù)文件夾了。? 打開(kāi)資源庫(kù)后找到/Application Support/iPhone Simulator/文件夾。這里面就是模擬器的各個(gè)程序的沙盒目錄了。 方法2、這種方法更方便,在Finder上點(diǎn)->前往->前往文件夾,輸入/Users/username/Library/Application Support/iPhone Simulator/? 前往。 username這里寫(xiě)用戶(hù)名。?
?
代碼查看目錄:
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);結(jié)果如下:
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-6E9398EFF5DB2013-09-03 20:31:27.210 ios那啥[8383:c07] NSHomeDirectoryForUser:/Users/wmm/Library/Application Support/iPhone Simulator/6.1/Applications/D803DBD2-9CB2-4D18-9152-6E9398EFF5DB2013-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??自定義類(lèi)返回各目錄路徑: #import <Foundation/Foundation.h>@interface ICSandboxHelper : NSObject+ (NSString *)homePath; // 程序主目錄,可見(jiàn)子目錄(3個(gè)):Documents、Library、tmp + (NSString *)appPath; // 程序目錄,不能存任何東西 + (NSString *)docPath; // 文檔目錄,需要ITUNES同步備份的數(shù)據(jù)存這里,可存放用戶(hù)數(shù)據(jù) + (NSString *)libPrefPath; // 配置目錄,配置文件存這里 + (NSString *)libCachePath; // 緩存目錄,系統(tǒng)永遠(yuǎn)不會(huì)刪除這里的文件,ITUNES會(huì)刪除 + (NSString *)tmpPath; // 臨時(shí)緩存目錄,APP退出后,系統(tǒng)可能會(huì)刪除這里的內(nèi)容 + (BOOL)hasLive:(NSString *)path; //判斷目錄是否存在,不存在則創(chuàng)建 #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; }轉(zhuǎn)載于:https://www.cnblogs.com/liubo175175/p/5282836.html
總結(jié)
以上是生活随笔為你收集整理的iOS沙盒路径的查看和使用的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 股票分位点是什么意思
- 下一篇: ios动画效果集锦(持续更新)