views:

143

answers:

3

Hi everyone, 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 doesnt 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?

A: 

I am doing the same thing at the moment and I learned a lot from the following tutorial:

http://iphonedevelopment.blogspot.com/2010/04/converting-iphone-apps-to-universal.html

This has helped me out to setup the project to support iPad and iPhone devices.

Wim Haanstra
+3  A: 

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.

Sagar
+1  A: 

The Info.plist ("Main nib file base name") for an app specifies a main window .xib file to load, and that .xib file specifies an app delegate and root window controller for the app.

The Info.plist for an iPad ("NSMainNibFile~ipad") additionally specifies a main window .xib file for the iPad, which can contain a different app delegate and a different root window controller for the app. Or they could be the same ones, using runtime UIUserInterfaceIdiomPad checks.

The runtime, which knows what device type on which it's running, picks the correct initial .xib file to load from the info.plist, and from there you get the correct app delegate and root window controller configured and running.

hotpaw2