Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Christina Cherry
1,110 Pointsuse NSArray to load html files stored locally
Is it possible to use NSArray to load html files stored in the app? I want to use html files rather than images so I can format information into tables with several columns and rows linked from the tableview showing the title of each table.
I've googled extensively and gone through heaps of tutorials but nothing really shows how to do it.
5 Answers
Aaron Daub
Courses Plus Student 144 PointsNot quite sure what you're trying to achieve. Could you explain, from a user's perspective, what you'd like to accomplish?
Ben Jakuben
Treehouse TeacherSounds like you want to load the HTML of the files as Strings and then put those Strings into an NSArray? That should be fairly straightforward.
Christina Cherry
1,110 PointsI have an NSArray of items from a game, which load a detail view that has the title, an image and also a text view of what the item is used for. I want to also load a webview in the same detail view that is fed from a html file that is included with the app, this will show a chart of the item, how long it takes to make and the value in a html table.
I tried the following into a uiwebview thinking it might work, but while it compiled with no errors it didn't load the file
_prodScreen = @[ @"item.html"];
Does this make sense?
Ben Jakuben
Treehouse TeacherSo this reply won't be incredibly helpful because I'm on the road and don't have my laptop handy to look up references or test anything. But what you need to do instead is use one of the following "load" methods for UIWebView:
loadData and load from the URL. In this case the URL would be local, though I'm not entirely sure if/How that can be done.
loadHTMLString and load the file contents into an NSString, and then use that NSString with this method.
Hopefully this helps, but if not, Amit or I will be in touch with you before too long.
Aaron Daub
Courses Plus Student 144 PointsYou don't necessarily need to load all of the HTML files and place them in an NSString instance and have an NSArray instance hold references to them. Another option would be to load the data from disk when a row of the UITableView is selected. To load the data from disk, familiarize yourself with the NSBundle class. See the attached sample code for an example.
NSString* htmlFile = [[NSBundle mainBundle] pathForResource: [NSString stringWithFormat:@"your-html-file-name%d", integerRepresentingSelectedRowIndex] ofType:@"html"];
if (!htmlFile) { NSLog(@"invalid path"); }
NSError* encodingError = nil; NSString* htmlString = [NSString stringWithContentsOfFile:html encoding:NSUTF8StringEncoding error:&encodingError];
if(error){ NSLog(@"Error encoding file to NSString"); }
[self.webView loadHTMLString:htmlString baseURL:nil];