How to check if a file exists in Documents folder?

后端 未结 7 1370
小蘑菇
小蘑菇 2020-11-29 14:53

I have an application with In-App Purchase, that when the user buy something, download one html file into the Documents folder of my app.

Now I must check if this HT

7条回答
  •  無奈伤痛
    2020-11-29 15:32

    Swift 3:

    let documentsURL = try! FileManager().url(for: .documentDirectory,
                                              in: .userDomainMask,
                                              appropriateFor: nil,
                                              create: true)
    

    ... gives you a file URL of the documents directory. The following checks if there's a file named foo.html:

    let fooURL = documentsURL.appendingPathComponent("foo.html")
    let fileExists = FileManager().fileExists(atPath: fooURL.path)
    

    Objective-C:

    NSString* documentsPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
    
    NSString* foofile = [documentsPath stringByAppendingPathComponent:@"foo.html"];
    BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:foofile];
    

提交回复
热议问题