Ben A. Ben A. - 2 months ago 6
iOS Question

How to access a computer file on a phone

I am using

let data = try? NSString(contentsOfFile: "/Users/BenA**** 1/Desktop/textFile.txt", encoding: String.Encoding.utf8.rawValue)
to access a file on my computer. When I test put the app on my phone, it obviously didn't work because it didn't have access to the file. How could I put this file on my phone and be able to access it from there? Thanks.


Include your "textFile.txt" in main bundle by drag and drop file in to xcode project, then access it by

class testViewController: UIViewController{

var data:String = ""
override func viewDidLoad() {

    let path = NSBundle.mainBundle().pathForResource("textFile", ofType: "txt")
    data = try? NSString(contentsOfFile: path!, encoding: ENCODING)

Try this way to access 'data' in rest of code. Hope this help.