Search code examples
iphoneipaddelegatesuniversal-binary

Accessing Shared Delegate verus iPhone/iPad delegate?


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?


Solution

  • 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.