SECOND EDITION
Learning
iOS
Programming
Alasdair Allan
Table of
Contents
Preface
ix1.
Why
Go
Native? 1TheProsandCons 1
Why
Write NativeApplications?
2The Release
Cycle
3Build It and
They
WillCome 42.
Becoming
aDeveloper
7Registering
as aniOSDeveloper
7Enrolling
intheiOSDeveloper
Program.
9The Mac
Developer Program
10Installing
the iOSSDK 11What
Happens
When ThereIsaBeta? 13Preparing
Your iOSDevice 14Creating
aDevelopment
Certificate 15Getting
theUDIDofYourDevelopment
Device 17Creating
anApp
ID 18Creating
aMobileProvisioning
Profile 19Making
Your Device Available forDevelopment
203. Your
First
iOSApp
23Objective-C
Basics 23Object-Oriented Programming
23The
Objective-C Object
Model 25The Basicsof
Objective-C Syntax
27Creating
aProject
28Exploring
theProject
inXcode 32Our
Project
inInterface Builder 39Building
theUserInterface 41Running
theApplication
inthe Simulator 46Putting
theApplication
onYouriPhone 484.
Coding
inObjective-C
49Declaring
andDefining
Classes
49Declaring
aClass with the Interface 49Defining
aClasswith theImplementation
51Object Typing
52Properties
52Synthesizing
Properties
54TheDot
Syntax
54Declaring
Methods 54Calling
Methods 55Calling
Methodson nil 56Memory
Management
56Creating Objects
57The Autorelease Pool 57
The
alloc,
retain, copy, and releaseCycle
58 AutomaticReferenceCounting
59The deallocMethod 60
Responding
toMemory
Warnings
60Fundamental iOS
Design
Patterns 60The Model-View-Controller Pattern 61
ViewsandViewControllers 61
The
Delegates
and DataSource Pattern 62Conclusion 63
5.
Table View-Based
Applications
65Creating
theProject
65Creating
aTable View 66Running
the Code 71Populating
the TableView 71Building
aModel 73Adding
CitiestotheGuide 78Adding
Images
toYourProjects
80Connecting
the Controllerto theModel 81Mocking
Up
Functionality
with Alert Windows 84Adding Navigation
ControlstotheApplication
85Adding
aCity
View 87Edit Mode 94
Deleting
aCity Entry
98Adding
aCity Entry
99Capturing
theCity
Data 1096.
Other View Controllers
115Utility Applications
115Making
theBattery Monitoring Application
116Tab Bar
Applications
127Adding
Another TabBar Item 128Combining
ViewControllers 132Modal View Controllers 137
Modifying
theCity
GuideApplication
139The
Image
PickerViewController 148Adding
theImage
Picker to theCity
GuideApplication
148Master-Detail
Applications
157Creating
aUniversalApplication
158Popover
Controllers 1677.
Connecting
tothe Network 169Detecting
NetworkStatus 169Apple's Reachability
Class 169Embedding
aWeb Browser in YourApp
181A
Simple
WebView Controller 182Displaying
Static HTML Files 191Getting
DataOut ofa UlWebView 192Sending
Email 192Getting
Datafromthe Internet 198Synchronous Requests
198Asynchronous
Requests
198Using
WebServices 1998.
Handling
Data219
Data
Entry
219UITextField and Its
Delegate
219UITextViewandIts
Delegate
221Parsing
XML 223Parsing
XMLwith libxml2 224Parsing
XMLwithNSXMLParser 225Parsing
JSON
227NSJSONSerialization
227The
JSON
Framework 229Retrieving
TwitterTrends 230Using
theTwitterFramework 232TheTwitterTrends
Application
232Introductionto
Regular
Expressions
240Storing
Data 245Using
Flat Files 245Storing
Information in aSQL
Database 246Core Data 254
9.
Using
Sensors
255
Hardware
Support
255Network
Availability
256Camera
Availability
256Audio
Input Availability
256GPS
Availability
257Magnetometer
Availability
257Setting
Required
Hardware
Capabilities
258Persistent WiFi 258
Background
Modes 259Differences BetweeniPhoneand iPad 260
Using
the Camera 261TheCore Motion Framework 262
Pulling
MotionData 262Pushing
MotionData 263TheAccelerometer 264
The
Gyroscope
268The
Magnetometer
270Accessing
theProximity
Sensor 275Using
Vibration 27710.
Geolocation
and
Mapping
279The
Core Location Framework 279Device
Heading
281Location-Dependent
Weather
283 ReverseGeocoding
284 ForwardGeocoding
284 CLPlacemarkObjects
285Modifying
the WeatherApplication
285User Location and
MapKit
293Annotating Maps
30111.
Introduction
toiCIoud
309HowCanI UseiCIoud? 309
iCIoud
Backup
310
Provisioning
YourApplication
for iCIoud 310Wrapping
Up
32012.
Integrating
YourApplication
323Application
Preferences 323TheAccountsFramework 333
TheTwitter Framework 335
Sending
Tweets 336CustomURLSchemes 337
Using
CustomSchemes 337Registering
Custom Schemes 339Media
Playback
344Using
the Address Book 349Interactive
People Picking
349Programmatic People Picking
353Sending
TextMessages
35313.
Distributing
Your
Application
355Adding
Missing
Features 355Adding
anIcon 355Adding
aLaunchImage
359Changing
theDisplay
Name 364Enabling
Rotation 365Building
andSigning
366Ad Hoc Distribution 367
Developer-to-Developer
Distribution 372App
StoreDistribution 373Submitting
to theApp
Store 374Building
YourApplication
forApp
Store Distribution 380The
App
Store Resource Center 383Reasonsfor
Rejection
38414.
Going
Further
389Cocoa and
Objective-C
389The iOS SDK 389 Web
Applications
390PhoneGap
390 Core Data 391In-App
Purchase 391 MKStoreKit 392 Core Animation 392 Game Kit 392Writing
Games 393Hardware Accessories 394