Source Code
INFO1-CE9236 iPhone and iPad Apps
INFO1-CE9704 Advanced iPhone and iPad Apps

  1. Documentation and portals

Hello, world!

An app must be written in the language Objective-C. You type the app into Xcode, an application which runs on a Mac. Test the app in the iPhone simulator, which also runs on the Mac. Debug the app in the Xcode debugger. Then download the app to your iPhone, iPad, or iPod Touch.

  1. The Objective-C language is a superset of C.
  2. Project: create an Xcode project that displays debugging output in the Console window.
  3. Class: create a class of objects. Instance variables, methods, properties.
  4. Memory management. We will skip these rules because we now check the checkbox for “use Automatic Reference Counting” when we create a project. This topic is of interest only if you have to maintain very old code (circa 2010).
  5. Collections I: a dictionary has two parallel columns of objects. Used in Hello, Attributes, Switch, and Segmented.
  6. Hello: the “Hello, World!” app prints “Hello, World!”. You can install your own font.
  7. GitHub: a website to which you can upload your .zip files.
  8. Debug your app with the Xcode debugger. Set breakpoints, print variables.
  9. Snapshot of your app.
  10. Icon for your app.
  11. Internationalize your app.
  12. Download your app to your iPhone, iPad, or iPod Touch.
  13. Zip: compress and decompress an Xcode project with zip and unzip. You don’t have to do this for this course, but it’s something you might need to know.
  14. Summary of the rules for writing an app, including Things I Often Forget.

Still-life 2D graphics

We printed a line of text in the drawRect: method of class UIView in the Hello app. Now let’s draw a picture.

  1. Japan: a simple still life.
  2. Manhattan: an outline.
  3. America: draw and fill a rectangle with CGContextAddRect. Draw an image with UIImage drawAtPoint:.
  4. Sine: graph the sine function, with a pattern in the background.

Collections, touches, and animations

  1. Collections II: a set is a big object that contains (pointers to) little objects.
  2. Touch: detect a touch, move a hockey puck. The methods touchesBegan:withEvent:, touchesMoved:withEvent:, touchesEnded:withEvent: receive an NSSet containing UITouch objects.
  3. Animate the response to the touch. Make the little view move under its own power.
  4. Label: class UILabel displays a line of text.
  5. Gone: animate the center property of a UILabel contained in a UIView.

Graphics that respond to a touch

  1. Etch: an Etch a Sketch using the CGMutablePathRef we saw in the isoceles right triangle example.
  2. Hit: which of several subviews were touched?
  3. Collections III: an array is a big object that contains (pointers to) little objects. Unlike the set in Collections II, an array keeps the pointers in their original order. Needed for Flip.
  4. Flip: transition between two views in response to a swipe. Although this example has no view controller, it belongs thematically in the section “View controlers that manage other view controllers” below. I put it here because everybody wants to transition between views as early as possible in the course.
  5. Puzzle: classic puzzle of 8 movable tiles in a 3 × 3 square with one missing.

Gesture Recognition

See Gesture Recognizers, the superclass UIGestureRecognizer, and Interactivity and Feedback.

  1. UITextView: a class that displays multiple lines of text.
  2. Swipe: recognize a swipe with a UISwipeGestureRecognizer. It has the property direction.
  3. Pinch: recognize a pinch or spread with a UIPinchGestureRecognizer.
  4. Tap: recognize a single or double (or triple) tap with a UITapGestureRecognizer. It has the property numberOfTapsRequired and the method requireGestureRecognizerToFail:.
  5. Other gesture recognizers, e.g. the two-fingered rotation gesture recognizer.

Read and write a file

Media files are stored in the app’s “bundle” in the iPhone’s flash memory. We will have to find the directory that holds the bundle and the files in the directory.

  1. Filenames: see the names of all the directories and files that the app is allowed to read and/or write in the iPhone.
  2. Attributes: see the attributes of the files.
  3. Use the bundle to find a file with a given name. See how we found the file gone.mp3 in Gone.
  4. Apps that create files and write to them: Segmented (a sound file), Smoky (a text file), Base (a database file).
  5. Read and write a file with an NSData object.
  6. App Preferences set in the Settings app.

