ios - 如何判斷資料夾下某個檔案是否存在,用fileExistsAtPath:總是NO,檔案是存在的
曾经蜡笔没有小新
曾经蜡笔没有小新 2017-05-24 11:31:54
0
3
1528

1。應用間資源共享,分享來的文檔存在沙盒的Documents/Inbox路徑下:
(lldb) po localPath
/var/mobile/Containers/Data/Application/151DF2B1-576C-42B5- 8F5D-051132FF2494/Documents/Inbox/?? Microsoft Word ?? (2)-1.docx

這個文件是存在的,但是用我想計算文檔大小,用以下方法

NSFileManager *fileManager = [NSFileManager defaultManager];
    if ([fileManager fileExistsAtPath:localPath]) {
        size = [[fileManager attributesOfItemAtPath:localPath error:nil] fileSize];
    }

總是回傳NO,檔案不存在,想問下這條路徑是不是有問題,不能這麼判斷?
若路徑改為/var/mobile/Containers/Data/Application/151DF2B1-576C-42B5-8F5D-051132FF2494/Documents/Inbox ,就能偵測到有檔案

#線上等~~~
可能問題描述不是很清楚,有時間的可以看下這個http://blog.csdn.net/zhonggaorong/article/details/51832089,我做的就是讓第三方app點擊“用其他應用程式開啟”,底部彈出的控制台裡能有自己的app,然後會跳到自己app內,回調openUrl:那個方法

曾经蜡笔没有小新
曾经蜡笔没有小新

全部回覆(3)
Ty80

Application/ 後面那一串,應用每次運行都會變的,你不應該把這個路徑分享出去,而且一個應用也不能訪問到另一個應用的文件目錄。

根據iOS App讓自己的應用程式在其他應用程式中開啟清單中顯示、iOS把自己的應用程式加入」活動「、將PDF檔案Open In MyApp

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
    if (url != nil) {
        NSFileManager *fileManager = [NSFileManager defaultManager];
        if ([fileManager fileExistsAtPath:url.path]) {
            unsigned long long size = [[fileManager attributesOfItemAtPath:url.path error:nil] fileSize];
            NSLog(@"size:%llu", size);
        }
        NSData *data = [NSData dataWithContentsOfURL:url];
        UIImage *image = [UIImage imageWithData:data];
        NSLog(@"%@", image);
    }
    
    return YES;
}
某草草
  1. 路徑中的字串是每次安裝應用時,系統分配的,只要不被卸載,這個路徑是不變的
    2.我懷疑是路徑名含非法字符導致的問題

3.DOCUMENT目錄不是sandbox,就是為分享和雲端備份,還有iTunes客戶端用的

淡淡烟草味

iOS的app都是沙盒,只能存取自己app下的目錄。你的app這一串151DF2B1-576C-42B5-8F5D-051132FF2494是會改變的。所以要透過相對路徑獲取。參考碼如下:

NSArray *pathArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
// 这个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];
}
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!