How to get URL for application's document directory iPhone

前端 未结 4 1168
迷失自我
迷失自我 2020-12-05 04:09

This code is a part of Core Data. The URLsForDirectory....method does not run on iOS < 4 so I need to know some other methods/objects to call. I would also appriciate doc

相关标签:
4条回答
  • 2020-12-05 04:36

    An alternative solution would be to use the -[NSFileManager URLsForDirectory:inDomains:] method and fetching the path from the returned NSArray:

    Objective-C:

    NSArray *paths = [[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask];
    NSURL *documentsURL = [paths lastObject];
    

    Swift:

    let paths = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)
    let documentsURL = paths[0] as! NSURL
    

    This is supported in iOS 4.0+

    0 讨论(0)
  • 2020-12-05 04:43

    Swift 3

    let documentsUrl = FileManager.default.urls(for: .documentDirectory, in:.userDomainMask).first!
    
    0 讨论(0)
  • 2020-12-05 04:46

    You can get the document path via the NSSearchPathForDirectoriesInDomains foundation function as follows:

    NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentPath = [searchPaths objectAtIndex:0];
    

    This will work on iOS 2+.

    0 讨论(0)
  • 2020-12-05 05:03

    Here is the Swift 2.2 version:

    let paths = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)
    let filePath = paths[0].URLByAppendingPathComponent("filename")
    
    0 讨论(0)
提交回复
热议问题