26

I'm here with a question that probably has a really simple answer that I am overlooking... how can I retrieve local files with NSURL? I have this here:

override func viewDidLoad() {
    super.viewDidLoad()
    var urlpath = NSBundle.mainBundle().pathForResource("bpreg", ofType: "xml")
    let url:NSURL = NSURL(string: urlpath!)!
    parser = NSXMLParser(contentsOfURL: url)!
    parser.delegate = self
    parser.parse()
}

But after it successfully builds it hangs on var urlpath. I've searched around and tried a few suggestions here and other places to no avail. Please help? :(

Dan Beaulieu
  • 19,406
  • 19
  • 101
  • 135
Philip Ulrich
  • 263
  • 1
  • 3
  • 6

3 Answers3

49

You are trying to load a file from your file system, not from web.

For creating the NSURL you need to use fileURLWithPath: class method.

Change your method like:

Swift 2

override func viewDidLoad()
{
    super.viewDidLoad()
    var urlpath     = NSBundle.mainBundle().pathForResource("bpreg", ofType: "xml")
    let url:NSURL   = NSURL.fileURLWithPath(urlpath!)!
    parser          = NSXMLParser(contentsOfURL: url)!
    parser.delegate = self
    parser.parse()
}

Swift 3

override func viewDidLoad()
{
    super.viewDidLoad()
    let urlpath     = Bundle.main.path(forResource: "bpreg", ofType: "xml")
    let url         = NSURL.fileURL(withPath: urlpath!)
    parser          = XMLParser(contentsOf: url)!
    parser.delegate = self
    parser.parse()
}

Note: In Swift 3 you can also use the URL class to construct the url instead of NSURL class. So the above code for constructing url changes to:

let url = URL(fileURLWithPath: urlpath!)
Community
  • 1
  • 1
Midhun MP
  • 103,496
  • 31
  • 153
  • 200
  • Better yet, just use `URLForResource` instead of `pathForResource` as suggested below. Toss-up which answer is better, both leave something out. – David Berry Feb 09 '15 at 21:36
  • @MidhunMP Sorry for bugging you again, but I'm having a similar issue with this section (loading a local html file). Maybe you can point out my stupid mistake here? :) – Philip Ulrich Feb 10 '15 at 02:26
  • `override func viewDidLoad() { super.viewDidLoad() var url = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource(pageLink, ofType: "html")!) var request = NSURLRequest(URL: url!) webView.loadRequest(request) webView.delegate = self }` – Philip Ulrich Feb 10 '15 at 02:27
  • Sorry, I fixed it myself! :) `override func viewDidLoad() { super.viewDidLoad() let url = NSBundle.mainBundle().URLForResource(pageLink, withExtension: "html"); let request = NSURLRequest(URL: url!) webView.loadRequest(request) webView.delegate = self }` – Philip Ulrich Feb 10 '15 at 02:40
  • @PhilipUlrich: Sorry, I was offline. Happy to hear that you solved the issue – Midhun MP Feb 10 '15 at 03:31
11

You should use URLForResource(_:withExtension:) instead of pathForResource:

let fileUrl = Bundle.main.url(forResource: "bpreg", withExtension: "xml")
Leo Dabus
  • 229,809
  • 59
  • 489
  • 571
7

Your file doesn't exist in your main-bundle yet.

You have to add the file your are using to your Bundle Resource.

So to add it. Go to App Target -> Build Phases and check the Bundle Resource and add it by drag n' drop.

Christian
  • 22,585
  • 9
  • 80
  • 106