iOS (iPhone, iPad) Tutorial on iOS Universal Applications

a universal application is an application that is designed for both iphone and ipad in a single binary. a universal application allows code reuse and fast updates.

universal application – steps involved

step 1 − create a simple view based application.

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. click next.

ios tutorial

step 4 − select the device family as ipad and click 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 application:didfinishlaunching:withoptions method in appdelegate.m as follows −

- (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;
}

step 9 − update the devices in project summary to universal as shown below −

ios tutorial

output

when we run the application, we'll get the following output −

ios tutorial

when we run the application in ipad simulator, we'll get the following output −

ios tutorial