Category Archives: Code Snippets

Coding snippets for iOS on iPhone / iPad

Core Data Helper – the final chapter!

I’ve probably written about the Core Data Helper too many times for one blog, but I thought just to finish it off I’d write one more article with a good solid downloadable project to demonstrate the use of Core Data Helper.  Basically the helper wraps core data stuff to make accessing it a little tidier… in fact, instead of re-inventing the written wheel (?), why not read about it in the other article HERE and then come back.

So what’s this article all about then?  Well, just below this paragraph is a download link to my project which very basically shows how to use the Core Data Helper.  It also has a useful XML parser for pulling in some default data from an XML file to your Core Data store.  I use this similar format for data which will appear in static lookups and suchlike.  Go download it and take a look…. Continue reading

Core Data helper – REVISED

After my initial post regarding the the modified Core Data helper (by Björn Sållarp in this article) I decided to alter it to be a bit more helpful, so I added a few more methods to perform some commonly required tasks. You’ll find the revised class at the bottom of this article. Here are the new methods and examples of how you would use them. For completeness I’ve included the original methods too. The error checking for all the methods has been changed and should be working correctly (they don’t do anything – but you’ll be notified if something didn’t work and can write your own handling). So… after including the class in your file you can do the following… Continue reading

Too bright or not too bright?

I thought I may was well mention this in a very very short post because I took a lot of time out to search for answers. As we all know, there are certain APIs which Apple don’t allow the use of which include some of the more useful features on the iPhone. A couple of days ago, I was looking into controlling brightness and guess what? You can’t do it. Not with the public APIs anyway.

But I wanted to find out if it were possible with any third party APIs and really couldn’t find anything. Yeah, it seems to be ‘that’ simple. I did see reference to using the following code in a couple of websites – but I assure you, this doesn’t seem to work (not in iOS 4.x anyway). Continue reading

Core Data query helper


I’ve recently started using Core Data quite a bit for a few different projects and quickly found this bit of code and modified it slightly for my own use. It was originally posted by Björn Sållarp through his blog, on a helpful post about how to use Core Data and I’ve continued to use it since in different formats.

The code basically returns a data set from Core Data (with or without a predicate) without having all the messy code for each query. Here’s the code… Continue reading

Making a count indicator

Count indicator exampleJust recently I wrote a list app for iPhone, so I could maintain lists with items in them.  I wanted to jazz it up a bit by using a count indicator similar to the one you see in the mail app, which shows you how many messages you’ve had from someone (you know the one… the little bubble with a number in it).  Anyway, turns out that Apple don’t provide a mechanism for this so I wrote my own one.  You can see it over there to the left (a little gray bubble with a count in it) and this post basically shows you how I achieved it (with code and an example project).

All it’s essentially doing is using Core Graphics to draw a line. That’s all. One line, except to get the rounded ends I’m using a cap as demonstrated in many of the Apple examples. I thought this was easier than figuring out how to round it off manually. Then I take the text and paste it as close to center as I can.

Now it’s not perfect I know… changing the font size doesn’t always vertically align the number properly – but it does look nice enough, and I’m sure someone can make use of this even if a little tweaking is required.  At around 14 point font size it looks good….

Continue reading