Suchi Suchi - 8 months ago 42
iOS Question

iOS - how to read entire content of an html resource file into an NSString *?

I want to put the content of my

resource file into an
object. Is it possible and advisable to do that? How could it be done?

Answer Source

Possible? - yes

Advisable? - unless it is an extremely large file, why not?

How? - There is already a method to do it for you in NSString - stringWithContentsOfFile:encoding:error:.

See the snippet below:

NSError* error = nil;
NSString *path = [[NSBundle mainBundle] pathForResource: @"foo" ofType: @"html"];
NSString *res = [NSString stringWithContentsOfFile: path encoding:NSUTF8StringEncoding error: &error];