Sunil Kumar Sahoo
Sunil Kumar Sahoo

Reputation: 53687

How to create html file in objective-c?

In my app i want to create html file from arrays, can anyone know how this can be achieved?

Thanks in advance!

Upvotes: 2

Views: 4168

Answers (1)

TomTom
TomTom

Reputation: 2014

this is acutally quite easy:

You can start with an NSMutableString and build up a large String which you can convert later in a file:

NSArray *yourArray = [NSArray arrayWithObjects: @"green", @"blue", @"red", nil];

NSMutableString *htmlText = [[NSMutableString alloc] init];
[htmlText appendFormat: @"<html><head><title>TEST</title></head>"];
[htmlText appendFormat: @"<body>"];

[htmlText appendFormat: @"<ul>"];
for ( NSString *tmpText in yourArray] )
{
  [htmlText appendFormat: @"<li>%@</li>", tmpText];
}
[htmlText appendFormat: @"</ul>"];

[htmlText appendFormat: @"</body>"];
[htmlText appendFormat: @"</html>"];

// create the file

NSError *error = nil;

[htmlText writeToFile:yourPath atomically:YES encoding: NSUTF8StringEncoding error:&error];

if ( error )
{
  // do some stuff
}

Upvotes: 10

Related Questions