开发者

iPhone create html receipt email based on contents template file

I'm tryi开发者_JAVA百科ng to send an HTML email receipt from the iphone. The receipt should have some dynamic data in it.

I would prefer to have a template .html file with {placeholders} in it that will be replaced by using something like this:

 strBody = [strBody stringByReplacingOccurrencesOfString:"{name}" withString:username]

But first I need to know how to load the contents of the file into a string so I can start replacing the placeholders.

How could this be done and will this work well?


You will have to add your .html file to your Xcode project, after that something like this should work:

NSString *file = [[NSBundle mainBundle] pathForResource:@"nameOfFile" ofType:@"html"];
if (file) {
    NSString *html = [NSString stringWithContentsOfFile:file encoding:NSUTF8StringEncoding error:nil];
    if (html) {
        html = [html stringByReplacingOccurrencesOfString:@"{title}" withString:@"foo"];
        html = [html stringByReplacingOccurrencesOfString:@"{body}" withString:@"bar"];
        NSLog(@"%@",html);
    }
}


For this kind of situation I highly recommend Matt Gemmell's open-source MGTemplateEngine. With that you create a template file of named placeholders, and pass in a dictionary of replacements where each key matches one of the placeholders. It's very flexible. Check out the demo project for details on how to use it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