0

How do I access the shared delegate or the device specific "delegate" in a Universal App?

I want to store properties on the Shared delegate and put basic logic there, but if I want to do, say iPhone specific stuff on the iPhone delegate, I would assume that I need to access the two delegates separately. Is this correct?

How do I access these delegates in code?

Moshe
  • 57,511
  • 78
  • 272
  • 425

1 Answers1

4

I'm not sure what you mean by device-specific delegates. I'm assuming that by "shared delegate" you're referring to your application delegate. If you needed something specific to iPhone or iPad, you could do this:

BOOL isiPad = NO;
if ([UIDevice instancesRespondToSelector:@selector(userInterfaceIdiom)]) {
    UIUserInterfaceIdiom idiom = [[UIDevice currentDevice] userInterfaceIdiom];

    if (idiom == UIUserInterfaceIdiomPad) {
        isiPad = YES;
    }
}

if (isiPad) {
    // iPad-specific stuff
} else {
    // iPhone-specific stuff
}

That's better than using #defines because you can compile one universal app to work across all iOS devices.

EDIT: Added some introspection to prevent this from crashing on iPhone OS 3.1.x and earlier. Thanks, Bastian.

Jeff Kelley
  • 19,021
  • 6
  • 70
  • 80
  • When upgrading an older project for iPad, XCode creates a shared delegate. I assumed that a new Universal application does the same. Apparently not. My mistake. – Moshe Jun 18 '10 at 19:10
  • Please note that this will crash on 3.1.x devices because userInterfaceIdiom don't exists there. You would have to check first if currentDevice has this selector. – Bastian Aug 05 '10 at 06:21
  • I just checked again and there is a define that you can use already defined in UIDevive.h: #define UI_USER_INTERFACE_IDIOM() ([[UIDevice currentDevice] respondsToSelector:@selector(userInterfaceIdiom)] ? [[UIDevice currentDevice] userInterfaceIdiom] : UIUserInterfaceIdiomPhone) – Bastian Aug 05 '10 at 06:29
  • Thanks, Bastian. I've updated the code, but I'll leave out the macro to give a better sense of what's going on. – Jeff Kelley Aug 05 '10 at 14:42