Reputation: 3803
I have to create a UIWebview by adding some data I'm getting from my API into a HTML file I have locally. It's the first time I have to do this and I can't find how to do this.
My HTML file looks like :
<h1>%%%title%%%</h1>
<p class="date">%%%date%%%</p>
<a %%%hrefimage%%% class="link-diapo">
<div class="image">
<img src="%%%img%%%" alt="%%%title%%%"/>
%%%diapo%%%
</div>
</a>
<div id="content" style="font-size:%%%size%%%px;">
<p>%%%chapo%%%</p>
<p>%%%html%%%</p>
<p class="source">%%%author%%%</p>
</div>
and my data :
"title": "My title",
"date": "2013-07-10",
"hour": "19h45",
"chapo": "blablabla",
"author": "Me",
"description": "html text"
...
Is there a method to do that easily ? Or do I have to detect the %%% in my HTML file ?
Thx !
Upvotes: 0
Views: 219
Reputation: 17094
You could use the Mustache templating engine to inject the dictionary's values into the markup.
To actually load the markup string into the UIWebView
you would do something like the following:
NSURL * URL = [NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]]
[self.webView loadHTMLString:markup baseURL:URL];
Upvotes: 1
Reputation: 1127
If you have your initial HTML in an NSString, you could use stringByReplacingOccurrencesOfString:withString:, something like
NSError *error;
NSString *initialHTMLString = [NSString stringWithContentsOfFile:(your file path) encoding:NSUTF8StringEncoding error:&error];
//do something to get the data back from the api, I'll assume NSData w/ JSON formatting
NSDictionary *jsonDic = [NSJSONSerialization JSONObjectWithData:jsonData options:kNilOptions error:nil];
NSString *titleString = [jsonDic objectForKey:@"title"];
NSString *HTMLStringWithTitleAdded = [initialHTMLString stringByReplacingOccurencesOfString:@"%%%title%%%" withString:titleString];
You would then need to repeat something like that for each of the key data you are getting back from the API.
Upvotes: 1