Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift 3 : How to get path of file saved in Documents folder

Tags:

ios

swift3

path = Bundle.main.path(forResource: "Owl.jpg", ofType: "jpg")

returns nil, however, using NSHomeDirectory() I'm able to verify that is under Documents/ folder.

like image 458
Marin Avatar asked Nov 14 '16 22:11

Marin


People also ask

How do I open a file path in Swift?

First make sure when you drag your folder audioFiles to your project to check copy items if needed and select create folder references. Make sure it shows a blue folder if your project. If you would like to get that file URL you can use NSBundle method URLForResource(withExtension:, subdirectory:) Thank You.

What is document directory in iOS Swift?

Every iOS app gets a slice of storage just for itself, meaning that you can read and write your app's files there without worrying about colliding with other apps. This is called the user's documents directory, and it's exposed both in code (as you'll see in a moment) and also through iTunes file sharing.

How do you set the path of a file?

To use a relative path, type ". \" to specify the first file path in the list, and then type the name of the subfolder. For example, if the first file path in the list is F:\Gainseek\Data\, then the path .


2 Answers

First, separate name and extension:

Bundle.main.path(forResource: "Owl", ofType: "jpg")

Second, separate (mentally) your bundle and the Documents folder. They are two completely different things. If this file is the Documents folder, it absolutely is not in your main bundle! You probably want something like this:

let fm = FileManager.default
let docsurl = try! fm.url(for:.documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false)
let myurl = docsurl.appendingPathComponent("Owl.jpg")

Third, if Owl is an image asset in the asset catalog, then say

let im = UIImage(named:"Owl") // or whatever its name is
like image 94
matt Avatar answered Oct 19 '22 20:10

matt


Tested on: xCode 8.3.2 & Swift 3.1

First drag your file (JPG, MP3, ZIP) inside your project folder and make sure Copy items if needed is checked and project/app is selected in Add to targetsenter image description here

Inside relevant ViewController

let fileName = "fileName"
let fileType = "fileType"

if let filePath = Bundle.main.path(forResource: fileName, ofType: fileType) {
    print(filePath)
}

If you need to get the file URL you can use NSBundle method

if let fileURL = Bundle.main.url(forResource: fileName, withExtension: fileType) {
    print(fileURL)
}

Also NSBundle method pathForResource has an initializer that you can specify in which directory your files are located like:

if let filePath = Bundle.main.path(forResource: fileName, ofType: fileType, inDirectory: "filesSubDirectory") {
    print(filePath)
}

And for getting file URL:

if let fileURL = Bundle.main.url(forResource: fileName, withExtension: fileType, subdirectory: "filesSubDirectory") {
    print(fileURL)
}
like image 45
Atlas_Gondal Avatar answered Oct 19 '22 20:10

Atlas_Gondal