user2578110
user2578110

Reputation: 19

Pulling the raw html string from an htm file stored locally in Xcode

I have a locally stored .htm file and I want to pull the raw html string and display it a textview. I know how to display strings in textviews and everything else, I just need to know how to get the inner strong from the locally stored .htm file. Right now all I can pull is the path.

Upvotes: 0

Views: 1519

Answers (1)

ctrucza
ctrucza

Reputation: 172

First, you'll have to add the file to your project and include it in the application bundle:

  • add the file to your project
  • select the target
  • go to the Build Phases tab
  • expand the Copy Bundle Resources section
  • add the file to the list

This makes sure the file is available when your application runs on the device.

To read the content of the file when your application runs, get the file path:

NSString *path = [[NSBundle mainBundle] pathForResource:@"your_file" ofType:@"htm"];

and then load the file

NSError *error;
NSString *content = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:&error];

Things to watch out for:

  • check for the error value after reading
  • if the file is large, you might want to read it asynchronously

Upvotes: 2

Related Questions