12

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?

sudo make install
  • 5,629
  • 3
  • 36
  • 48
Suchi
  • 9,989
  • 23
  • 68
  • 112
  • Possible duplicate of [How to download a web page data in Objective-C on iPhone?](http://stackoverflow.com/questions/1867002/how-to-download-a-web-page-data-in-objective-c-on-iphone) – Richard Stelling Nov 28 '16 at 18:24

1 Answers1

29

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];
Krizz
  • 11,362
  • 1
  • 30
  • 43
  • Don't forget to check for the error afterwards though. And be sure to save your HTML file as UTF8. – Ahti Dec 16 '11 at 20:53
  • 1
    And to call it foo.html. ;) Just hoped author of a question will read the code instead of blindly copying it. – Krizz Dec 16 '11 at 21:00
  • haha don't worry I'm not that much a n00b. it worked just fine. thanks! – Suchi Dec 16 '11 at 22:03