Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

ios

iphone

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?

like image 716
Suchi Avatar asked Dec 16 '11 20:12

Suchi


People also ask

Why can't I encode the entire string in a string resource?

For some strings, you may not want to (or be able to) encode the entire string in a string resource because portions of the string might change at runtime. For example, if a string contains the name of a user document, you need to be able to insert that document name into the string dynamically.

What is a string file in resources?

Resource files that contain localizable strings are referred to as strings files because of their filename extension, which is .strings. You can create strings files manually or programmatically depending on your needs. The standard strings file format consists of one or more key-value pairs along with optional comments.

How to get the entire document as a string in JavaScript?

Given an HTML document, the task is to get the entire document as a string using JavaScript. Here few methods are discussed: getElementsByTagName () Method. This method returns a set of all elements in the document with the defined tag name, as a NodeList object. This object represents a collection of nodes, Which are accessed by index numbers.

What is the best way to save strings in Xcode?

Note: It is recommended that you save strings files using the UTF-8 encoding, which is the default encoding for standard strings files. Xcode automatically transcodes strings files from UTF-8 to UTF-16 when they’re copied into the product.


1 Answers

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];
like image 77
Krizz Avatar answered Sep 21 '22 03:09

Krizz