The previous technique shows how you can modify the Targeted Device Family setting to create a single application that runs on both the iPhone and the iPad, called a Universal application. The challenge is adapting the UI of the application for each platform — you have to programmatically detect the type of device the application is running on and then modify the layout of the UI dynamically.
Apple recommends that you create a Universal application, one that targets both the iPhone and the iPad, with separate XIB files representing the UI for each platform. The following Try It Out demonstrates how you can create a Universal application.
TRY IT OUT: Creating a Universal Application
This has been a very straightforward exercise. First, you created a Universal application using Xcode. When you create a Universal application project, Xcode automatically creates two XIB files for you - one for iPhone and one for iPad. When the application is loaded, it automatically detects the platform on which it is running. This is evident in the application delegate:
- (BOOL) application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease]; // Override point for customization after application launch. if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) { self.viewController = [[[MyUniversalAppViewController alloc] initWithNibName:@“MyUniversalAppViewController_iPhone” bundle:nil] autorelease]; } else { self.viewController = [[[MyUniversalAppViewController alloc] initWithNibName:@“MyUniversalAppViewController_iPad” bundle:nil] autorelease]; } self.window.rootViewController = self.viewController; [self.window makeKeyAndVisible]; return YES; }
If the application is running on the iPhone, the MyUniversalAppViewController will be loaded using the MyUniversalAppViewController_iPhone.xib file. If it is running on the iPad, then the same View Controller will load the MyUniversalAppViewController_iPad.xib file. Note that in this case, you have two different XIB files, and only one View Controller for the two XIB files. The important thing to keep in mind about a Universal application is that you need to create separate XIB files for the different platforms — one for the iPhone and one for the iPad. Once you do that, you can then load the appropriate XIB files during runtime. Using this approach, you have only one executable for your application.
It is worth pointing out that the MyUniversalApp-Info.plist file now has one additional key: Supported interface orientations (iPad). The project will use this key (see Figure 5-16) to set the supported interface orientation for the application when it is run as an iPad app.
18.222.184.122