Adding properties to a class through Categories

Nope, it can’t be done!  Well that’s not entirely true. Dave is certainly a man of mystery, but perhaps we can expand upon his cryptic answer?  Associative references are, indeed, the answer, but what are they, and how can they be used to implement properties through a category? I needed to do just that a while back when I was working on adding NSCoding functionality to UIImage.  The advisability of doing that is debatable, but it was an interesting exercise, and it has its uses.  Problem is, I wanted the user to be able to specify what format the image should be encoded into, and at what quality (if applicable).  We can’t know for sure when the UIImage will get encoded, so we obviously have to store that information with it somehow. Continue reading
Posted in Uncategorized | Leave a comment

How to center a UIView

When centering a UIView, Apple often recommends using the “center” property. Not a bad idea, but really easy to to get wrong. The two common pitfalls in this scenario are: 1) Transforming the new center coordinates from wherever you got them to the current UIView’s coordinate system. 2) Settings an integer center coordinate can result in fractional view origins. I’m going to address the second problem first since I think it’s a little less obvious, and can have some unexpected consequences. Take a look at these two pictures and see if you can spot the difference. UIView with fractional origin values Continue reading
Posted in Uncategorized | Leave a comment

Why a missing InvalidDimensionsException ruined my day

Back in the ancient times of mobile development (about a year ago) I came across an interesting bug in Apple’s UIPickerView. Let me first say that I don’t use Interface Builder. Oooooo I know, crazy. I’ll get into that another time. Point is, I instantiate my views by hand. Well I was trying to customize a UIPickerView for the first time, and I kept getting this bizarre visual glitch. It didn’t happen every time, but after certain animations, the whole control would go weird. Here’s a screen shot of the issue.
Picker Glitch

UIPickerView Glitch

Continue reading
Posted in bugs, iPhone | Tagged , | Leave a comment

Pair object class in Objective C

It often comes up that you need to store a tuple of two objects for some reason. Key-Value pairs we call em. It was surprising to me, with Cocoa’s focus on Key-Value pair paradigms, that there doesn’t seem to be a simple object for storing such a pair. Android’s got one in android.util.Pair. Fortunately it’s a pretty easy thing to whip up. Thought I’d share it in case it helps someone out. Continue reading
Posted in iPhone | 4 Comments

Sign checking compiler option

If you’re new to iPhone development, and didn’t come from a C background, you’ve probably ignored the compiler options under your project’s “Build” tab. You may have set a Base SDK there, but probably haven’t mucked around with the rest. There are a lot of useful flags to turn on or off under the compiler options though. I won’t go into all of them at once, but I’ll try to point out useful options from time to time. In this case, the Sign Comparison option: Turning this option on means you’ll get compiler warnings for all the signed to unsigned comparisons you perform. Unless you’re doing some serious C-fu, you probably never want to do that kind of comparison. It generally leads to behavior like the following:
NSUInteger a = 1;
if (a > -1) {
    NSLog(@"WHOOPS!");
}
Even if the problem is obvious to you in that case, did you know that the C spec allows compilers to choose from a variety of data types to represent enums. If you have a negative enum member, it’ll probably choose NSInteger… if not it might choose NSUInteger. If you turn on Sign Comparison warnings, you’ll know.
Posted in iPhone, xcode | Tagged , , | Leave a comment

Intercepting Google Analytics referrer tracking on Android

NOTE – There are issues with this technique with the Android Market app v3.x.  This issue effects all tracking, Google Analytics included.  Refer here for details.
If you’re advertising commercial applications for Android, you’d probably like the ability to track ad clicks all the way through to installations and, ultimately, tie those clicks to individual users. The point is to be able to track how different ads perform. Google Analytics provides the ability to penetrate the Google App Store, and track these referrals through to the app, but the documentation assumes you’ll just let Google handle tracking those statistics for you. If you want to track your own ad referrals though, Google’s documentation on the subject is mum on the topic of exactly what the Intent that gets passed contains. They tell you to just add the following manifest snippet and be done with it: Continue reading
Posted in Android | Tagged , , | 3 Comments

Investigating touch misses on iphone

Today I was trying to figure out why an entire view controller wasn’t responding to touches. It’s a little late, I’m a little tired, and I didn’t think about the parent view being 0×0 pixels. I mean, all the child views are showing up properly, how could the parent possibly be 0×0? Duh, it’s declared as [[UIView alloc] init] in loadView. Easy fix, but if you find yourself in a similar situation, here’s how I diagnosed it. There’s no way in iOS to register some sort of global touch listener, to figure out what’s actually receiving your touches. So since our problem is we have no idea, what can we do? I ended up adopting a solution I saw here. They’re using it to resign all UIViews’ first responder status, but we can use it to log nice little traces of UIView touches using Categories. Continue reading
Posted in iPhone | Tagged , | Leave a comment

Android and iPhone localization “support”

I’ve started this blog to keep track of my experiences writing a commercial application for Android and iPhone, and I think the best place to start is internationalization. Why? Because it’s one thing that both platforms are miserable at. It’s all up-hill from here baby! If you ever end up writing an application for an international audience, at some point you are going to have to dive into localization. Maybe you’ll get lucky and only have to substitute a few strings for your users in the UK so they understand how to use their “tele” in the “lift”. Life is good for you my friend. In my case, I’m obliged to support nearly every country on Earth in a full-featured social application. So how do Android and iPhone handle this, and how can we improve it? Continue reading
Posted in Android, iPhone | Tagged , , | Leave a comment