简体   繁体   中英

iOS - Check if file exists Dropbox Sync API iOS SDK

I'm new to iOS development, and working on an app that support Dropbox sync for text files.

Having followed tutorial on Dropbox site but I'm not be able to check if a file is exist.

As implemented:

NSString *dropboxFileExtension;
switch ([[NSUserDefaults standardUserDefaults] integerForKey:kFileExtension]) {
    case txt:
        dropboxFileExtension = [NSString stringWithFormat:@"%@.txt", titleString];
        break;
    case md:
        dropboxFileExtension = [NSString stringWithFormat:@"%@.md", titleString];
        break;
    case markdown:
        dropboxFileExtension = [NSString stringWithFormat:@"%@.markdown", titleString];
        break;
    default:
        break;
}

DBPath *newPath = [[DBPath root] childPath:[NSString stringWithFormat:@"%@", dropboxFileExtension]];
DBFile *file = [[DBFilesystem sharedFilesystem] createFile:newPath error:nil];
[file writeString:self.note.contents error:nil];

If I update its contents, this will throws an error that file is exists.

So how can I check that file is exists and then perform appropriate action like overwriting file or updating file. Thank you!

EDIT / Working Solution : Logically, I just have to check whether if file info exists using DBFileInfo class (1). If (1) true -> we call openFile:error before writeString:error , else call createFile:error . As suggested by @rmaddy.

So...

DBPath *newPath = [[DBPath root] childPath:[NSString stringWithFormat:@"%@", dropboxFileExtension]];
DBError *error = nil;
DBFileInfo *info = [[DBFilesystem sharedFilesystem] fileInfoForPath:newPath error:&error];
if (info) {
    // file exists
    NSLog(@"size %lli byte(s), modified dated %@", info.size, info.modifiedTime);
    _file = [[DBFilesystem sharedFilesystem] openFile:newPath
                                                error:nil];
} else {
    _file = [[DBFilesystem sharedFilesystem] createFile:newPath
                                                  error:nil];
}

[_file writeString:self.note.contents error:nil];

Try getting the DBFileInfo for the path:

DBError *error = nil;
DBFileInfo *info = [[DBFileSystem sharedFileSystem] fileInfoForPath:newPath error:&error];
if (info) {
    // file exists
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM