Accessing Files stored in Application Bundle on iOS with Swift


  • Share on Pinterest

Here is a quick rundown on how you can access files within your application bundle on iOS with Swift.

Since the sandboxing on iOS gives your application access to its own folder structure you can gain access to files in your bundle by tapping into the NSFileManager and the main bundle resource path.

For this example let's say you have some reason to read in a text file contained in your application bundle and display it on the screen. Not a massive mind-blowing demonstration, but a simple one to get the idea across.

Set-Up

First, create a single view application and add a Text View object to the storyboard. I also removed the default text so it starts out empty.

Next, Create an @IBOutlet for the Text View object, I called mine displayText.

Now add a new file to the project (right-click on the project name and choose new file), This is the text file that will hold the data, I called it thetext.txt, and placed 3 paragraphs of plain text in the file.

That's the set-up complete, on to the coding fun.

The Code

So what is it we need to do to make it all work?

  • We need to locate the text file.
  • We need to load the text file.
  • We need to display the text.

Let's get started, for simplicity, I am going to do this in the viewDidLoad to keep only minimal code in the example.

First, we need to get the full path to our application

let filePath = NSBundle.mainBundle().resourcePath!

NSBundle.mainBundle() gives us the bundle object that contains our application executable. resourcePath! gives us the full pathname to the bundle's subdirectories containing our resources which include our text file.

Now we try and load the contents of the file into a constant.

let textContent = try! String(contentsOfFile: filePath + "/thetext.txt", encoding: NSUTF8StringEncoding)

Since there is the possibility that our file does not exist or something else could go wrong we put the statement in a try block. We concatenate the path and add what we know to be the file name, noting the ‘/' in front of the file name.

Finally, we assign the text content to our text field.

displayText.text = textContent

The viewDidLoad() code should now look something like

override func viewDidLoad() {
super.viewDidLoad() 		// Do any additional setup after loading the view, typically from a nib.		 		let filePath = NSBundle.mainBundle().resourcePath! 		 		let textContent = try! String(contentsOfFile: filePath + "/thetext.txt", encoding: NSUTF8StringEncoding) 		 		displayText.text = textContent 	}

This same technique can be used to load any file, not just text. You could just as easily use it to load images or any other media.

Run the application and everything should go according to plan.

I bet you thought it was harder than that, nothing else is needed unless you want to complicate the matter 🙂