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
-
The one-day version of this iOS course
consists of the 16 examples marked with an orange sun.
☀
-
Objective-C
version of this iOS course.
-
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.
- The
Swift language:
☀
just enough to read today’s examples.
- Create a
class
and objects thereof in Swift.
- Create a simple
class
and objects thereof in Swift.
- The first app.
-
Hello, world
☀
project.
Print text on the screen.
Update it in response to a change of orientation
or the passage of time.
-
Dictionary.
Look up a key in a
dictionary
and display the corresponding value.
- Icon.
Give your app an
icon
and a
launch
screen.
- Internationalization (I18n).
Make the app change its language
when the user selects a new language in the Settings app.
- Copy your app to other places.
- Upload
your project to the
GitHub
website.
- Download
your app to your iPhone or iPad.
- Upload
your app to the App Store.
Better start at the
beginning
of this document.
- Still-life graphics.
- Japan.
☀
Draw
circles,
rectangles,
and
lines.
Outline
vs.
fill-in.
Three
transformations:
translate,
scale,
rotate.
- Manhattan.
Outline a map using latitude and longitude.
- America.
Put an image file into an
asset
catalog
file.
- Sine.
A sine wave with a graph paper background made of
wallpaper.
- Touch
sensitivity
and animation.
The operating system delivers a
set
of one or more
touch
objects
to the app.
- Set.
☀
How to access the values contained in a set object.
- Touch.
☀
Move a subview in response to a touch.
Also our first example of a subview.
- Animate.
☀
Move a subview under its own power.
- Hit:
make only a subview touch-sensitive,
not the entire screen.
- Puzzle:
classic puzzle of 8 movable tiles in a 3 × 3 square with one missing.
Calls the
hitTest(_:withEvent:)
method of class
UIView
.
- Etch.
Make an
Etch A Sketch
using the
CGMutablePathRef
we saw in the
“fill and stroke”
example.
- 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.
- Label.
An object of class
UILabel
displays one line of text.
- Gone:
animate the
center
property of a
UILabel
that is a subview of a blue
UIView
.
- Star Wars:
animate the
transform
property of a
UIImageView
that is a subview of a black
UIView
.
- Gesture
recognition.
See
Gestures
in the iOS
Human
Interface Guidelines.
- TextView.
☀
We will display information about the gesture using a
UITextView
object.
- Swipe:
☀
horizontal vs. vertical.
The recognizer has the property
direction
.
- Pinch:
☀
pinch vs. spread.
The recognizer has the property
scale
.
- Tap:
☀
single vs. double.
The recognizer has the property
numberOfTapsRequired
.
- Rotate:
clockwise vs. counterclockwise.
The recognizer has the property
rotation
.
-
Apple’s other recognizers
(i.e., subclasses of class
UIGestureRecognizer
):
-
UILongPressGestureRecognizer
(hold)
-
UIPanGestureRecognizer
(drag)
-
UIScreenEdgePanGestureRecognizer
(drag on the edge of the screen)
-
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.
- A
control
object
can have more than one
target
object.
There is no need for a target object to conform to a
protocol.
- Button
☀
and
AudioServices
for a sound shorter than 30 seconds.
- Notify.
Ask the
notification
center
to carry a
notification
from one object to another.
Needed for
Video.
- Video.
Make the button play a video file.
- Switch
☀
and
AVAudioPlayer
for a sound longer than 30 seconds (i.e., background music).
An object and its delegate:
protocol
AVAudioPlayerDelegate
.
- Slider.
- Slide.
A slider that moves automatically as an audio file plays.
You can drag on the slider too.
Uses the
CADisplayLink
in
Pong.
- Subviews.
Position subviews using
layoutSubviews
,
autoresizingMask
,
and
NSLayoutConstraints
.
- 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.
- Date Picker.
Uses
wNSLayoutConstraint
s.
- Apple’s other controls
(i.e., subclasses of class
UIControl
):
- Page Controls:
class
UIPageControl
.
- Steppers:
class
UIStepper
.
- Refresh control:
class
UIRefreshControl
.
- iOS has no checkbox.
Use a
Switch
(
UISwitch
)
instead.
- An object can have at most one
delegate
object.
The delegate object must adopt (conform to) a
protocol.
- In every app, the
UIApplication
object has a delegate (the
AppDelegate
)
that adopts the
UIApplicationDelegate
protocol.
- Switch:
class
AVAudioPlayer
and protocol
AVAudioPlayerDelegate
.
- TextField:
☀
class
UITextField
and protocol
UITextFieldDelegate
.
- Segmented:
class
AVAudioRecorder
and protocol
AVAudioRecorderDelegate
;
class
AVAudioPlayer
and protocol
AVAudioPlayerDelegate
.
- A
View
Controller
that manages a view.
A plain old view controller can have one view underneath it.
- 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.
- Button
- Switch
- Segmented
- TextField
- Pong:
a view controller with one view underneath it.
The view controller uses a
CADisplayLink
to redraw the view 60 times per second.
- 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.
- 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.
-
Tab
bar controllers:
class
UITabBarController
.
- TabBar.
A
tab
bar controller
displays a
tab
bar
containing a row of up to five tabs across the bottom of the screen.
-
Tabs.zip
.
I cobbled together a tab bar controller
with five student view controllers underneath it.
- Pastoral.
A tab bar controller with a
UITabBarControllerDelegate
.
Plays five sound files.
-
Navigation controllers: class
UINavigationController
.
- Navigate.
A
Navigation
controller
displays a
navigation
bar
(usually containing a back button)
across the top of the screen.
- Hybrid.
A row of navigation controllers under a tab bar controller.
- Modally presented view controllers.
- 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.
-
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.
-
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.
-
Table
Views:
class
UITableView
.
Documentation in the
HIG
and
UIKit.
- The Settings app is an example of a tree of table views.
-
IndexPath:
☀
an
IndexPath
object holds the section number and row number of a
UITableViewCell
in a
UITableView
.
-
States:
☀
a
UITableView
consisting of one section.
Its view controller is a
UITableViewController
,
which also acts as the
UITableViewDataSource
of the
UITableView
.
-
Weather:
☀
A table view of records downloaded from a database on a server.
-
Section:
divide the table view into sections.
Each section can have a title.
-
Goner:
remove a cell from a table view,
and remove the corresponding datum from the table view’s data source.
-
Reorder:
reorder the cells in a table view and its data source.
-
Correct:
correct the spelling errors in the cells of a table view.
-
Insert:
insert a cell using a green plus.
-
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
UITableViewController
s.
- Tree: present a tree of directories as a series of table views.
-
Files and directories
-
Filenames.
List the names of the directories and files visible to the app.
-
Attributes.
List the attributes (size, owner, time of creation, etc.)
of each directory and file.
- Web
views:
class
WKWebView
supersedes class
UIWebView
.
-
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.
- HTML5 Reference.
There are lots of HTML5 tutorials online.
-
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.
-
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.
-
Generate
the page of HTML dynamically with a
for
loop.
-
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.
-
Other subclasses of class
UIView
:
-
Activity
indicator when you don’t know what fraction of the activity
has already been completed.
NSTimer
, too.
-
Progress
view when you do know what fraction of the activity has already been completed.
-
Alert:
use a
UIAlertController
to temporarily display an alert view or an action sheet.
-
Picker:
a
UIPickerView
has rotating drums like a Las Vegas slot machine.
UIPickerViewDataSource
,
UIPickerViewDelegate
.
-
Search
bars:
class
UISearchBar
.
- Core Location, Google maps, Reverse geocoding.
- The Core Location framework:
class
CLLocationManager
and protocol
CLLocationManagerDelegate
.
- The Map Kit framework:
class
MKMapView
(a subclass of cass
UIView
that is specialized for displaying one Google map)
and protocol
MKMapViewDelegate
.
- Class
CLGeocoder
for converting between street address
and latitude/longitude coördinates.
- 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
.
- SQLite databases and Core Data.
- Core Animation.
- OpenGL ES graphics.
Bibliography
- Apple’s
Swift
Tour
and
Language
Guide.
- Swift
Wikipedia
article.
- Swift resources.
- Apple
Storyboard
tutorial.
Still to do
Notes to myself about things I need to do.
-
Finish changing every
println
to
print
in
.zip
files.
-
Button:
needs exercise with
AudioServicesGetPropertyInfo(_:_:_:_:_:)
and
AudioServicesGetProperty(_:_:_:_:_:)
.
-
Switch:
easier way to print four-character error code?
-
Storyboard in center panel of Xcode no longer has left pane.
-
Navigate:
navigation bar already has
left (back) button.
Three ways to create a right button.
-
Still:
Get list of images in camera roll or other albums.
Get image from camera roll or other album.
Display resulting
UIImage
.
-
Goner:
indent
override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath)
.
- Instructions for creating text file in
Smoky;
copy them from
States.
-
Reorder:
also write
map
example with a
for
loop.
-
Insert:
title of view controller didn’t update itself.
- Retrofit NSURLRequest into Tree, etc.
- JavaScript:
in
print
in the .html file, change javaScript to javascript.