A control and the target/action pattern;
an object and its delegate

Some controls (button, page control, slider, text field) allow you to specify their size. Other controls (switch, segmented control, date picker) have a preferred size: you can set their position but not their size. The only way to make them bigger or smaller is by putting a scaling factor in their transform property.

  1. Differences between targets and delegates are listed in the rules.
  2. Button, the simplest control. When the button is pressed, it can NSLog a message, vibrate the iPhone, or play an audio file.
  3. Notification center: needed for video.
  4. Video: play a video file when the button is pressed.
  5. Switch and AVAudioPlayer. The switch has a target; the audio player has a delegate.
  6. PageControl: a row of dots.
  7. Slider
  8. Segmented: a row of buttons. Classes AVAudioSession, AVAudioRecorder, AVAudioPlayer.
  9. DatePicker and its date formatter and calendar: pick a date or time.
  10. UITextField and its delegate: input one line of text.

View controllers that manage a view

Every view controller class is a subclass of UIViewController. MVC stands for model-view-controller.

  1. ViewController: a portrait/landscape view controller.
  2. Pong: animation with CADisplayLink.
  3. Pearls on an elastic string.

View controllers that manage other view controllers

Every view controller has a black tab bar item and a brushed aluminum navigation item. The tab bar item becomes visible only when the view controller is under a tab bar controller, and the navigation item becomes visible only when the view controller is under a navigation controller. The three above examples had no tab bar controller or navigation controller (except for a brief presentation in the portrait/landscape view controller).

  1. Tab bar controller for a radio interface. The Five Boroughs.
  2. five student projects controlled by one tab bar controller.
  3. Pastoral: a tab bar controller. Each view controller underneath it turns its sound on with viewDidAppear: and off with viewWillDisappear:.
  4. Navigate: each successive view enters from the right. Make a stack of train stations.
  5. Modal controller: visit a controller temporarily, and then return to the previous controller. The temporary controller enters from the bottom.
  6. LTrain: move horizontally with a navigation controller, vertically with a modal view controller. Float like a butterfly, sting like a bee. layoutSubviews.
  7. Cover flow: a parade of album covers.
  8. Split: a UISplitViewController for iPad has two view controllers underneath it, master and detail.
  9. Popover: same as the above example, except that the master view is relegated to a popover in portrait orientation. The detail view gets a toolbar.

View controllers that manage a special type of view

  1. Orson: a view controller above a MPMoviePlayerController (which we saw here) must be a MPMoviePlayerViewController. Also demonstrates UITabBarControllerDelegate.
  2. A view controller above a UITableView must be a UITableViewController (which we will see here).
  3. UIImagePickerController will come here.


  1. Board: use a storyboard to create an app.
  2. Storyboarding (Single View Application)
  3. Converting to StoryBoards
  4. Your First iOS App
  5. Your Second iOS App: Storyboards

View classes

Every view class is a subclass of UIView. Some views are still lifes: UILabel, UIImageView, and UIWebView. Some display changing output: UIActivityIndicatorView, UIProgressView. Some demand input: UIAlertView, UIActionSheet, and UIPickerView. Some perform both input and output: UITextView and UITableView.

  1. UILabel was used in Label (stationary) and Gone (animated).
  2. Smoky: a UITextView that we can edit, and its delegate. Write the text to a file.
  3. UIImageView holds a UIImage, as UILabel holds an NSString. See Pinch, Puzzle, Orson.
  4. Activity indicator when you don’t know what fraction of the activity has already been completed. NSTimer, too.
  5. Progress view when you do know what fraction of the activity has already been completed.
  6. Review enumerations before UIAlertView and UIActionSheet.
  7. Alert: UIAlertView, UIActionSheet, and their delegates. Terminate the application and launch another one.
  8. Collections VI: a two-dimensional array. Will be used in the UIPickerView examples.
  9. Picker: a UIPickerView has rotating drums like a Las Vegas slot machine. UIPickerViewDataSource, UIPickerViewDelegate.
  10. Plot: a silly example of a UIPickerView. The Official Movie Plot Generator by the Brothers Heimberg: Justin and Jason.
  11. Search Bar and its delegate.
  12. The view class MKMapView will be covered down in the maps section.


