SECOND EDITION Learning ios Programming Alasdair Allan Beijing Cambridge Farnham Koln Sebastopol O'REILLY Tokyo
Table of Contents Preface ix 1. Why Go Native? 1 The Pros and Cons 1 Why Write Native Applications? 2 The Release Cycle 3 Build It and They Will Come 4 2. Becoming a Developer 7 Registering as an ios Developer 7 Enrolling in the ios Developer Program. 9 The Mac Developer Program 10 the ios SDK 11 Installing What Happens When There Is a Beta? 13 Preparing Your ios Device 14 Creating a Development Certificate 15 Getting the UDID of Your Development Device 17 Creating an App ID 18 Creating a Mobile Provisioning Profile 19 Making Your Device Available for Development 20 3. Your First ios App 23 Objective-C Basics 23 Object-Oriented Programming 23 The Objective-C Object Model 25 The Basics of Objective-C Syntax 27 Creating a Project 28 Exploring the Project in Xcode 32 Our Project in Interface Builder 39 Building Connecting the User Interface 41 the User Interface to the Code 42 iii
Table Running the Application in the Simulator 46 Putting the Application on Your iphone 48 4. Coding in Objective-C 49 Declaring and Defining Classes 49 Declaring a Class with the Interface 49 Defining a Class with the Implementation 51 Object Typing 52 Properties 52 Synthesizing Properties 54 The Dot Syntax 54 Declaring Methods 54 Calling Methods 55 Calling Methods on nil 56 Memory Management Creating Objects The Autorelease Pool 57 The alloc, retain, copy, and release Cycle 58 Automatic Reference Counting 59 The dealloc Method 60 Responding to Memory Warnings 60 Fundamental ios Design Patterns 60 The Model-View-Controller Pattern 61 Views and View Controllers 61 The Delegates and DataSource Pattern 62 Conclusion 63 56 57 5. Table View-Based Applications 65 Creating the Project 65 Creating a Table View 66 Running the Code 71 Populating the Table View 71 Building a Model 73 Adding Cities to the Guide 78 Adding Images to Your Projects 80 Connecting the Controller to the Model 81 Mocking Up Functionality with Alert Windows 84 Adding Navigation Controls to the Application 85 Adding a City View 87 Edit Mode 94 Deleting a City Entry 98 Adding a City Entry 99 The "Add New City" Interface 102 iv of Contents
Capturing the City Data 109 6. Other View Controllers 115 Utility Applications 115 Making the Battery Monitoring Application 116 Tab Bar Applications 127 Adding Combining Another Tab Bar Item 128 View Controllers 132 Modal View Controllers 137 Modifying the City Guide Application 139 The Image Picker View Controller 148 Adding the Image Picker to the City Guide Application 148 Master-Detail Applications 157 Creating a Universal Application 158 Popover Controllers 167 7. Connecting to the Network 169 Detecting Network Status 169 Apple's Reachability Class 169 Embedding a Web Browser in Your App 181 A Simple Web View Controller 182 Displaying Static HTML Files 191 Getting Data Out of a UlWebView 192 Sending Email 192 Getting Data from the Internet 198 Synchronous Requests 198 Asynchronous Requests 198 Using Web Services 199 8. Handling Data 219 Data Entry 219 UITextField and Its Delegate 219 UITextView and Its Delegate 221 Parsing XML 223 Parsing XML with libxml2 224 Parsing XML with NSXMLParser 225 Parsing JSON 227 NSJSONSerialization 227 The JSON Framework 229 Retrieving Twitter Trends 230 Using the Twitter Framework 232 The Twitter Trends Application 232 Regular Expressions 240 Table of Contents v
Introduction to Regular Expressions 240 Storing Data 245 Using Flat Files 245 Storing Information in a SQL Database 246 Core Data 254 9. Using Sensors 255 Hardware Support 255 Network Availability 256 Camera Availability 256 Audio Input Availability 256 GPS Availability 257 Magnetometer Availability 257 Setting Required Hardware Capabilities 258 Persistent WiFi 258 Background Modes 259 Differences Between iphone and ipad 260 Using the Camera 261 The Core Motion Framework 262 Pulling Motion Data 262 Pushing Motion Data 263 The Accelerometer 264 The Gyroscope 268 The Magnetometer 270 Accessing the Proximity Sensor Using Vibration 275 277 10. Geolocation and Mapping 279 The Core Location Framework Device Heading 281 Location-Dependent Weather 283 Reverse Geocoding 284 Forward Geocoding 284 CLPlacemark Objects 285 Modifying the Weather Application 285 User Location and MapKit 293 Annotating Maps 301 279 11. Introduction to icioud 309 How Can I Use icioud? icioud Backup 310 Provisioning Your Application for icioud 310 Using Key-Value Storage 313 309 vi Table ofcontents
Wrapping Up 320 12. Integrating Your Application 323 Application Preferences 323 The Accounts Framework 333 The Twitter Framework 335 Sending Tweets 336 Custom URL Schemes 337 Using Custom Schemes 337 Registering Custom Schemes 339 Media Playback 344 Using the Address Book 349 Interactive People Picking 349 Programmatic People Picking 353 Sending Text Messages 353 13. Distributing Your Application 355 Adding Missing Features 355 Adding an Icon 355 Adding a Launch Image 359 Changing the Display Name 364 Enabling Rotation 365 Building and Signing 366 Ad Hoc Distribution 367 Developer-to-Developer Distribution 372 App Store Distribution 373 Submitting to the App Store 374 Building Your Application for App Store Distribution 380 The App Store Resource Center 383 Reasons for Rejection 384 14. Going Further 389 Cocoa and Objective-C 389 The ios SDK 389 Web Applications 390 PhoneGap 390 Core Data 391 In-App Purchase 391 MKStoreKit 392 Core Animation 392 Game Kit 392 Writing Games 393 Look and Feel 393 Table of Contents vii
Hardware Accessories 394 Index 395 viil Table of Contents