David Sanford
David Sanford

Reputation: 745

defining location of local HTML files

Silly question that am embarrassed to ask, but cannot figure it out.

Adding a local html is easy, but as I will have a few different pages (scenes), I need each html page to have its own directory.

I am struggling how to define the location (directory) of the html files to be used.

Here is the code using:

    let filePath = ("html_shark" as  NSString).stringByAppendingPathComponent("sharks.html")
    let url:NSURL=NSURL(fileURLWithPath:filePath)
    let request:NSURLRequest=NSURLRequest(URL:url)
    myWebView!.loadRequest(request)

The two folders that will be used are the

html_shark @ sharks.html and HTML_turtlesCon @turtles.html

Upvotes: 0

Views: 498

Answers (2)

Elf Sundae
Elf Sundae

Reputation: 1615

[NSURL fileURLWithPath:@"foo/bar.html"] will be file:///foo/bar.html, it means a file URL which located /foo/bar.html on the device file system, / is not the root directory of your app's bundle path.
You can get the root path for your app with NSBundle class. e.g. [NSBundle mainBundle].bundlePath.

For your issue, if your html_shark HTML_turtlesCon directories show yellow icon color in Xcode, use this code below:

NSURL *url = [[NSBundle mainBundle].resourceURL URLByAppendingPathComponent:@"sharks.html"];

and if directories contains html files show blue icon color in Xcode, use:

NSURL *url = [[NSBundle mainBundle].resourceURL URLByAppendingPathComponent:@"html_shark/sharks.html"];

Swift code:

let url:NSURL = (NSBundle.mainBundle().resourceURL?.URLByAppendingPathComponent("sharks.html"))!
webView.loadRequest(NSURLRequest(URL: url))

Upvotes: 0

Christian Dietrich
Christian Dietrich

Reputation: 11868

are you sure you have real folders and not groups only. if so something like

let path = NSBundle.mainBundle().pathForResource("htmlpage2", ofType: "htm", inDirectory:"sub")
// use path to create nsurl for nsurlrequest

Upvotes: 2

Related Questions