[[UIDevice currentDevice] uniqueIdentifier] Substitute

Identifying devices uniquely is handy. Apple deprecated their uniqueIdentifier property, with the severe warning “Do not use the uniqueIdentifier property”, so it’s time to migrate before they remove it altogether or start rejecting apps *sigh*.

With that in mind, I created a replacement method for UIDevice: UDID. It uses the device’s MAC address to generate a unique ID that is the same length as the old UDIDs. I was inspired by some other people’s solutions, but rolled my own implementation which I feel is neater, and more compatible (as it generates similar 40char UDIDs). You can fork it on github.

It’s not a drop-in replacement, for fear of monkey patching over an existing method. Just do a find & replace with: [[UIDevice currentDevice] UDID].

Of course: consider if device identifiers are right for you. To identify a specific install of your app (which can be migrated from one iPhone another due to loss/upgrading), generating a UUID and saving that as a NSUserDefault would work best (since it will transfer with the app data).

License: public domain.

23 Jan 2012, 6:11pm

2 comments

Turbocharging git on Mac OS X

This weekend I discovered two awesome graphical tools to improve my git experience on Mac OS X:

The most awesome is KDiff3 (search for ‘Apple Mac OSX binary’ on that page for the Mac binary). This is a three-way merge tool with an editing panel below. Seriously where has this been all my life? Amazing.

Download it, and install – make sure it’s in your system PATH. Then simply run git config --global merge.tool kdiff3 to set it as your default merge tool. Now every time you get a conflict, your git mergetool command will actually be rather helpful! Basically you are shown the base revision (that both branches split from) in the middle, one branch either side, and an editor below allowing you to pick which side you want and/or edit the file directly.

The second tool I (re)discovered was SourceTree (Mac App Store DL Link). It is a Mac GUI client for git. I’m pretty comfortable with the command line, but what I love about this tool is it allows me to review my changes before I commit them (either staged or unstaged), discard hunks I don’t want, and of course open the file in question for editing. Very convenient. git diff is nice and all, but I love having that handy ‘discard hunk’ button in SourceTree so I can skip my silly changes. For me this is the most useful feature, though no doubt there are others. Oh and it’s free (it’s a bit confusing, as there is a “trial” period, but after the trial you simply register for free, and are given a license).

more »

11 Jan 2012, 12:50pm

leave a comment

Migrating data in a Rails migration

Turns out it’s easy to whack any old piece of code into the migration for migrating your data, for example these work well for some arbitrary SQL statements (I used UPDATE ones…)

ActiveRecord::Base.connection.select_one('SELECT COUNT(*) FROM mytable')
ActiveRecord::Base.connection.execute('SELECT * FROM mytable')

Thanks revgeorge.

19 Dec 2011, 6:23pm

leave a comment

AppleIDs and multiple iOS developer accounts

“In the iOS Development Ecosystem the people are represented by two separate, yet equally important groups. The iOS Developer Users who create apps and the iTunesConnect Users who upload the apps. These are their stories.”

An iOS team is actually in two parts: the iOS Developer Team, accessed via the Member Center, and a completely separate iTunes Connect team.

It turns out that the one AppleID can belong to multiple iOS developer teams, and Apple asks you when you login to the member center which team you want to use. However iTunes Connect has no such feature, and each AppleID can only belong to one iTunes Connect “team”.

So you can re-use AppleIDs for developer accounts, but not itunes connect accounts. It’s up to you I guess if you want a totally separate AppleID for each team, or just each iTunes connect account.

18 Dec 2011, 11:49am

leave a comment

monit

Monit for linux is a great way to make sure Apache is running.

I set it up with the help of this fantastic tutorial. My steps after the break.

more »

13 Dec 2011, 10:02pm

leave a comment

NSDateFormatter format specifiers

Ever wanted the list of format specifiers?

As per the docs, it is here.

Anything in single quotes is put through as-is.

13 Dec 2011, 7:23pm

leave a comment

Android Market Resources

Confused about what each of the images do and how they’re cropped?

Look no further than this awesome test app! Market link here.

Some high quality phone image marketing resources. I like this one.

