Creating iOS Apps in Swift
http://markmeretzky.com/swift/
mark.meretzky@gmail.com

In Fall 2018 at NYU SPS, this course is iOS INFO1-CE9913.

Setup

As of March 22, 2024, the current non-beta version of Xcode is 15.3. It is available free in the Mac App Store but requires a Macintosh running at least version 10.13.6 of macOS. Xcode 10.0 comes with version 12.0 of the iOS operating system, version 10.0 of the iOS Simulator, and version 4.2 of the Swift language. To see what version of Swift you have, launch the Terminal application on your Mac and say

$ swift --version
swift-driver version: 1.62.15 Apple Swift version 5.7.2 (swiftlang-5.7.2.135.5 clang-1400.0.29.51)
Target: x86_64-apple-macosx12.0

Other courses

  1. The one-day version of this iOS course consists of the 16 examples marked with an orange sun.
  2. Objective-C version of this iOS course.
  3. Android course in the same format.

Example apps

Each app is created as an Xcode project and then turned into a .zip file by running the Bourne Again shellscript makezip on a Macintosh. Each example contains a link to the corresponding .zip file.

  1. The Swift language: just enough to read today’s examples.

  2. Create a class and objects thereof in Swift.

  3. Create a simple class and objects thereof in Swift.

  4. The first app.
    1. Hello, world project. Print text on the screen. Update it in response to a change of orientation or the passage of time.
    2. Dictionary. Look up a key in a dictionary and display the corresponding value.
    3. Icon. Give your app an icon and a launch screen.
    4. Internationalization (I18n). Make the app change its language when the user selects a new language in the Settings app.

  5. Copy your app to other places.
    1. Upload your project to the GitHub website.
    2. Download your app to your iPhone or iPad.
    3. Upload your app to the App Store. Better start at the beginning of this document.

  6. Still-life graphics.
    1. Japan. Draw circles, rectangles, and lines. Outline vs. fill-in. Three transformations: translate, scale, rotate.
    2. Manhattan. Outline a map using latitude and longitude.
    3. America. Put an image file into an asset catalog file.
    4. Sine. A sine wave with a graph paper background made of wallpaper.

  7. Touch sensitivity and animation. The operating system delivers a set of one or more touch objects to the app.
    1. Set. How to access the values contained in a set object.
    2. Touch. Move a subview in response to a touch. Also our first example of a subview.
    3. Animate. Move a subview under its own power.
    4. Hit: make only a subview touch-sensitive, not the entire screen.
    5. Puzzle: classic puzzle of 8 movable tiles in a 3 × 3 square with one missing. Calls the hitTest(_:withEvent:) method of class UIView.
    6. Etch. Make an Etch A Sketch using the CGMutablePathRef we saw in the “fill and stroke” example.
    7. Flip: transition between two views in response to a swipe. Later we’ll have an even better way to do this using a view controller above the two views.
    8. Label. An object of class UILabel displays one line of text.
    9. Gone: animate the center property of a UILabel that is a subview of a blue UIView.
    10. Star Wars: animate the transform property of a UIImageView that is a subview of a black UIView.

  8. Gesture recognition. See Gestures in the iOS Human Interface Guidelines.
    1. TextView. We will display information about the gesture using a UITextView object.
    2. Swipe: horizontal vs. vertical. The recognizer has the property direction.
    3. Pinch: pinch vs. spread. The recognizer has the property scale.
    4. Tap: single vs. double. The recognizer has the property numberOfTapsRequired.
    5. Rotate: clockwise vs. counterclockwise. The recognizer has the property rotation.
    6. Apple’s other recognizers (i.e., subclasses of class UIGestureRecognizer):
      1. UILongPressGestureRecognizer (hold)
      2. UIPanGestureRecognizer (drag)
      3. UIScreenEdgePanGestureRecognizer (drag on the edge of the screen)
    7. Dragrot.zip. Drag on a postage stamp with one finger, rotate it with two fingers. This app recognizes the two gestures by giving each stamp its own UIPanGestureRecognizer and UIRotationGestureRecognizer. The rotation is implemented by putting a rotation transformation into the transform property of the stamp. I was going to implement the drag by putting a translation transformation into the transform property too. But it would have been complicated to prevent the two transformations from interfering with each other, so I implemented the drag simply by changing the value of the stamp’s center property.

  9. A control object can have more than one target object. There is no need for a target object to conform to a protocol.
    1. Button and AudioServices for a sound shorter than 30 seconds.
    2. Notify. Ask the notification center to carry a notification from one object to another. Needed for Video.
    3. Video. Make the button play a video file.
    4. Switch and AVAudioPlayer for a sound longer than 30 seconds (i.e., background music). An object and its delegate: protocol AVAudioPlayerDelegate.
    5. Slider.
    6. Slide. A slider that moves automatically as an audio file plays. You can drag on the slider too. Uses the CADisplayLink in Pong.
    7. Subviews. Position subviews using layoutSubviews, autoresizingMask, and NSLayoutConstraints.
    8. Segmented: a row of buttons. Classes AVAudioSession, AVAudioRecorder, AVAudioPlayer, and the delegates of the last two. This is our first app to create a file as it runs.
    9. Date Picker. Uses wNSLayoutConstraints.
    10. Apple’s other controls (i.e., subclasses of class UIControl):
      1. Page Controls: class UIPageControl.
      2. Steppers: class UIStepper.
      3. Refresh control: class UIRefreshControl.
      4. iOS has no checkbox. Use a Switch (UISwitch) instead.

  10. An object can have at most one delegate object. The delegate object must adopt (conform to) a protocol.
    1. In every app, the UIApplication object has a delegate (the AppDelegate) that adopts the UIApplicationDelegate protocol.
    2. Switch: class AVAudioPlayer and protocol AVAudioPlayerDelegate.
    3. TextField: class UITextField and protocol UITextFieldDelegate.
    4. Segmented: class AVAudioRecorder and protocol AVAudioRecorderDelegate; class AVAudioPlayer and protocol AVAudioPlayerDelegate.

  11. A View Controller that manages a view. A plain old view controller can have one view underneath it.
    1. Every view controller contains a property named view which refers to the view under the view controller. In a few cases, we gave the init method of the view a parameter named viewController which refers to the view controller above the view.
      1. Button
      2. Switch
      3. Segmented
      4. TextField
    2. Pong: a view controller with one view underneath it. The view controller uses a CADisplayLink to redraw the view 60 times per second.
    3. Pearl: a view controller with one view underneath it. The view controller redraws the view with a CADisplayLink. The objects have mass and momentum to make them feel solid.

  12. View controllers that manage other view controllers. Special types of view controller can have a row of one or more other view controllers underneath them, each one with its own view.
    1. Tab bar controllers: class UITabBarController.
      1. TabBar. A tab bar controller displays a tab bar containing a row of up to five tabs across the bottom of the screen.
      2. Tabs.zip. I cobbled together a tab bar controller with five student view controllers underneath it.
      3. Pastoral. A tab bar controller with a UITabBarControllerDelegate. Plays five sound files.
    2. Navigation controllers: class UINavigationController.
      1. Navigate. A Navigation controller displays a navigation bar (usually containing a back button) across the top of the screen.
      2. Hybrid. A row of navigation controllers under a tab bar controller.
    3. Modally presented view controllers.
      1. Modal. Visit a view controller and its view temporarily, and then return to the previous view controller and its view. The temporary view enters from, and exits to, the bottom edge of the screen.
      2. Still. Take a still photo with a modally presented UIImagePickerController and its UIImagePickerControllerDelegate. Save the photo in the Camera Roll when the controller goes back down. Video, too.
      3. Smoky. Edit text in a UITextView and write it to a file; modally display a confirmation page with a navigation bar containing an OK button.

  13. Table Views: class UITableView. Documentation in the HIG and UIKit.
    1. The Settings app is an example of a tree of table views.
    2. IndexPath: an IndexPath object holds the section number and row number of a UITableViewCell in a UITableView.
    3. States: a UITableView consisting of one section. Its view controller is a UITableViewController, which also acts as the UITableViewDataSource of the UITableView.
    4. Weather: A table view of records downloaded from a database on a server.
    5. Section: divide the table view into sections. Each section can have a title.
    6. Goner: remove a cell from a table view, and remove the corresponding datum from the table view’s data source.
    7. Reorder: reorder the cells in a table view and its data source.
    8. Correct: correct the spelling errors in the cells of a table view.
    9. Insert: insert a cell using a green plus.
    10. Tree. Drill down through a series of table views. Each table view has its own UITableViewController, and there is one UINavigationController on top of the UITableViewControllers.
    11. Tree: present a tree of directories as a series of table views.

  14. Files and directories
    1. Filenames. List the names of the directories and files visible to the app.
    2. Attributes. List the attributes (size, owner, time of creation, etc.) of each directory and file.

  15. Web views: class WKWebView supersedes class UIWebView.
    1. Sample page of HTML (HyperText Markup Language). See the actual HTML by pulling down View Source in your browser. To see the Develop menu in Safari,
      Safari → Preferences… → Advanced
      ☑ Show Develop menu in menu bar
      Then pull down
      Develop → Show Page Source
      Get the hexadecimal code numbers for the special characters from the Unicode code charts.
    2. HTML5 Reference. There are lots of HTML5 tutorials online.
    3. HTML. Display a page of HTML in Apple’s new class WKWebView. The page can come from the web, or from a text file in the app, or it can be hardcoded into a .swift file in the app.
    4. HTML. Display a page of HTML in Apple’s old class UIWebView. The page can come from the web, or from a text file in the app, or it can be hardcoded into a .swift file in the app.
    5. Generate the page of HTML dynamically with a for loop.
    6. 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.

  16. Other subclasses of class UIView:
    1. Activity indicator when you don’t know what fraction of the activity has already been completed. NSTimer, too.
    2. Progress view when you do know what fraction of the activity has already been completed.
    3. Alert: use a UIAlertController to temporarily display an alert view or an action sheet.
    4. Picker: a UIPickerView has rotating drums like a Las Vegas slot machine. UIPickerViewDataSource, UIPickerViewDelegate.
    5. Search bars: class UISearchBar.

  17. Core Location, Google maps, Reverse geocoding.
    1. The Core Location framework: class CLLocationManager and protocol CLLocationManagerDelegate.
    2. The Map Kit framework: class MKMapView (a subclass of cass UIView that is specialized for displaying one Google map) and protocol MKMapViewDelegate.
    3. Class CLGeocoder for converting between street address and latitude/longitude coördinates.
    4. Map: demonstrates the three previous topics working together. Get our location from a CLLocationManager, display a map of that location in a MKMapView, and look up the nearest street address with a CLGeocoder.

  18. SQLite databases and Core Data.

  19. Core Animation.

  20. OpenGL ES graphics.

Bibliography

  1. Apple’s Swift Tour and Language Guide.
  2. Swift Wikipedia article.
  3. Swift resources.
  4. Apple Storyboard tutorial.

Still to do

Notes to myself about things I need to do.

  1. Finish changing every println to print in .zip files.
  2. Button: needs exercise with AudioServicesGetPropertyInfo(_:_:_:_:_:) and AudioServicesGetProperty(_:_:_:_:_:).
  3. Switch: easier way to print four-character error code?
  4. Storyboard in center panel of Xcode no longer has left pane.
  5. Navigate: navigation bar already has left (back) button. Three ways to create a right button.
  6. Still: Get list of images in camera roll or other albums. Get image from camera roll or other album. Display resulting UIImage.
  7. Goner: indent override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) .
  8. Instructions for creating text file in Smoky; copy them from States.
  9. Reorder: also write map example with a for loop.
  10. Insert: title of view controller didn’t update itself.
  11. Retrofit NSURLRequest into Tree, etc.
  12. JavaScript: in print in the .html file, change javaScript to javascript.