iPhone Dev Tip #3: Using New Symbols While Preserving Backwards Compatibility
August 25, 2010
Recently, I was presented with an issue with backwards compatibility: we want to use the new UILocalNotifications API but still preserve compatibility with 3.x devices. I haven't really had a problem doing this with objects that existed in 3.x (check for respondsToSelector:) but I was presented with a new problem: UILocalNotification doesn't exist in 3.x! When you try to run the app, you get something like this:
dyld: Symbol not found: _OBJC_CLASS_$_UILocalNotification
Referenced from: /var/mobile/Applications/3BB86986-88EB-4AF5-9FCF-12C7040889F5/MyApp.app/MyApp
Expected in: /System/Library/Frameworks/UIKit.framework/UIKit
in /var/mobile/Applications/3BB86986-88EB-4AF5-9FCF-12C7040889F5/MyApp.app/MyApp
Not too surprising that it didn't work! So how did I fix it? Basically you have to treat it like an id and instantiate it with a Class queried at runtime:
id localNotif = [[NSClassFromString(@"UILocalNotification") alloc] init];
You cannot use properties (of course) but you can operate on the notification object just like you would normally. Pretty simple solution to a complex linking problem!