If you have code like this in your app, you’re doing it wrong:
Instead, if you’re just talking about static text with some styling, you could keep all of that in an HTML document and do something like this:
NSString *privacyPath = [[NSBundle mainBundle] pathForResource:@"lovely_terms_of_use" ofType:@"html"]; NSString *privacyHTML = [NSString stringWithContentsOfFile:privacyPath encoding:NSUTF8StringEncoding error:nil]; [self.webView loadHTMLString:privacyHTML]
Let’s say you had a list of documents like this you needed to display, like terms and conditions and about, which were all listed as rows in a table view. Instead of coding what each row is, you could save a lot of code by putting that information into a property list. If you store the info for each document as an item in array — with keys like
documentName — you can load that array in your view controller and then pretty much automate this whole process with two classes and a few lines of code.
On top of getting clutter out of your code and saving the amount of code you write, there’s other nice things you can do now too. For example, maybe you want to be able to update any of these documents remotely. All you need to do is check a URL, download a copy of the files into your cache directory, and check for if there’s a match there before you display the one you bundled with the app.
Getting a feel for where you can get static content of out of code and into external resource files lets you not only write better code, but less of it.