Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

iOS Universal Applications

IOS Universal Applications


Introduction

A universal application is one designed for both iPhone and iPad in a single binary file. This helps code reuse and enables faster updates.

Example Steps

1. Create a simple View based application

2. In the file inspector on the right, rename the file ViewController.xib to ViewController_iPhone.xib, as shown below

UniversalAppInterfaceRename

3. Select File -> New -> File..., then choose User Interface, then View, and click Next

NewIpadXib

4. Select iPad as the device and click Next:

UniversalAppSelectDeviceType

5. Save the file as ViewController_iPad.xib, then select Create

6. Add a label to the center of the screen in ViewController_iPhone.xib and ViewController_iPad.xibd

7. In ViewController_iPhone.xib, select the identity inspector and set the custom class to ViewController

UniversalAppSetClass

8. Update the application:DidFinishLaunching:withOptions method in AppDelegate.m



- (BOOL)application:(UIApplication *)application

  didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

{

   self.window = [[UIWindow alloc] initWithFrame:[[UIScreen 

   mainScreen] bounds]];

   // Override point for customization after application launch.

   if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {

        self.viewController = [[ViewController alloc] 

        initWithNibName:@"ViewController_iPhone" bundle:nil];

   }

   else{

        self.viewController = [[ViewController alloc] initWithNibName:

        @"ViewController_iPad" bundle:nil];

   }

   self.window.rootViewController = self.viewController;

   [self.window makeKeyAndVisible];

   return YES;

}

9. In the project summary, update the device to universal, as shown below:

UniversalAppSetDevices

Output

Run the application and we will see the following output

UniversalAppiPhone_Output

Run the application in the iPad simulator and we get the following output:

UniversalAppiPad_Output