Archive for the ‘iOS Development’ Category

JEDownloader

Monday, April 23rd, 2012

When working on iOS projects, there are some tasks that you find yourself doing over and over. For example, using NSURLRequest and NSURLConnection to call an API, or download a file. There are many heavyweight libraries out there (e.g. ASIHTTPRequest) which are great for larger projects, but I wanted to produce a small and lightweight library that encapsulates the common behaviours needed to access a web service, download a small image, etc. that are core activities within most modern apps.

You can find JEDownloader on GitHub here: https://github.com/jonathanellis/jedownloader

Generating non-retina images automatically for iOS

Monday, April 9th, 2012

Developing apps, I found I spent a lot of time in Photoshop making minor adjustments and then having to export them at both retina (@2x) and standard resolutions. After unsuccessfully searching online to try and find any scripts, plugins or apps to generate the non-retina images automatically, I took the plunge and wrote one myself.

Simply export all of your images as PNGs from Photoshop (or other image editing package) at retina resolution, with the @2x suffix, and this PHP (CLI) script will scale the images to 50% and copy them without the @2x suffix. Unlike many other image resizing scripts out there, my version fully supports alpha transparency.

(more…)

performSelector with Primitives, Enums, Structs and Objects

Friday, February 10th, 2012

Most iOS developers will at some point have come across performSelector: and its’ variants, which provide much greater flexibility than standard method calling, and is often used to defer method call decisions to runtime. Particularly when combined with NSSelectorFromString(), it becomes an incredibly powerful meta-programming tool.

However, one thing that performSelector: and its variants don’t handle well at all are primitives, enums and structs (hereon referred to simply as “primitives”). Because performSelector: heavily uses the generic id type which is only suitable for objects. Therefore, only objects can be specified as arguments, and only objects are returned (if a primitive is returned, it just reverts to nil).

However, there are often times when you are working not only with just objects, but also structs, enums and primitives both as arguments and as return values. I’ve therefore written a wrapper around NSInvocation (a more powerful set of tools than performSelector:) which handles passing pointers to primitives, and returning pointers to primitives.

(more…)

Detecting touches on MKOverlayView

Friday, January 27th, 2012

So you’ve used MKOverlayView to add a Google Map to your app and you’ve added to it a series of MKOverlays and implemented mapView:viewForOverlay: which returns the corresponding MKOverlayView for your overlay. But the problem is that now you want to detect touch events on the MKOverlayView so that an action can be taken depending on which view is tapped.

This seems to be one of those questions that many have asked, but has not really received any successful answer! Well, there is actually a way to detect touches on an MKOverlayView, and it’s surprisingly simple!

(more…)

Working with SQLite in Objective-C/iOS

Thursday, January 26th, 2012

The Cocoa API provides Cocoa Touch which is a great (and powerful!) abstraction layer over the underlying SQLite libraries, but sometimes you just want to keep things simple with simple operations directly on a database. Unfortunately, the SQLite libraries themselves are implemented in C, so not object-oriented, and are fairly complex.

With that in mind, for a recent project I developed my own small wrapper around the underlying SQLite libraries to allow me to perform queries and get back responses as Objective-C objects rather than C primitives. I’m not going to reproduce all of the code here, but I will provide a starter that will get you up & running and give you most of the functionality you would need. Of course there is more you can do (particularly error handling) that I don’t get into here, but feel free to post any improvements you may have in the comments.

(more…)

Multithreading with CocoaAsyncSocket

Friday, December 2nd, 2011

CocoaAsyncSocket is a fantastic socket library for Cocoa (iOS and MacOS). It comes in RunLoop and GCD flavours, but either way its asynchronous, which means that you can fire off and receive data without your code blocking (which means your code can get on with other things, like providing GUI interactivity, etc.)

However, one of the problems I found with CocoaAsyncSocket (using the RunLoop version) was that despite its asynchronous nature, when building a server application handling multiple sockets that also required interactivity, managing a large number of sockets and packets could cause significant slowdown to my app.

(more…)