tags:

views:

132

answers:

2

I am using local notifications in a app I'm making.

I use this:

Class myClass = NSClassFromString(@"UILocalNotification");
if (myClass) {
//Local Notification code
}

To avoid using UILocalNotifications when not supported.

But my app crashes on launch with this error code:

warning: Unable to read symbols for "/Library/MobileSubstrate/MobileSubstrate.dylib" (file not found). dyld: Symbol not found: _OBJC_CLASS_$_UILocalNotification Referenced from: /var/mobile/Applications/FCFFFCB2-A60B-4A8D-B19B-C3F5DE93DAD2/MyApp.app/MyApp Expected in: /System/Library/Frameworks/UIKit.framework/UIKit

Data Formatters temporarily unavailable, will re-try after a 'continue'. (Not safe to call dlopen at this time.) mi_cmd_stack_list_frames: Not enough frames in stack. mi_cmd_stack_list_frames: Not enough frames in stack.

How can i prevent this?

A: 

Never mind.. I used:

Class myClass = NSClassFromString(@"UILocalNotification");
if (myClass) {
   //Local Notification code
   UILocalNotification *notification = [[myClass alloc] init];
}

instead of:

Class myClass = NSClassFromString(@"UILocalNotification");
if (myClass) {
  //Local Notification code
  UILocalNotification *notification = [[UILocalNotification alloc] init];
}

and it worked!

Edited to clearify!

Larsaronen
It works on iOS4 but it will fail on older versions. See my answer.
St3fan
But on older devices, the IF statement will be false, so the code won't be executed anyway. Or is this not the case?
Tom Irving
Sorry should have been more clear but this is inside the:Class localNotificationClass = NSClassFromString(@"UILocalNotification");if (localNotificationClass != nil) {block..So im basically doing the same as you st3fan. @Tom Irving although it is not executed on older devices the [[UILocalNotification alloc] init]; part makes it crash..
Larsaronen
You can't do that kind of alloc because like St3fan said, it'll cause linker errors. Instead do [localNotificationClass alloc]
Jorge Israel Peña
+3  A: 

The proper way to do this is:

Class localNotificationClass = NSClassFromString(@"UILocalNotification");
if (localNotificationClass != nil) {
    // The UILocalNotification class is available
    UILocalNotification* localNotification =
        [[localNotificationClass alloc] initWith....];
}

You can not use [UILocalNotificationClass alloc] because that will cause Link Errors when your code is loaded on an older iOS where the class is not available. And that is exactly what you are trying to prevent.

BTW: Those MobileSubstrate errors are what you get when you jailbreak your phone: an unpredictable/undefined development platform :-)

St3fan
Hehe OK. Cant wait to replace my 2g with iPhone 4. No need for sim unlocking and jailbreaking.
Larsaronen