A UITableView is a menu of UITableViewCells. See Table View Programming Guide for iPhone OS. The Android equivalent is ListView.

  1. See a table view divided into sections in the Settings app.
    Settings → General.
  2. An NSIndexPath contains a pair of numbers: section and row.
  3. States: provide the UITableView with a table view controller, which will also act as the table view data source and the table view delegate.
  4. Section: divide the table view into sections. Each section can have a title.
  5. Goner: delete a row from a UITableView.
  6. Reorder the rows in a UITableView.
  7. Correct the spelling errors in a UITableView.
  8. Insert: insert, delete, reorder.
  9. Tree: a tree of data. A UINavigationController on top of a stack of UITableViewControllers.
  10. Make a parody of the Settings app.


Class UIWebView and protocol UIWebViewDelegate. The HIG.

  1. Sample page of HTML (HyperText Markup Language). See the actual HTML by pulling down View Source in your browser. Get the hexadecimal code numbers for the special characters from the Unicode code charts.
  2. HTML 4 reference: this is the version of HTML that your instructor uses. Click on elements at the top of the page.
  3. Display a page of HTML. The page can come from the web, or from the app bundle, or it can be hardcoded into the app.
  4. Generate the page of HTML dynamically with a for loop.
  5. JavaScript program to display a Google map in the page of HTML. Wait until the UIWebViewDelegate says that the page is fully loaded before trying to execute the JavaScript.
  6. HTML5: draw the outline of Manhattan.

Maps, Motion, and Momentum

  1. Map: get the latitude and longitude from a CLLocationManager. Give the latitude and longitude to a MKMapView, which displays a Google map without using JavaScript, and to a CLGeocoder, which translates the latitude and longitude into a street address. Display the street address in an MKAnnotation. Annotations and overlays.

  2. Examples after this point need to be redone for the current versions of iOS and Xcode.
  3. Circle of uncertainty
  4. Orientation: device orientation vs. interface orientation
  5. Accelerometer: gravity vs. acceleration.
  6. Heading: which way are we facing? Magnetic north vs. true north.
  7. Motion events, analogous to touch events.
  8. Attitude: the CMMotionManager uses accelerometer and gyroscope data to compute the device’s attitude.
  9. Proximity: is the iPhone near the user’s head?

SQLite databases

SQL stands for Structed Query Language. It comes in several flavors: MySQL, SQLite, etc. An SQLite database can be stored in the iPone and read and written by an app.

  1. Play with sqlite using the interactive shell in the Macintosh Terminal window.
  2. A database with two tables and a primary key. Database join.
  3. Base: an app that creates, writes, and reads a database.
  4. the previous example, as a Universal app on iOS 4.3, with the UITextView or UIWebView replaced by a UITableView.
  5. Zip: a database with zipcode, city, state, latitude, longitude, county.
  6. Distance: find all zipcodes within 5 miles of a given zipcode.
  7. Blob: “binary large object” such as an image file.

Core Data

  1. Core Data: write and read a few records.
  2. Tutorial for Core Data. The project in the Core Data Tutorial for iOS.

Core Animation

  1. Pythagoras1: animate a view by calling drawRect:.
  2. A layer with its own delegate. The above example, but redraw only the part of the picture that changes.
  3. Meetup notes.
  4. Torch: add an animation to a layer.
  5. Series: execute a series of animation blocks, one after another.
  6. Frames: an animation made of a series of frames.

OpenGL ES graphics: geometry, perspective, and motion

