Create a project using Xcode 5.1.1

This project is an iPhone app that displays only a blank, white screen under the iOS status bar. It will, however, print a message using the NSLog function in the Console window of Xcode.

  1. If your device (iPhone, iPad, iPod Touch) is plugged into the computer with a USB cable, unplug it. Then launch Xcode.app on your Mac. Its icon is a hammer on a blue background.
  2. The Xcode preferences I use are

    Xcode → Preferences… → Text Editing → Indentation
    Prefer indent using: Tabs
    Tab width: 4 spaces
    Indent width: 4 spaces
    Tab key: Inserts tab character

    and I uncheck the six automatic indents. You can select your own preferences.
  3. File → New → Project…
  4. In the upper left corner, under iOS, select “Application”. In the upper right corner, select “Empty Application”. (In older Xcodes, select “Window-based Application”.) In the lower right corner, press Next.
  5. The product name is the name of the project and the Macintosh folder that will hold the project. The organization name will appear in the copyright notices in the source code. The company identifier is a backwards domain name. The class prefix is the same as the project name. It will be prefixed to the names of the Objective-C classes that Xcode creates for you.

    Product Name: Project
    Organization Name: John Doe
    Company Identifier: edu.nyu.scps
    Bundle Identifier: edu.nyu.scps.Project
    Class prefix: Project
    Devices: iPhone     (for the time being)

    Do not check Use Core Data; it’s better to talk directly to the SQLite database. Press Next.

  6. Save the project on the Desktop for now. Uncheck Source Control; we will create the local git repository later. Press Create. Observe that your Desktop contains a new folder named Project, containing a file named Project.xcodeproj.
  7. In the upper left corner of Xcode, to the right of the triangle and square, select
    Project > iPhone Retina (4-inch 64-bit).
    This selects which simulator we’re going to run on.
  8. In the upper left corner of Xcode, press the Run button (a triangle). This should launch the iOS Simulator, which displays a black window that turns white after a few seconds. (Enable developer mode if it asks you.) Your app is now running on the simulator, displaying a white screen under the “Carrier” status bar. To make the simulator small enough to fit on your Mac screen, select
    Window → Scale → 50%
  9. You can run your app again by pressing the Xcode Run button again. Xcode will ask you if you first want to stop the previous instance of the app; go ahead and press Stop. Another way to run the app again is to press the simulator’s Home button. Do this by pulling down the simulator’s Hardware menu and selecting Home. You can then click on the icon for your Project app.
  10. The left pane of Xcode is called the Project Navigator and lists the folders and files of the project. If the list has disappeared,
    View → Navigators → Show Project Navigator
  11. In the Project Navigator, open the Supporting Files folder. In the Supporting Files folder, select the file main.m. In the main function in the main.m file, you should see a statement that calls the function UIApplicationMain.
    		return UIApplicationMain(argc, argv, nil, NSStringFromClass([ProjectAppDelegate class]));
    
    immediately before this statement, insert statements such as the following.
    		NSLog(@"This is output produced by NSLog.");
    
    		int i = 10;
    		NSLog(@"The value of i is %d.", i);
    
    		CGFloat f = 3.14159;
    		NSLog(@"The value of f is %g.", f);
    
    		NSString *s = @"How are you?";
    		NSLog(@"The value of s is %@.", s);
    
    		CGPoint p = CGPointMake(10.0, 20.0);
    		NSLog(@"The value of p is (%g, %g).", p.x, p.y);
    
    		CGRect r = CGRectMake(0.0, 0.0, 360.0, 480.0);
    
    		NSLog(@"The value of r is (%g, %g), %g by %g.",
    			r.origin. x, r.origin.y,
    			r.size.width, r.size.height
    		);
    
    		//There is only one object of class UIDevice, and it already
    		//exists.  Get the address of this object.
    		UIDevice *device = [UIDevice currentDevice];
    
    		NSString *version = device.systemVersion;
    		NSLog(@"The version of iOS is %@.", version);
    
    2014-06-08 14:58:51.224 Project[1080:60b] The version of iOS is 7.1.
    
    		//There is only one object of class UIScreen, and it already
    		//exists.  Get the address of this object.
    		UIScreen *screen = [UIScreen mainScreen];
    
    		CGRect bounds = [screen bounds];
    		NSLog(@"Origin is (%g, %g), dimensions are %g by %g.",
    			bounds.origin.x, bounds.origin.y,
    			bounds.size.width, bounds.size.height
    		);
    
    2014-06-08 15:00:53.834 Project[1104:60b] Origin is (0, 0), dimensions are 320 by 568.
    
    The new statement(s) must be inserted before the call to UIApplicationMain, because we never return from this call.
  12. To display the debug area at the bottom right of the Xcode window,
    View → Debug Area → Activate Console
    You may also have to press the blue left and right buttons below the Console.
  13. Run the app again. The debug area should display the output produced by NSLog. Don’t worry about the “root view controller” warning: we will create a view controller later in the course.
    2014-06-08 15:00:53.904 Project[1104:60b] Application windows are expected to have a root view controller at the end of application launch
    
    When you select
    Project > iPad Retina (64-bit)
    to the right of the Xcode Run button, the dimensions will be 768 × 1024 = 210, an aspect ration of 3:4.

Source code in Project.zip

The Project.zip file contains 16 files in 15 subdirectories, starting with the Project directory itself. Shown below are the one file that we edited (main.m) and another pair of files (ProjectAppDelegate.h and ProjectAppDelegate.m). You can save the Project.zip file on your disk. To re-open the project, double-click on Project.zip, double-click on the Project folder, and double-click on the Project.xcodeproj file. This will open the project in Xcode, and you can press the Run button.

  1. main.m. We added calls to NSLog to this file.
  2. Class ProjectAppDelegate. We did not edit these two files.

In the current Xcode and iOS, the function main in the file main.m created for you by Xcode is

int main(int argc, char *argv[])
{
	@autoreleasepool {
		return UIApplicationMain(argc, argv, nil, NSStringFromClass([ProjectAppDelegate class]));
	}
}
In older versions of Xcode and iOS, the function main in the file main.m function was
int main(int argc, char *argv[])
{
	NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
	int retVal = UIApplicationMain(argc, argv, nil, nil);
	[pool release];
	return retVal;
}

Things to Try

  1. In the method application:didFinishLaunchingWithOptions: in the file ProjectAppDelegate.m, change the window’s whiteColor to yellowColor. Then run the project again.