TECHNIQUE 2 — CREATING UNIVERSAL APPLICATIONS

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

image

  1. Using Xcode, create a Single View Application project and name it MyUniversalApp. Ensure that you select Universal for the Device Family (see Figure 5-10). You will also use the project name as the Class Prefix. Ensure that the Use Automatic Reference Counting option is unchecked.

    image

    FIGURE 5-10

  2. Observe that you now have two XIB files (see Figure 5-11) in your project.

    image

    FIGURE 5-11

  3. Select the MyUniversalAppViewController_iPhone.xib file to edit it in Interface Builder. 3. Add a Label view to the View window and label it as shown in Figure 5-12.

    image

    FIGURE 5-12

  4. Select the MyUniversalAppViewController_iPad.xib file to edit it in Interface Builder. Add a Label view to the middle of the View window and label it as shown in Figure 5-13.

    image

    FIGURE 5-13

  5. Press Command-R to test the application on the iPhone Simulator, first using the iPhone 5.0 Simulator scheme, followed by the iPad 5.0 Simulator scheme. You will see the application running on the iPhone Simulator as an iPhone app (see Figure 5-14) and as an iPad app (see Figure 5-15).

    image

    FIGURE 5-14

    image

    FIGURE 5-15

How It Works

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.

image

FIGURE 5-16

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.222.184.122