This section has only enough lighting and color to show us what’s going on.

  1. Intro.
  2. The OpenGL ES Template in Xcode contains two-dimensional code for OpenGL ES 1.1 and 2.0. Build a square out of two triangles.
  3. OpenGL in Xcode. This is a straight C++ program that runs on the Mac. It is not an iPhone app.
  4. OpenGL on iPhone. The same project as the one that ran on a Mac, but now it runs on an iPhone.
  5. Pythagoras2: animate a 2-D line drawing.
  6. Three dimensions, with light and shade.
  7. Matrix stack: manipulate a group of objects as a unit.
  8. Camera: think of the observer as a movable camera.
  9. Texture: print an image on a triangle.
  10. Cube: build a cube out of six squares in OpenGL ES 1.1 and 2.0.
  11. Touch and move a three-dimensional object.
  12. Color
  13. Build the Platonic solids out of equilateral triangles: tetrahedron, octahedron, icosahedron. Build a sphere out of many small triangles. Optimizations for drawing many triangles: GL_TRIANGLE_STRIP, glDrawElements instead of glDrawArrays.
  14. SphereWorld: the example from the OpenGL SuperBible.
  15. Viewports: orthogonal with glOrthof, perspective with glFrustumf. Field of view.
  16. A linear transformation can be represented by a 3 × 3 matrix. Since translation is not linear, we have to use a 4 × 4 matrix. Translate (move), scale (make bigger and smaller), rotate (taking care to avoid gimbal lock; quaternions), project (flatten 3D to 2D), shear, identity (make no change). Combine with matrix multiplication; vectorize the multiplication with the VFP math library. Represent a point as a 1 × 3 matrix, a vector as a 3 × 1 matrix. The modelview matrix and the projection matrix: glMatrixMode(GL_MODELVIEW).
  17. Interleave the position, normal, and color data for each vertex.
  18. Animation by interpolation between key frames. Spherical and normalized linear interpolation.
  19. Quaternions as an alternative to rotation matrices: avoid gimbal lock. Convert between a quaternion and other representations of a rotation.

OpenGL ES graphics: lighting, color, and texture

  1. Enable color with glEnableClientState(GL_COLOR_ARRAY).
  2. Shade models: GL_FLAT and GL_SMOOTH.
  3. Lighting: glEnable(GL_LIGHTING). The three components of a light source: specular, diffuse, ambient. Spotlights with GL_SPOT_DIRECTION, GL_SPOT_CUTOFF. Vertex normals: GL_NORMAL_ARRAY, glNormalPointer.
  4. Materials: front or back, specular, diffuse, ambient. GL_SHININESS. Objects that glow in the dark with GL_EMISSION.
  5. Texture mapping. Specify the blending function with glBlendFunc. Generate names (actually ID numbers) for the textures. Bind the textures with glBindTexture; specify linear scaling. Load a texture from a UIImage object or a PVRCT file. Pad the image to satisfy the aspect ratio requirements. Specify the texture coördinates of each vertex by mapping the ST axes onto the UV axes; make sure the picture isn’t upside down. Tile a texture: GL_REPEAT is better than GL_CLAMP_TO_EDGE.


  1. Client: a client that talks to a server hosted on
  2. Put: upload a file from the client (the app) to the server ( using HTTP PUT.


  1. Camera: get an image or movie from the camera, photo albums, or saved photos.


  1. Bluetooth

Three technologies for playing audio

  1. AudioServices for a short sound. Our example was Button.
  2. AV FoundationFramework, including class AVAudioPlayer, for background music. Our examples were Gone and Switch.
  3. queueplay
  4. Queue: Audio Queue for sound that can be synchronized with graphics.

C++ and Objective C

  1. Peaceable Kingdom: an Objective-C object and a C++ object side by side.

Things that need to be changed

  1. JavaScript: return elevation of point at center of map. Elevation callback is never called.
  2. Correct: works correctly, even though we don’t get font size, label frame as in Insert.
  3. @property creates an instance variable with the same name as the property, but with a leading underscore. Should all instance variables have leading underscores? Don’t need @synthesize any more. App delegate doesn’t need _window instance variable any more.
  4. TextField: what difference does the return value of textFieldShouldReturn: make?
  5. Tree: Model.h has information.
  6. HTML: You must be born after this date in 1994 in order to purchase…