Suchi Suchi - 7 days ago 5
iOS Question

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

I want to put the content of my

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

Answer

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];
Comments