5 Dec 2011, 4:16pm

leave a comment

Posting to Rails with AJAX

Don’t forget your csrf token!

How to do it in Javascript.

The token itself can be retrieved in Rails with: session[:_csrf_token]

17 Nov 2011, 1:40pm

1 comment

When symbolicatecrash Fails (and it often does)

This problem has plagued me for almost three years now. It seems sometimes symbolicatecrash works, sometimes it doesn’t.

I have all my dSYM files (I even use a build script that archives them all automatically), and spotlight can find them from the UDID, but to no avail.

The solution is atos.

I copied the dSYM file (actually a folder) to the desktop, and run this command & you will get the file & line of code. Repeat for each symbol in the crash that you need (generally you only need one or two key ones…)

$ atos -arch armv7 -o ~/Path/to/Your.app.dSYM/Contents/Resources/DWARF/AppName 0x000cefd8
 -[SomeClass someMethod] (in GeoLog) (AppName.m:242)

Thanks to NaveenShan for this tip.

Incidentally, If someone writes a Mac OS X app that can reliably symbolicate crashes, each time, every time, and doesn’t break on every new version of the SDK/OS – I will pay upwards of $50.

I wish things would just work, and I wouldn’t have to waste my precious dev time stuffing around with this crap.

4 Nov 2011, 10:14pm

10 comments

Oh My God They Killed parentViewController

Pre iOS 5, ‘parentViewController’ would return the view-stack’s parent view controller, or the controller which presented that controller when called on a modal view controller. This was the intended, documented functionality of the method, not some mistake.

iOS 5 changes this by splitting out the latter functionality to ‘presentingViewController’. Unfortunately, rather than creating 2 new methods with new functionality, they simply changed the old one and created the new one. So for anyone like me who missed this particular change (it’s not even in the API diffs as it’s not changed, or removed), [self parentViewController] starts returning nil, your code continus to run, but will probably hang if when you expect to have dismissed the view controller.

What terrible design. I think it would have been better to deprecated the old function & split it into two new ones. Or at least provide some warnings to the change. If Objective-C actually crashed on nil values like java, again this would be easy to spot & change – but hang bugs are particularly nasty to try and solve.

Anyway, here are my testing results of [self parentViewController] where self is a controller that was presented as modal:

XCode 4.1 & SDK 4.3: iOS 5: presentingViewController, iOS 4.x: presentingViewController
XCode 4.2 & SDK 5.0: iOS 5: nil, iOS 4.x: presentingViewController

As you can see, when iOS5 runs the older SDK it doesn’t break (because that would break everyone’s code), and iOS4 running with code from the new SDK doesn’t break either. Just SDK & iOS 5 together.

Fixing this if you only support iOS 5 is simple. Replace every instance of [self parentViewController] where you want the model view controllers parent to [self presentingViewController].

Most people need to support iOS 4. This is a bit of a pain because you can’t just call ‘presentingViewController’ without it crashing (due to this method not existing on iOS4). Here’s my #define to get the old behaviour back:

#define self_parentViewController (([self parentViewController] != nil || ![self respondsToSelector:@selector(presentingViewController)]) ? [self parentViewController] : [self presentingViewController])

In english, this means: if the parentViewController is not nil, or the presentingViewController method doesn’t exist yet (in the case of < iOS 5.0), return the result of presentingViewController. Else return the result of presentingViewController.

EDIT: if you need to call the old parentViewController on something other than self, try my revised #define.

NB. just like before – this *may* not return the presenting view controller, if self has it’s own parentViewController that isn’t the one which presented it. But this is equal to the old SDK behaviour, so I don’t think it will introduce bugs.

Everywhere in your code, replace [self parentViewController] with self_parentViewController (no square brackets).

#defines are normally pretty evil but in this case, until you drop 4.0 support I actually feel it’s cleaner. Now you could do [self parentViewController] || [self presentingViewController], and this will work in many cases, that is until parentViewController returns nil on iOS 4, when your app will crash due to the unrecognised ‘presentingViewController’ selector. Safter to use my #define for full backwards compatibility.

more »