As promised in my last post I’ll shortly talk about what libraries did I use in the process of making the “Free Wallpaper a Day” app.
But first – for the ones who missed on Part 1, here a short resume:
My friend and partner, Nikolay Petkov, has drawn few hundred wallpapers for both the 3.5 inch and 4 inch iPhone screens. And so when I finally got some free time on my hands (after the big push to get “iOS6 by Tutorials” out) I thought I might quickly put together an application to distribute his wallpapers.
And now for part 2 “I are classes”:
ALAssetsLibrary+CustomPhotoAlbum
Since the application is essentially an iPhone wallpapers catalogue, which allows the users to save an image to their Camera Roll and then instructs them to open the image and set it as a screen wallpaper (still the only way to do that on iOS), I used my ALAssetsLibrary category to save the wallpapers into a custom photo album called “Wallpapers”. Neat!
You should download the category from my original post here and have a look: http://www.touch-code-magazine.com/ios5-saving-photos-in-custom-photo-album-category-for-download/
The category really makes saving a photo a breeze. Here’s the actual code from the app source code:
ALAssetsLibrary* library = [[ALAssetsLibrary alloc] init];[library saveImage: img toAlbum: @"Wallpapers"withCompletionBlock: ^(NSError *error) { //code ... if (error!=nil) { //image was not saved [[[UIAlertView alloc] initWithTitle:@"Error" message:[error localizedDescription] delegate:nil cancelButtonTitle:@"Close" otherButtonTitles: nil] show]; } //further code }]; |
Notice one thing in the code above. I am creating an ALAssetsLibrary and using it for a one shot operation. This is contrary to what Apple recommends – namely, to have one shared instance of ALAssetsLibrary. I had countless comments from people that instances of ALAssetsLibrary mysteriously disappear and much more. Further the Assets framework is not thread safe, so using a shared instance is always a pain.
Therefore to minimise trouble along the way – for the “Free Wallpapers a Day” app I use a one-shot instance of the library. If you don’t use heavily your instance, I’d say – use a one shot copy as well.
MTPopupWindow
The concept of a little window that animates in on the screen and shows an HTML file is based on my app Pimpy – Terms and Conditions screen.
Recently I released a polished version and uploaded in on GitHub, and also I’ve put together a presentation post, you should check out here: http://www.touch-code-magazine.com/showing-a-popup-window-in-ios6-modernized-tutorial-code-download/
In short MTPopupWindow allows you to easily show modal window views and load HTML content inside. The actual code from the app doing so:
-(IBAction)actionTerms:(id)sender{ [MTPopupWindow showWindowWithHTMLFile:@"Terms.html"];} |
This action is connected to the Terms button on the about screen and shows up the Terms, included in Terms.html in the app bundle. Easy – peasy
HUD
Matej Bukovinski (http://www.bukovinski.com/) put out a simple HUD control and there’s a ton of forks around GitHub. I like the ease of use of his control, and instead of doing another fork I did a simple class, which gives shortcut methods to showing and hiding different HUD controls.
This is for example how an alert looks like using my HUD class.
The class is pretty easy to use. Here’s how you show a spinner (while doing some blocking work, like authorisation via Internet, or something else):
[HUD showUIBlockingIndicator]; |
Here’s how you show a timed message – it disappears after a specified period of time:
[HUD showUIBlockingIndicatorWithText:@"Will timeout in 2 secs!" withTimeout:2.0]; |
And finally how to show an alert message:
[HUD showAlertWithTitle:@"An alert box!" text:@"Yupeee! Press the checkmark to close it whenever you're done reading the text!" target:self action:@selector(closed)]; |
The class can do much more, you should check the source code and the little demo app I’ve put on GitHub:
https://github.com/icanzilb/HUD
Tracker
Using Google Analytics is pretty neat to have anonymous data about how many people use your app, what languages they use, etc. etc.
However for a class that you would use often and on every screen of your app, it does have very inconvenient method names. Sometimes my workflow really gets broken by looking up method names and how to use them. That’s why I am a big fan of producing shortcut classes (like the HUD class above) and like the Tracker class below.
You can have a look at the code on GitHub: https://github.com/icanzilb/Tracker.
You should read the short introduction on GitHub, but all in all what the class does is allow me to report screen views in a super simplistic manner, like so:
-(void)viewDidAppear:(BOOL)animated{ [Tracker screenView:@"/about"];} |
You can also report something that is not necessarily related to opening a new screen. For example the wallpapers app keeps a log of the previewed wallpapers, this way it’s easy to see which papers get more interest. Here’s the code which reports previewing a wallpaper:
[Tracker event: [NSString stringWithFormat:@"wallpaper %@", image.filename] ]; |
This way in Google Analytics I can check which filename was previewed most often:
All that is left is to check which wallpaper is the trink.jpg file on the web site of “Free Wallpaper a Day”:
Sweet!
And it’s also a great idea to include Tracker.h in your .pch file, because you are indeed going to use the class in all your view controller classes.
NSObject+AutoCoding
If there’s one Objective-C class which I really am fascinated by this is the AutoCoding category on NSObject. This category allows to your save almost any object to a .plist file. You don’t need to do any NSCoding – the category retrospects any object and persists its properties in a dictionary form, so the data can be saved then to a normal .plist file.
The category is awesome and you should quickly have a look here:
https://github.com/nicklockwood/AutoCoding
But wait! There’s more …
Yes, there’s more
After for about a year working on iOS apps using heavily JSON for communicating with a web server (the last task being putting together quickly the Wallpapers JSON API) I grew tired by the traditional hand crafted ways of modelling JSON data.
Therefore I am to release soon an open source framework for rapid JSON models.
I know – there are already several out there, but I am not satisfied with them. I mean … pre-writing code for handling data in and out? Pardon my French, but I haven’t seen a bloody API, which doesn’t morph a thousand times before version 1.0 … so, I am asking, of what use to me is a tool that pre-writes the code to handle this ever-changing input?
Anyhow. My JSON model lib is coming out soon – stay tuned.
That’s it for now! There was also a bunch of other classes I wrote for the Wallpapers app, but these will probably come out in the blog anyway.
Soon to come as well is Part 3 – the software tools I used in putting together the app! Stay tuned!
Oh, yes – also give “Free Wallpaper a Day” a try on the App Store!
Cheers, Marin
The post was originally published on the following URL: http://www.touch-code-magazine.com/free-wallpaper-a-day-is-live-on-the-appstore-part-23-the-code-libraries/·
Source : touch-code-magazine[dot]com
0 comments:
Post a Comment