Knowledge Drilling
Knowledge Drilling

Reputation: 1036

How can I find the path of iOS app internal directory in Swift?

How can I find the path of iOS app internal directory in Swift?

Firstly, what is the equivalent name of android app internal directory in iOS app?

Secondly, how can I find the path of the directory? which api should I use in Swift?

My main purpose is trying to copy Bundle sqlite file to the directory

Upvotes: 1

Views: 1181

Answers (2)

teja_D
teja_D

Reputation: 438

Try this -

public func getDatabasePath() -> String {

    let databaseURL = try? FileManager.default
        .url(for: .applicationSupportDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
        .appendingPathComponent("YOUR_DB_FILE_NAME.sqlite")

    // If DB file not present in file manager copy DB file to user document directory
    let fileManager = FileManager.default
    if  fileManager.fileExists(atPath: (databaseURL?.path)!) {
        print("FILE AVAILABLE")
        return (databaseURL?.path)!
    } else {
        print("FILE NOT AVAILABLE ... Copying")
        if fileManager.createFile(atPath: (databaseURL?.path)!, contents: Data(), attributes: nil) {
            return (databaseURL?.path)!
        }
    }
    return ""
}

Upvotes: 0

Sunny
Sunny

Reputation: 831

To get path of document directory you can use this code.

 // path to documents directory
let documentDirectoryPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first
if let documentDirectoryPath = documentDirectoryPath { 

}

For more information follow these links : File System Basics and Accessing Files and Directories

Upvotes: 2

Related Questions