Search code examples
iphoneobjective-cxcodeipad

Universal iPhone/iPad AppDelegate


I was wondering if anyone would be able to tell me, starting a new app from scratch, how would I organise having a universal (iPhone/iPad) iOS app. I noticed using the default template with the Xcode Beta, it gives you both a shared AppDelegate and subclassed appdelegates for iPhone and iPad.

Now, how do you put in the logic to determine which app delegate to use, how does it know which one to instantiate and work with as the default template doesn't state. If I were to write in the iPhone appDelegate, how do I know that this will only run for the iPhone iOS for example?


Solution

  • Use following code

    id<UIApplicationDelegate> delegate = [[UIApplication sharedApplication] delegate]; 
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
        AppDelegate_iPad *appDelegate = (AppDelegate_iPad *) delegate;
    else
        AppDelegate_iPhone *appDelegate = (AppDelegate_iPhone *) delegate;
    

    whenever you are required to access to the delegate.