分享來(lái)的文檔存在沙盒的Documents/Inbox路徑下:
/var/mobile/Containers/Data/Application/151DF2B1-576C-42B5-8F5D-051132FF2494/Documents/Inbox/%3F%3F%20Microsoft%20Word%20%3F%3F%20(2)-1.docx這個(gè)文件
這個(gè)文件是存在的,但是用我想計(jì)算文檔大小,用如下方法
NSFileManager *fileManager = [NSFileManager defaultManager];
? ? if([fileManagerfileExistsAtPath:localPath]) {
? ? ? ? size = [[fileManagerattributesOfItemAtPath:localPath error:nil] fileSize];
? ? }總是返回NO,文件不存在
Application/ 后面那一串,應(yīng)用每次運(yùn)行都會(huì)變的..........................................................
------解決辦法
iOS的app都是沙盒,只能訪問(wèn)自己app下的目錄。你的app這一串151DF2B1-576C-42B5-8F5D-051132FF2494是會(huì)發(fā)生變化的。所以要通過(guò)相對(duì)路徑獲取。參考代碼如下:
獲取Documents目錄
NSArray *pathArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
// 這個(gè)documentDirectory就是你的/var/mobile/Containers/Data/Application/151DF2B1-576C-42B5-8F5D-051132FF2494/Documents/目錄了
NSString *documentDirectory = [pathArray firstObject];
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *localPath = [documentDirectory stringByAppendingPathComponent:@"/Inbox/%3F%3F%20Microsoft%20Word%20%3F%3F%20(2)-1.docx"];
if([fileManager fileExistsAtPath:localPath]) {
? ? size = [[fileManager attributesOfItemAtPath:localPath error:nil] fileSize];
}
----------------------------------------------------------------------------------------------------------
//目錄結(jié)構(gòu):
------AppName.app
------Documents
------Library
??????----------Caches
??????----------Preferences
------tmp
------
Documents:?用于存儲(chǔ)用戶數(shù)據(jù)或其它應(yīng)該定期備份的信息
Preferences?:?應(yīng)用程序的偏好設(shè)置文件
Caches?:?保存應(yīng)用程序再次啟動(dòng)過(guò)程中需要的信息。您的應(yīng)用程序通常需要負(fù)責(zé)添加和刪除這些文件,
但在對(duì)設(shè)備進(jìn)行完全恢復(fù)的過(guò)程中,iTunes會(huì)刪除這些文件
tmp?:?存放臨時(shí)文件,保存應(yīng)用程序再次啟動(dòng)過(guò)程中不需要的信息
獲取各個(gè) 路徑
-----------Documents :?
NSString*?documentsDirectory?=?[NSHomeDirectory()?stringByAppendingPathComponent:@"Documents"];
????NSLog(@"documentsDirectory----%@\n\n",documentsDirectory);
或者NSArray?*paths?=?NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,?YES);NSString?*documentsDirectory1?=?[paths?objectAtIndex:0];
----------Library?
NSString*?LibraryDirectory?=?[NSHomeDirectory()?stringByAppendingPathComponent:@"Library"];
或者NSArray?*paths?=?NSSearchPathForDirectoriesInDomains(NSLibraryDirectory,NSUserDomainMask,?YES);
?NSString?*LibraryDirectory1?=?[paths?objectAtIndex:0];
-----------Cache
NSArray?*paths?=?NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask,?YES);NSString?*documentsDirectory1?=?[paths?objectAtIndex:0];
----------test.app
NSString?*str?=?[[NSBundle?mainBundle]?resourcePath];
????NSLog(@"str---%@\n\n",str);
?///你所導(dǎo)入的文件就放在這里,文章開(kāi)頭已經(jīng)說(shuō)了如何獲取其路徑
----------獲取 根目錄
NSHomeDirectory();