RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


iPad Programming: Port Your iPhone Apps, Part I

Now that Apple has lifted the NDA on the iPhone 3.2 SDK, it's time for developers to start developing iPad applications.

Now that Apple has started selling the iPad and the NDA lifted on the iPhone 3.2 SDK, it is time for developers to start developing iPad applications. And for a large number of developers, developing iPad applications mean porting their existing applications to the iPad platform. Though the iPad is also running the iPhone OS, there are some subtle differences that developers need to look out for when porting their applications over to the new device. This article examines the various ways in which you can port your existing iPhone apps to the iPad.

Method 1: Supporting Both the iPhone and iPad Platforms

Out of the box, the iPad will run your existing iPhone apps using the same screen size that is available on the iPhone and iPod touch -- 320 x 480 pixels. Your applications will utilize only a portion of the screen. However, applications running in this mode do not do justice to the much bigger screen estate afforded by the iPad. Clearly, this is an interim solution that Apple is adopting in order to buy time for developers to port their applications UI to the much bigger iPad screen.

The easiest way for you to ensure that your iPhone applications run as an iPad application (that is, full screen) is to modify the device target setting in your Xcode application. The following steps show you how.

Using Xcode, create a new View-based Application (iPhone) project (see Figure 1).

Figure 1. Creating a View-based Application for iPhone.

Name the project MyiPhoneApp. Double-click on the MyiPhoneAppViewController.xib file to edit it in Interface Builder. Populate the View window with the following views (see also Figure 2):

* Label
* TextField
* Button

Figure 2. Populating the View window with views.

Back in Xcode, press Command-R to test the application on the iPhone Simulator. You should see the screen as shown in Figure 3.

Figure 3. Running the application on the iPhone Simulator.

In Xcode, change the simulator to Simulator - 3.2 | Debug and press Command-R again. This time, the application will be shown running in the iPad Simulator, running as an iPhone application (see Figure 4). This is the default behavior of iPhone applications running on the iPad.

Figure 4. Running the application on the iPad Simulator.

Next, in Xcode, expand on the Targets item and select the MyiPhoneApp item. Click the Info (i) button (located at the top of the toolbar) to display its Info page (see Figure 5). Click on the Build tab and scroll down the list. Under the Deployment section, select "iPhone/iPad" for the Targeted Device Family setting.

Figure 5. Changing the Targeted Device Family setting of the project to iPhone/iPad.

Press Command-R to test the application on the simulator again. This time, your application will run natively as an iPad application (see Figure 6).

Figure 6. Running the application as an iPad application.

Notice that the UI of the application is exactly the same as that on the iPhone. It is your responsibility to repurpose your UI when the application is running on the iPad. You would need to programmatically realign your views when your application detects that it is running on an iPad.

Detecting the Platform Programmatically

In order to repurpose your UI depending on the device it is running on, it is important to be able to programmatically detect if your application is running as an iPhone/iPod touch or iPad application. The following code shows you how.

In the MyiPhoneAppViewController.m file, code the following:

- (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"
    [alert show];
    [alert release];     

    [super viewDidLoad];
The above code includes a conditional compilation directive to indicate that if the application is compiled against the minimum iPhone OS version of 3.2, then it will include a block of code to programmatically detect the type of application it is currently running as.

Press Command-R to test the application on the Simulator. Observe the message displayed as shown in Figure 7.

Figure 7. Displaying the platform it is running as.

If you now go back to Xcode and choose the Simulator - 3.1.3 | Debug item and press Command-R again, the iPhone Simulator will appear and there will be no alert displayed. This is because the application will now be compiled with the iPhone OS 3.1.3 target and hence the compiler will omit the block of code enclosed using the #if-#endif block.

Next, back in Xcode select "iPhone" for the Targeted Device Family setting (as shown in the previous section) and choose the Simulator - 3.2 | Debug item. Press Command-R and you will notice that the application now runs on the iPad Simulator as an iPhone application. Figure 8 shows the alert that it will display.

Figure 8. Running as an iPhone application on the iPad Simulator

Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date