ios - How to determine whether a file in a folder exists, use fileExistsAtPath: always NO, the file exists
曾经蜡笔没有小新
曾经蜡笔没有小新 2017-05-24 11:31:54
0
3
1564

1. Resource sharing between applications, the shared documents are stored in the Documents/Inbox path of the sandbox:
(lldb) po localPath
/var/mobile/Containers/Data/Application/151DF2B1-576C-42B5- 8F5D-051132FF2494/Documents/Inbox/?? Microsoft Word ?? (2)-1.docx

This file exists, but I want to calculate the document size using the following method

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

Always returns NO, the file does not exist. I would like to ask if there is a problem with this path. Can't I judge it this way?
If the path is changed to /var/mobile/Containers/Data/Application/151DF2B1-576C-42B5-8F5D-051132FF2494/Documents/Inbox, the file will be detected

Waiting online~~~
Maybe the description of the problem is not very clear. If you have time, you can read this http://blog.csdn.net/zhonggaorong/article/details/51832089. What I do is let the third party When the app clicks "Open with other applications", the console that pops up at the bottom can have its own app, and then it will jump to its own app and call back openUrl: that method

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

reply all(3)
Ty80

The string after

Application/ will change every time the application is run. You should not share this path, and one application cannot access the file directory of another application.

According to iOS App, let your application be displayed in the list of other applications. iOS can add your application to "Activities" and open PDF files 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. The string in the path is assigned by the system every time the application is installed. As long as it is not uninstalled, this path will remain unchanged
    2. I suspect that the problem is caused by the path name containing illegal characters

3. The DOCUMENT directory is not a sandbox, but is used for sharing, cloud backup, and iTunes client

淡淡烟草味

iOS apps are all sandboxed and can only access the directory under your own app. The string 151DF2B1-576C-42B5-8F5D-051132FF2494 in your app will change. So it needs to be obtained through relative paths. The reference code is as follows:

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];
}
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template