TECHNIQUE 1 — MODIFYING THE DEVICE TARGET SETTING

The easiest way to ensure that your iPhone application runs as an iPad application (that is, full screen) is to modify the Targeted Device Family setting in your Xcode project. The following Try It Out shows you how to achieve this.

TRY IT OUT: Modifying the Device Target Setting

image

  1. Using Xcode, create a new Single View Application (iPhone) project and name it MyiPhoneApp. (You will also use the project name as the Class Prefix.) Ensure that you have the Use Automatic Reference Counting option unchecked.
  2. Select the MyiPhoneAppViewController.xib file to edit it in Interface Builder.
  3. Populate the View window with the following views (see Figure 5-1):
    • Label (set it to display “Please enter your name”)
    • Text field
    • Round Rect button (set it to display “OK”)

    image

    FIGURE 5-1

  4. Back in Xcode, press Command-R to test the application on the iPhone Simulator. You should see the screen shown in Figure 5-2.
  5. At the top-left corner of the Xcode window, select the iPad 5.0 Simulator scheme (see Figure 5-3).

    image

    FIGURE 5-2

    image

    FIGURE 5-3

  6. Press Command-R again. This time, the application will be shown running in the iPhone Simulator (simulating the iPad), running as an iPhone application (see Figure 5-4). This is the default behavior of iPhone applications running on the iPad.

    image

    FIGURE 5-4

  7. In Xcode, select the MyiPhoneApp project name. In the Summary tab, change the Devices option to Universal (see Figure 5-5). If you click the Build Settings tab now, you will see that the Targeted Device Family setting is now set to iPhone/iPad (see Figure 5-6).

    image

    FIGURE 5-5

    image

    FIGURE 5-6

  8. Press Command-R to test the application on the iPhone Simulator (with the iPad 5.0 Simulator scheme selected) again. This time, your application will run natively as an iPad application — that is, full screen (see Figure 5-7).

    image

    FIGURE 5-7

How It Works

In this example, you first created an iPhone application that you then tested on the iPhone Simulator, simulating both the iPhone and the iPad. By default, all iPhone applications run in their original screen size — 320 × 480 pixels. If you want your iPhone application to run full screen on the iPad, you have to modify the Targeted Device Family setting in your project.

The Targeted Device Family setting provides three different values: iPhone, iPad, or iPhone/iPad. Setting it to iPhone/iPad ensures that your application can automatically detect the device on which it is running, and runs your application full screen.

Notice that the UI of the application is exactly the same as that on the iPhone. It is your responsibility to re-layout your UI when the application is running on the iPad. One way would be to programmatically reposition your views when your application detects that it is running on an iPad. Another way would be to use the Size Inspector window to set the Autosize property of each view on the View window to anchor the view to the edges of the screen. The next section describes how to detect the device on which an application is currently running.

Detecting the Platform Programmatically

In order to re-layout your UI according to the device on which it is running, it is useful to be able to programmatically detect if your application is running on an iPhone/iPod touch or an iPad. The following Try It Out shows you how.

TRY IT OUT: Detecting the Device

  1. Using the project created in the previous section, add the following statements shown in bold to the MyiPhoneAppViewController.m file:
    - (void)viewDidLoad
    {
    #if (__IPHONE_OS_VERSION_MAX_ALLOWED >= 30200)
    
        NSString *str;
        if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
            str = [NSString stringWithString:@“Running as an iPad application”];
        } else {
            str = [NSString stringWithString:
                   @“Running as an iPhone/iPod touch application”];
        }
    
        UIAlertView *alert =
            [[UIAlertView alloc] initWithTitle:@“Platform”
                                       message:str
                                      delegate:nil
                             cancelButtonTitle:@“OK”
                             otherButtonTitles:nil];
           [alert show];
           [alert release];
    
       #endif
    
           [super viewDidLoad];
       }
  2. In Xcode, choose the iPhone 5.0 Simulator scheme and press Command-R to test the application on the iPhone Simulator. You will see the message displayed in Figure 5-8.

    image

    FIGURE 5-8

  3. In Xcode, choose the iPad 5.0 Simulator scheme and press Command-R to test the application on the iPhone Simulator. You will see the message displayed in Figure 5-9.

    image

    FIGURE 5-9

How It Works

The preceding code includes a conditional compilation directive to indicate that if the application is compiled against the minimum iOS version of 3.2, then it will include a block of code to programmatically detect the type of application it is currently running as:

#if (__IPHONE_OS_VERSION_MAX_ALLOWED >= 30200)

//---code within this block will be compiled if application is compiled
// for iPhone OS 3.2 and above---

#endif

To detect if the application is running on an iPad, you check the result of the UI_USER_INTERFACE_IDIOM( ) function. This function returns the interface idiom supported by the current device. If it is an iPad, then the result of this function will be UIUserInterfaceIdiomPad:

NSString *str;
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
    str = [NSString stringWithString:@“Running as an iPad application”];
} else {
    str = [NSString stringWithString:
           @“Running as an iPhone/iPod touch application”];

If the application is running as an iPhone application (that is, not full screen) on the iPad, the UI_USER_INTERFACE_IDIOM() function will return UIUserInterfaceIdiomPhone.

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

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