How do I verify a file's existence in iCloud?

前端 未结 3 1195
半阙折子戏
半阙折子戏 2021-01-16 20:13

I know that the file exists, because I can download it, but I need to check to see whether it exists. I have tried using

[NSFileManager contentsOfDirectory         


        
3条回答
  •  逝去的感伤
    2021-01-16 20:56

    To find files in iCloud, you use NSMetadataQuery. That will find both files that have already been downloaded as well as files that are in the user's account but which haven't been downloaded to the local device yet. Using NSFileManager will, at best, only find files that have already been downloaded.

    You set it up with something like this:

    NSMetadataQuery *query = [[NSMetadataQuery alloc] init];
    [self setMetadataQuery:query];
    [query setSearchScopes:@[NSMetadataQueryUbiquitousDataScope]];
    [query setPredicate:[NSPredicate predicateWithFormat:@"%K LIKE '*'", NSMetadataItemFSNameKey]];
    

    You'll want to observe NSMetadataQueryDidStartGatheringNotification, NSMetadataQueryDidUpdateNotification, and probably NSMetadataQueryDidFinishGatheringNotification. Then start the query:

    [query startQuery];
    

    With that done, you'll get notifications as the query discovers iCloud files. The notifications will include instances of NSMetadataItem, which you can use to get information like file size, download status, etc.

提交回复
热议问题