English 中文(简体)
iOS - Universal Applications
  • 时间:2024-09-17

iOS - Universal Apppcations


Previous Page Next Page  

A universal apppcation is an apppcation that is designed for both iPhone and iPad in a single binary. A universal apppcation allows code reuse and fast updates.

Universal Apppcation – Steps Involved

Step 1 − Create a simple View based apppcation.

Step 2 − Change the File name ViewController.xib file to ViewController_iPhone.xib as shown below in the file inspector in the right hand side.

iOS Tutorial

Step 3 − Select File → New → File... then select the subsection "User Interface" and select View. Cpck Next.

iOS Tutorial

Step 4 − Select the device family as iPad and cpck next.

iOS Tutorial

Step 5 − Save the file as ViewController_iPad.xib and select Create.

Step 6 − Add a label in the center of the screen in both ViewController_iPhone.xib and ViewController_iPad.xib.

Step 7 − In ViewController_iPad.xib, select the identity inspector and set the custom class as ViewController.

iOS Tutorial

Step 8 − Update the apppcation:DidFinishLaunching:withOptions method in AppDelegate.m as follows −

- (BOOL)apppcation:(UIApppcation *)apppcation
   didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
   self.window = [[UIWindow alloc] initWithFrame:[[UIScreen 
   mainScreen] bounds]];
   
   // Override point for customization after apppcation 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;
}

Step 9 − Update the devices in project summary to Universal as shown below −

iOS Tutorial

Output

When we run the apppcation, we ll get the following output −

iOS Tutorial

When we run the apppcation in iPad simulator, we ll get the following output −

iOS Tutorial Advertisements