HTML Email Template as NSString

I am adding functionality to my application that will allow it to send some pretty detailed information. I would like to send this as an email in HTML format so that I can use the HTML and CSS style.

The approach I would like to take is to create a template with placeholders for the fields you need to fill in, load the template into NSString, and then use line replacement to replace the placeholders with actual data.

Instead of having one huuuuuuge line, for example:

NSString *emailTemplate = [NSString stringWithFormat:@"<h1>TITLE</h1><hr noshade/><p>Here is the information about the thing that you are interested in. Find more from the table below:</p> . . ."]

I would like to do it in a more elegant way. Should I write a template to a file and then upload it? What is the best way to do this?

Also, how can I do this with localization capability?

Thank!

+3
source share
2 answers

I'm not sure of a better way to do this in a localized way, but it would be much more useful for you to write HTML in your own .html file and read the contents of this file into NSMutableStringan object when you need it.

You can do this using

+ (id)stringWithContentsOfFile:(NSString *)path encoding:(NSStringEncoding)enc error:(NSError **)error

If you use this method, you need to call mutableCopyin the returned string so that you can replace the string later. Do not forget that with the help of a mutableCopynew object will be returned NSMutableString, for which you will be responsible for the release, when you finish with it.

Then you can use

- (NSUInteger)replaceOccurrencesOfString:(NSString *)target withString:(NSString *)replacement options:(NSStringCompareOptions)opts range:(NSRange)searchRange

To replace placeholders with your content.

, HTML WYSIWYG IDE . , .

+3

Matt Gemmells, MGTemplateEngine. OS X iOS. , , SVN, - git -svn'ed Github.

, (, , , , , ).

0

Source: https://habr.com/ru/post/1736346/


All Articles