iOS7 App Development Essentials.pdf
文件大小: 14813k
源码售价: 10 个金币 积分规则     积分充值
资源说明:Table of Contents 1. Start Here ...............................................................................................................................1 1.1 For New iOS Developers ......................................................................................................... 2 1.2 For iOS 6 Developers .............................................................................................................. 3 1.3 Source Code Download .......................................................................................................... 4 1.4 Feedback ................................................................................................................................ 4 1.5 Errata ...................................................................................................................................... 4 2. Joining the Apple iOS Developer Program...............................................................................5 2.1 Registered Apple Developer ................................................................................................... 5 2.2 Downloading Xcode and the iOS 7 SDK .................................................................................. 5 2.3 iOS Developer Program .......................................................................................................... 6 2.4 When to Enroll in the iOS Developer Program? ..................................................................... 6 2.5 Enrolling in the iOS Developer Program ................................................................................. 7 2.6 Summary ................................................................................................................................ 8 3. Installing Xcode 5 and the iOS 7 SDK .......................................................................................9 3.1 Identifying if you have an Intel or PowerPC based Mac ......................................................... 9 3.2 Installing Xcode 5 and the iOS 7 SDK .................................................................................... 10 3.3 Starting Xcode ...................................................................................................................... 10 4. Creating a Simple iOS 7 App..................................................................................................13 4.1 Starting Xcode 5 ................................................................................................................... 13 4.2 Creating the iOS App User Interface .................................................................................... 18 4.3 Changing Component Properties ......................................................................................... 21 4.4 Adding Objects to the User Interface ................................................................................... 21 4.5 Building and Running an iOS 7 App in Xcode 5..................................................................... 23 4.6 Dealing with Build Errors ...................................................................................................... 24 4.7 Testing Different Screen Sizes .............................................................................................. 25 4.8 Testing User Interface Appearance in Different iOS Versions .............................................. 25 4.9 Monitoring Application Performance................................................................................... 26 4.10 Summary ............................................................................................................................ 27 5. iOS 7 Architecture and SDK Frameworks...............................................................................29 5.1 iPhone OS becomes iOS ....................................................................................................... 29 5.2 An Overview of the iOS 7 Architecture ................................................................................ 30 5.3 The Cocoa Touch Layer......................................................................................................... 31 iv 5.3.1 UIKit Framework (UIKit.framework) ............................................................................. 31 5.3.2 Map Kit Framework (MapKit.framework).....................................................................32 5.3.3 Push Notification Service............................................................................................... 32 5.3.4 Message UI Framework (MessageUI.framework) ........................................................ 33 5.3.5 Address Book UI Framework (AddressUI.framework) ................................................... 33 5.3.6 Game Kit Framework (GameKit.framework) ................................................................ 33 5.3.7 iAd Framework (iAd.framework)................................................................................... 33 5.3.8 Event Kit UI Framework (EventKit.framework) ............................................................. 33 5.3.9 Accounts Framework (Accounts.framework) ................................................................ 33 5.3.10 Social Framework (Social.framework) ........................................................................ 34 5.3.11 SpriteKit Framework (SpriteKit.framework) ................................................................ 34 5.4 The iOS Media Layer............................................................................................................. 34 5.4.1 Core Video Framework (CoreVideo.framework) ........................................................... 34 5.4.2 Core Text Framework (CoreText.framework) ................................................................ 34 5.4.3 Image I/O Framework (ImageIO.framework) ............................................................... 34 5.4.4 Assets Library Framework (AssetsLibrary.framework)..................................................34 5.4.5 Core Graphics Framework (CoreGraphics.framework) ................................................. 35 5.4.6 Core Image Framework (CoreImage.framework) ......................................................... 35 5.4.7 Quartz Core Framework (QuartzCore.framework)........................................................ 35 5.4.8 OpenGL ES framework (OpenGLES.framework) ............................................................ 35 5.4.9 GLKit Framework (GLKit.framework) ............................................................................ 35 5.4.10 NewsstandKit Framework (NewsstandKit.framework) ............................................... 36 5.4.11 iOS Audio Support ....................................................................................................... 36 5.4.12 AV Foundation framework (AVFoundation.framework) ............................................. 36 5.4.13 Core Audio Frameworks (CoreAudio.framework, AudioToolbox.framework and AudioUnit.framework) ........................................................................................................... 36 5.4.14 Open Audio Library (OpenAL)...................................................................................... 36 5.4.15 Media Player Framework (MediaPlayer.framework).................................................. 36 5.4.16 Core Midi Framework (CoreMIDI.framework) ............................................................ 36 5.5 The iOS Core Services Layer ................................................................................................. 37 5.5.1 Address Book Framework (AddressBook.framework) ................................................... 37 5.5.2 CFNetwork Framework (CFNetwork.framework) .......................................................... 37 5.5.3 Core Data Framework (CoreData.framework) .............................................................. 37 5.5.4 Core Foundation Framework (CoreFoundation.framework) ......................................... 37 5.5.5 Core Media Framework (CoreMedia.framework) ......................................................... 37 5.5.6 Core Telephony Framework (CoreTelephony.framework) ............................................ 38 5.5.7 EventKit Framework (EventKit.framework)................................................................... 38 5.6 Foundation Framework (Foundation.framework) ............................................................... 38 v 5.6.1 Core Location Framework (CoreLocation.framework) .................................................. 38 5.6.2 Mobile Core Services Framework (MobileCoreServices.framework)............................. 38 5.6.3 Store Kit Framework (StoreKit.framework) ................................................................... 38 5.6.4 SQLite library ................................................................................................................. 39 5.6.5 System Configuration Framework (SystemConfiguration.framework) ......................... 39 5.6.6 Quick Look Framework (QuickLook.framework) ........................................................... 39 5.7 The iOS Core OS Layer .......................................................................................................... 39 5.7.1 Accelerate Framework (Accelerate.framework) ........................................................... 39 5.7.2 External Accessory Framework (ExternalAccessory.framework) .................................. 40 5.7.3 Security Framework (Security.framework)....................................................................40 5.7.4 System (LibSystem) ....................................................................................................... 40 6. Testing Apps on iOS 7 Devices with Xcode 5 .........................................................................41 6.1 Configuring Xcode with Apple IDs ........................................................................................ 41 6.2 Generating Signing Identities ............................................................................................... 42 6.3 Adding a Device to the Developer Portal ............................................................................. 44 6.4 Running an Application on a Registered Device ................................................................... 45 6.5 Summary .............................................................................................................................. 46 7. The Basics of Objective-C Programming................................................................................47 7.1 Objective-C Data Types and Variables.................................................................................. 47 7.2 Objective-C Expressions ....................................................................................................... 48 7.3 Objective-C Flow Control with if and else ............................................................................ 52 7.4 Looping with the for Statement ........................................................................................... 53 7.5 Objective-C Looping with do and while................................................................................ 54 7.6 Objective-C do ... while loops ............................................................................................... 54 8. The Basics of Object Oriented Programming in Objective-C ..................................................57 8.1 What is an Object? ............................................................................................................... 57 8.2 What is a Class? .................................................................................................................... 57 8.3 Declaring an Objective-C Class Interface .............................................................................. 57 8.4 Adding Instance Variables to a Class .................................................................................... 58 8.5 Define Class Methods ........................................................................................................... 59 8.6 Declaring an Objective-C Class Implementation .................................................................. 60 8.7 Declaring and Initializing a Class Instance ............................................................................ 61 8.8 Automatic Reference Counting (ARC) .................................................................................. 62 8.9 Calling Methods and Accessing Instance Data ..................................................................... 62 8.10 Objective-C and Dot Notation ............................................................................................ 63 8.11 How Variables are Stored ................................................................................................... 64 vi 8.12 An Overview of Indirection................................................................................................. 65 8.13 Indirection and Objects ...................................................................................................... 67 8.14 Indirection and Object Copying .......................................................................................... 67 8.15 Creating the Program Section ............................................................................................ 68 8.16 Bringing it all Together ....................................................................................................... 69 8.17 Structuring Object-Oriented Objective-C Code .................................................................. 71 9. The Basics of Modern Objective-C.........................................................................................75 9.1 Default Property Synthesis ................................................................................................... 75 9.2 Method Ordering.................................................................................................................. 77 9.3 NSNumber Literals................................................................................................................ 77 9.4 Array Literals ........................................................................................................................ 78 9.5 Dictionary Literals................................................................................................................. 79 9.6 Summary .............................................................................................................................. 80 10. An Overview of the iOS 7 Application Development Architecture.......................................81 10.1 Model View Controller (MVC) ............................................................................................ 81 10.2 The Target-Action pattern, IBOutlets and IBActions .......................................................... 82 10.3 Subclassing ......................................................................................................................... 83 10.4 Delegation .......................................................................................................................... 84 10.5 Summary ............................................................................................................................ 84 11. Creating an Interactive iOS 7 App........................................................................................85 11.1 Creating the New Project ................................................................................................... 85 11.2 Creating the User Interface ................................................................................................ 85 11.3 Building and Running the Sample Application ................................................................... 88 11.4 Adding Actions and Outlets ................................................................................................ 89 11.5 Building and Running the Finished Application .................................................................. 94 11.6 Summary ............................................................................................................................ 95 12. Writing iOS 7 Code to Hide the Keyboard............................................................................97 12.1 Creating the Example App .................................................................................................. 97 12.2 Hiding the Keyboard when the User Touches the Return Key ........................................... 98 12.3 Hiding the Keyboard when the User Taps the Background..............................................100 12.4 Summary .......................................................................................................................... 101 13. Understanding iOS 7 Views, Windows and the View Hierarchy.........................................103 13.1 An Overview of Views....................................................................................................... 103 13.2 The UIWindow Class ......................................................................................................... 103 vii 13.3 The View Hierarchy .......................................................................................................... 104 13.4 View Types........................................................................................................................ 106 13.4.1 The Window .............................................................................................................. 106 13.4.2 Container Views ........................................................................................................ 107 13.4.3 Controls ..................................................................................................................... 107 13.4.4 Display Views ............................................................................................................ 107 13.4.5 Text and Web Views..................................................................................................107 13.4.6 Navigation Views and Tab Bars ................................................................................ 107 13.4.7 Alert Views and Action Sheets................................................................................... 107 13.5 Summary .......................................................................................................................... 108 14. An Introduction to Auto Layout in iOS 7............................................................................109 14.1 An Overview of Auto Layout ............................................................................................ 110 14.2 Alignment Rects................................................................................................................ 111 14.3 Intrinsic Content Size........................................................................................................111 14.4 Content Hugging and Compression Resistance Priorities ................................................ 111 14.5 Three Ways to Create Constraints.................................................................................... 112 14.6 Constraints in more Detail................................................................................................ 112 14.7 Summary .......................................................................................................................... 113 15. Working with iOS 7 Auto Layout Constraints in Interface Builder .....................................115 15.1 A Simple Example of Auto Layout in Action ..................................................................... 115 15.2 Enabling and Disabling Auto Layout in Interface Builder ................................................. 115 15.3 The Auto Layout Features of Interface Builder ................................................................ 123 15.3.1 Suggested Constraints............................................................................................... 123 15.3.2 Visual Cues ................................................................................................................ 124 15.3.3 Highlighting Constraint Problems ............................................................................. 126 15.3.4 Viewing, Editing and Deleting Constraints ................................................................ 129 15.4 Creating New Constraints in Interface Builder ................................................................. 131 15.5 Resolving Auto Layout Problems ...................................................................................... 132 15.6 Summary .......................................................................................................................... 134 16. An iOS 7 Auto Layout Example .......................................................................................... 135 16.1 Preparing the Project ....................................................................................................... 135 16.2 Designing the User Interface ............................................................................................ 135 16.3 Adding Auto Layout Constraints.......................................................................................137 16.4 Adjusting Constraint Priorities.......................................................................................... 140 16.5 Testing the Application..................................................................................................... 143 16.6 Summary .......................................................................................................................... 143 viii 17. Implementing iOS 7 Auto Layout Constraints in Code ....................................................... 145 17.1 Creating Constraints in Code ............................................................................................ 145 17.2 Adding a Constraint to a View .......................................................................................... 147 17.3 Turning off Auto Resizing Translation .............................................................................. 148 17.4 An Example Application.................................................................................................... 148 17.5 Creating the Views ........................................................................................................... 148 17.6 Creating and Adding the Constraints ............................................................................... 149 17.7 Removing Constraints ...................................................................................................... 152 17.8 Summary .......................................................................................................................... 153 18. Implementing Cross-Hierarchy Auto Layout Constraints in iOS 7 ......................................155 18.1 The Example Application .................................................................................................. 155 18.2 Establishing Outlets .......................................................................................................... 157 18.3 Writing the Code to Remove the Old Constraint ............................................................. 158 18.4 Adding the Cross Hierarchy Constraint ............................................................................ 159 18.5 Testing the Application..................................................................................................... 159 18.6 Summary .......................................................................................................................... 159 19. Understanding the iOS 7 Auto Layout Visual Format Language.........................................161 19.1 Introducing the Visual Format Language ......................................................................... 161 19.2 Visual Language Format Examples ................................................................................... 161 19.3 Using the constraintsWithVisualFormat: Method............................................................ 163 19.4 Summary .......................................................................................................................... 165 20. Using Storyboards in Xcode 5............................................................................................167 20.1 Creating the Storyboard Example Project ........................................................................ 167 20.2 Accessing the Storyboard ................................................................................................. 168 20.3 Adding Scenes to the Storyboard ..................................................................................... 170 20.4 Configuring Storyboard Segues ........................................................................................ 171 20.5 Configuring Storyboard Transitions.................................................................................. 172 20.6 Associating a View Controller with a Scene ..................................................................... 173 20.7 Passing Data Between Scenes .......................................................................................... 175 20.8 Unwinding Storyboard Segues ......................................................................................... 176 20.9 Triggering a Storyboard Segue Programmatically ............................................................ 178 20.10 Summary ........................................................................................................................ 179 21. Using Xcode 5 Storyboards to Create an iOS 7 Tab Bar Application ................................... 181 21.1 An Overview of the Tab Bar.............................................................................................. 181 ix 21.2 Understanding View Controllers in a Multiview Application ........................................... 181 21.3 Setting up the Tab Bar Example Application .................................................................... 182 21.4 Reviewing the Project Files............................................................................................... 183 21.5 Renaming the Initial View Controller ............................................................................... 183 21.6 Adding the View Controller for the Second Content View ............................................... 183 21.7 Adding the Tab Bar Controller to the Storyboard ............................................................ 183 21.8 Adding a Second View Controller to the Storyboard ....................................................... 185 21.9 Designing the View Controller User interfaces ................................................................ 187 21.10 Configuring the Tab Bar Items ........................................................................................ 188 21.11 Building and Running the Application ............................................................................ 189 21.12 Summary ........................................................................................................................ 190 22. An Overview of iOS 7 Table Views and Xcode 5 Storyboards.............................................191 22.1 An Overview of the Table View ........................................................................................ 191 22.2 Static vs. Dynamic Table Views......................................................................................... 192 22.3 The Table View Delegate and dataSource........................................................................192 22.4 Table View Styles..............................................................................................................192 22.5 Table View Cell Styles ....................................................................................................... 194 22.6 Table View Cell Reuse....................................................................................................... 195 22.7 Summary .......................................................................................................................... 196 23. Using Xcode 5 Storyboards to Build Dynamic TableViews with Prototype Table View Cells ............................................................................................................................................... 197 23.1 Creating the Example Project ........................................................................................... 197 23.2 Adding the TableView Controller to the Storyboard ........................................................ 198 23.3 Creating the UITableViewController and UITableViewCell Subclasses ............................ 199 23.4 Declaring the Cell Reuse Identifier ................................................................................... 201 23.5 Designing a Storyboard UITableView Prototype Cell ....................................................... 201 23.6 Modifying the CarTableViewCell Class ............................................................................. 202 23.7 Creating the Table View Datasource ................................................................................ 204 23.8 Downloading and Adding the Image Files ........................................................................ 207 23.9 Compiling and Running the Application ........................................................................... 208 23.10 Summary ........................................................................................................................ 208 24. Implementing iOS 7 TableView Navigation using Storyboards in Xcode 5 ......................... 209 24.1 Understanding the Navigation Controller ........................................................................ 209 24.2 Adding the New Scene to the Storyboard ........................................................................ 210 24.3 Adding a Navigation Controller ........................................................................................ 211 24.4 Establishing the Storyboard Segue ................................................................................... 212 x 24.5 Modifying the CarDetailViewController Class .................................................................. 213 24.6 Using prepareForSegue: to Pass Data between Storyboard Scenes ................................ 215 24.7 Testing the Application..................................................................................................... 216 24.8 Summary .......................................................................................................................... 217 25. Using an Xcode 5 Storyboard to Create a Static Table View ..............................................219 25.1 An Overview of the Static Table Project...........................................................................219 25.2 Creating the Project.......................................................................................................... 220 25.3 Adding a Table View Controller ........................................................................................ 220 25.4 Changing the Table View Content Type ........................................................................... 220 25.5 Designing the Static Table ................................................................................................ 221 25.6 Adding Items to the Table Cells ........................................................................................ 223 25.7 Modifying the StaticTableViewController Class ............................................................... 226 25.8 Building and Running the Application .............................................................................. 226 25.9 Summary .......................................................................................................................... 227 26. An iPad iOS 7 Split View and Popover Example.................................................................229 26.1 An Overview of Split View and Popovers ......................................................................... 229 26.2 About the Example iPad Split View and Popover Project.................................................230 26.3 Creating the Project.......................................................................................................... 230 26.4 Reviewing the Project....................................................................................................... 230 26.5 Configuring Master View Items ........................................................................................ 231 26.6 Configuring the Detail View Controller ............................................................................ 234 26.7 Connecting Master Selections to the Detail View ............................................................ 235 26.8 Popover Implementation ................................................................................................. 235 26.9 Testing the Application..................................................................................................... 236 26.10 Summary ........................................................................................................................ 238 27. Implementing a Page based iOS 7 Application using UIPageViewController .....................239 27.1 The UIPageViewController Class ...................................................................................... 239 27.2 The UIPageViewController DataSource ............................................................................ 240 27.3 Navigation Orientation ..................................................................................................... 240 27.4 Spine Location .................................................................................................................. 241 27.5 The UIPageViewController Delegate Protocol ................................................................. 241 27.6 Summary .......................................................................................................................... 242 28. An Example iOS 7 UIPageViewController Application .......................................................243 28.1 The Xcode Page-based Application Template .................................................................. 243 28.2 Creating the Project.......................................................................................................... 244 xi 28.3 Adding the Content View Controller ................................................................................ 244 28.4 Creating the Data Model .................................................................................................. 246 28.5 Initializing the UIPageViewController............................................................................... 250 28.6 Running the UIPageViewController Application .............................................................. 253 28.7 Summary .......................................................................................................................... 253 29. Using the iOS 7 UIPickerView and UIDatePicker Components...........................................255 29.1 The DatePicker and PickerView Components .................................................................. 255 29.2 A DatePicker Example ...................................................................................................... 256 29.3 Designing the User Interface ............................................................................................ 256 29.4 Coding the Date Picker Example Functionality................................................................. 258 29.5 Building and Running the Date Picker Application ........................................................... 259 30. An iOS 7 UIPickerView Example ........................................................................................261 30.1 Creating the iOS 7 PickerView Project..............................................................................261 30.2 UIPickerView Delegate and DataSource........................................................................... 261 30.3 The PickerViewController.h File ....................................................................................... 262 30.4 Designing the User Interface ............................................................................................ 262 30.5 Initializing the Arrays ........................................................................................................ 264 30.6 Implementing the DataSource Protocol ........................................................................... 265 30.7 Implementing the Delegate Protocol ............................................................................... 266 30.8 Hiding the Keyboard ......................................................................................................... 267 30.9 Testing the Application..................................................................................................... 267 31. Working with Directories on iOS 7 ....................................................................................269 31.1 The Application Documents Directory ............................................................................. 269 31.2 The Objective-C NSFileManager, NSFileHandle and NSData Classes ............................... 270 31.3 Understanding Pathnames in Objective-C ....................................................................... 270 31.4 Obtaining a Reference to the Default NSFileManager Object.......................................... 271 31.5 Identifying the Current Working Directory ...................................................................... 271 31.6 Identifying the Documents Directory ............................................................................... 271 31.7 Identifying the Temporary Directory................................................................................273 31.8 Changing Directory ........................................................................................................... 273 31.9 Creating a New Directory ................................................................................................. 274 31.10 Deleting a Directory........................................................................................................ 275 31.11 Listing the Contents of a Directory ................................................................................. 275 31.12 Getting the Attributes of a File or Directory .................................................................. 276 32. Working with Files on iOS 7 ..............................................................................................279 xii 32.1 Creating an NSFileManager Instance ............................................................................... 279 32.2 Checking for the Existence of a File..................................................................................279 32.3 Comparing the Contents of Two Files .............................................................................. 280 32.4 Checking if a File is Readable/Writable/Executable/Deletable ........................................ 280 32.5 Moving/Renaming a File................................................................................................... 281 32.6 Copying a File ................................................................................................................... 281 32.7 Removing a File ................................................................................................................ 282 32.8 Creating a Symbolic Link................................................................................................... 282 32.9 Reading and Writing Files with NSFileManager ............................................................... 283 32.10 Working with Files using the NSFileHandle Class ........................................................... 283 32.11 Creating an NSFileHandle Object ................................................................................... 284 32.12 NSFileHandle File Offsets and Seeking ........................................................................... 284 32.13 Reading Data from a File ................................................................................................ 285 32.14 Writing Data to a File...................................................................................................... 286 32.15 Truncating a File ............................................................................................................. 286 32.16 Summary ........................................................................................................................ 287 33. iOS 7 Directory Handling and File I/O – A Worked Example ..............................................289 33.1 The Example Application .................................................................................................. 289 33.2 Setting up the Application Project.................................................................................... 289 33.3 Designing the User Interface ............................................................................................ 289 33.4 Checking the Data File on Application Startup ................................................................. 291 33.5 Implementing the Action Method....................................................................................292 33.6 Building and Running the Example...................................................................................293 34. Preparing an iOS 7 App to use iCloud Storage ................................................................... 295 34.1 What is iCloud?................................................................................................................. 295 34.2 iCloud Data Storage Services ............................................................................................ 295 34.3 Preparing an Application to Use iCloud Storage .............................................................. 296 34.4 Enabling iCloud Support for an iOS 7 Application ............................................................ 296 34.5 Reviewing the iCloud Entitlements File ............................................................................ 297 34.6 Manually Creating the Entitlements File .......................................................................... 298 34.7 Accessing Multiple Ubiquity Containers...........................................................................299 34.8 Ubiquity Container URLs .................................................................................................. 299 34.9 Summary .......................................................................................................................... 300 35. Managing Files using the iOS 7 UIDocument Class ............................................................301 35.1 An Overview of the UIDocument Class ............................................................................ 301 35.2 Subclassing the UIDocument Class ................................................................................... 301 xiii 35.3 Conflict Resolution and Document States ........................................................................ 302 35.4 The UIDocument Example Application.............................................................................303 35.5 Creating a UIDocument Subclass...................................................................................... 303 35.6 Designing the User Interface ............................................................................................ 303 35.7 Implementing the Application Data Structure ................................................................. 305 35.8 Implementing the contentsForType Method ................................................................... 305 35.9 Implementing the loadFromContents Method ................................................................ 306 35.10 Loading the Document at App Launch ........................................................................... 306 35.11 Saving Content to the Document ................................................................................... 310 35.12 Testing the Application................................................................................................... 311 35.13 Summary ........................................................................................................................ 311 36. Using iCloud Storage in an iOS 7 Application.....................................................................313 36.1 iCloud Usage Guidelines ................................................................................................... 313 36.2 Preparing the iCloudStore Application for iCloud Access.................................................314 36.3 Configuring the View Controller ....................................................................................... 314 36.4 Implementing the viewDidLoad Method ......................................................................... 315 36.5 Implementing the metadataQueryDidFinishGathering: Method ..................................... 318 36.6 Implementing the saveDocument Method ...................................................................... 322 36.7 Enabling iCloud Document and Data Storage .................................................................. 322 36.8 Running the iCloud Application ........................................................................................ 323 36.9 Reviewing and Deleting iCloud Based Documents ........................................................... 324 36.10 Reviewing iCloud Activities in the Xcode Debugging Debug Navigator.......................... 325 36.11 Making a Local File Ubiquitous....................................................................................... 325 36.12 Summary ........................................................................................................................ 326 37. Synchronizing iOS 7 Key-Value Data using iCloud..............................................................327 37.1 An Overview of iCloud Key-Value Data Storage ............................................................... 327 37.2 Sharing Data Between Applications ................................................................................. 328 37.3 Data Storage Restrictions ................................................................................................. 329 37.4 Conflict Resolution ........................................................................................................... 329 37.5 Receiving Notification of Key-Value Changes................................................................... 329 37.6 An iCloud Key-Value Data Storage Example ..................................................................... 329 37.7 Enabling the Application for iCloud Key Value Data Storage ........................................... 330 37.8 Designing the User Interface ............................................................................................ 330 37.9 Implementing the View Controller ................................................................................... 331 37.10 Modifying the viewDidLoad Method.............................................................................. 332 37.11 Implementing the Notification Method ......................................................................... 333 xiv 37.12 Implementing the saveData Method ............................................................................. 334 37.13 Testing the Application................................................................................................... 334 38. iOS 7 Data Persistence using Archiving..............................................................................337 38.1 An Overview of Archiving ................................................................................................. 337 38.2 The Archiving Example Application .................................................................................. 338 38.3 Designing the User Interface ............................................................................................ 338 38.4 Checking for the Existence of the Archive File on Startup ............................................... 340 38.5 Archiving Object Data in the Action Method ................................................................... 342 38.6 Testing the Application..................................................................................................... 342 38.7 Summary .......................................................................................................................... 343 39. iOS 7 Database Implementation using SQLite ...................................................................345 39.1 What is SQLite? ................................................................................................................ 345 39.2 Structured Query Language (SQL) .................................................................................... 346 39.3 Trying SQLite on MacOS X ................................................................................................ 346 39.4 Preparing an iOS Application Project for SQLite Integration ............................................ 348 39.5 Key SQLite Functions ........................................................................................................ 349 39.6 Declaring a SQLite Database............................................................................................. 349 39.7 Opening or Creating a Database ...................................................................................... 350 39.8 Preparing and Executing a SQL Statement ....................................................................... 350 39.9 Creating a Database Table................................................................................................ 351 39.10 Extracting Data from a Database Table .......................................................................... 352 39.11 Closing a SQLite Database .............................................................................................. 353 39.12 Summary ........................................................................................................................ 353 40. An Example SQLite based iOS 7 Application ...................................................................... 355 40.1 About the Example SQLite Application............................................................................. 355 40.2 Creating and Preparing the SQLite Application Project.................................................... 355 40.3 Importing sqlite3.h and declaring the Database Reference ............................................. 356 40.4 Designing the User Interface ............................................................................................ 356 40.5 Creating the Database and Table ..................................................................................... 358 40.6 Implementing the Code to Save Data to the SQLite Database.........................................360 40.7 Implementing Code to Extract Data from the SQLite Database ....................................... 361 40.8 Building and Running the Application .............................................................................. 362 40.9 Summary .......................................................................................................................... 363 41. Working with iOS 7 Databases using Core Data.................................................................365 41.1 The Core Data Stack ......................................................................................................... 365 xv 41.2 Managed Objects ............................................................................................................. 366 41.3 Managed Object Context ................................................................................................. 366 41.4 Managed Object Model.................................................................................................... 367 41.5 Persistent Store Coordinator............................................................................................367 41.6 Persistent Object Store..................................................................................................... 368 41.7 Defining an Entity Description..........................................................................................368 41.8 Obtaining the Managed Object Context .......................................................................... 369 41.9 Getting an Entity Description ........................................................................................... 370 41.10 Creating a Managed Object ............................................................................................ 370 41.11 Getting and Setting the Attributes of a Managed Object .............................................. 370 41.12 Fetching Managed Objects ............................................................................................. 371 41.13 Retrieving Managed Objects based on Criteria .............................................................. 371 41.14 Summary ........................................................................................................................ 372 42. An iOS 7 Core Data Tutorial...............................................................................................373 42.1 The Core Data Example Application ................................................................................. 373 42.2 Creating a Core Data based Application ........................................................................... 373 42.3 Creating the Entity Description ........................................................................................ 373 42.4 Adding a Storyboard to the Project .................................................................................. 375 42.5 Adding a View Controller.................................................................................................. 376 42.6 Designing the User Interface ............................................................................................ 376 42.7 Saving Data to the Persistent Store using Core Data ....................................................... 378 42.8 Retrieving Data from the Persistent Store using Core Data ............................................. 379 42.9 Building and Running the Example Application................................................................ 380 42.10 Summary ........................................................................................................................ 381 43. An Overview of iOS 7 Multitouch, Taps and Gestures .......................................................383 43.1 The Responder Chain ....................................................................................................... 383 43.2 Forwarding an Event to the Next Responder ................................................................... 384 43.3 Gestures ........................................................................................................................... 384 43.4 Taps .................................................................................................................................. 384 43.5 Touches ............................................................................................................................ 385 43.6 Touch Notification Methods............................................................................................. 385 43.6.1 touchesBegan method .............................................................................................. 385 43.6.2 touchesMoved method ............................................................................................. 385 43.6.3 touchesEnded method .............................................................................................. 385 43.6.4 touchesCancelled method ......................................................................................... 386 43.7 Summary .......................................................................................................................... 386 xvi 44. An Example iOS 7 Touch, Multitouch and Tap Application ................................................ 387 44.1 The Example iOS 7 Tap and Touch Application ................................................................ 387 44.2 Creating the Example iOS Touch Project .......................................................................... 387 44.3 Designing the User Interface ............................................................................................ 387 44.4 Enabling Multitouch on the View ..................................................................................... 389 44.5 Implementing the touchesBegan Method ....................................................................... 389 44.6 Implementing the touchesMoved Method ...................................................................... 390 44.7 Implementing the touchesEnded Method ....................................................................... 390 44.8 Getting the Coordinates of a Touch ................................................................................. 391 44.9 Building and Running the Touch Example Application ..................................................... 391 45. Detecting iOS 7 Touch Screen Gesture Motions ................................................................393 45.1 The Example iOS 7 Gesture Application ........................................................................... 393 45.2 Creating the Example Project ........................................................................................... 393 45.3 Designing the Application User Interface ......................................................................... 393 45.4 Implementing the touchesBegan Method ....................................................................... 395 45.5 Implementing the touchesMoved Method ...................................................................... 395 45.6 Implementing the touchesEnded Method ....................................................................... 396 45.7 Building and Running the Gesture Example ..................................................................... 396 45.8 Summary .......................................................................................................................... 396 46. Identifying Gestures using iOS 7 Gesture Recognizers.......................................................397 46.1 The UIGestureRecognizer Class ........................................................................................ 397 46.2 Recognizer Action Messages ............................................................................................ 398 46.3 Discrete and Continuous Gestures ................................................................................... 398 46.4 Obtaining Data from a Gesture ........................................................................................ 398 46.5 Recognizing Tap Gestures ................................................................................................ 399 46.6 Recognizing Pinch Gestures.............................................................................................. 399 46.7 Detecting Rotation Gestures ............................................................................................ 399 46.8 Recognizing Pan and Dragging Gestures .......................................................................... 400 46.9 Recognizing Swipe Gestures.............................................................................................400 46.10 Recognizing Long Touch (Touch and Hold) Gestures ..................................................... 401 46.11 Summary ........................................................................................................................ 401 47. An iOS 7 Gesture Recognition Tutorial ..............................................................................403 47.1 Creating the Gesture Recognition Project........................................................................403 47.2 Designing the User Interface ............................................................................................ 403 47.3 Implementing the Action Methods .................................................................................. 406 xvii 47.4 Testing the Gesture Recognition Application ................................................................... 407 48. An Overview of iOS 7 Collection View and Flow Layout ....................................................409 48.1 An Overview of Collection Views...................................................................................... 409 48.2 The UICollectionView Class .............................................................................................. 412 48.3 The UICollectionViewCell Class ........................................................................................ 412 48.4 The UICollectionReusableView Class................................................................................ 413 48.5 The UICollectionViewFlowLayout Class............................................................................413 48.6 The UICollectionViewLayoutAttributes Class ................................................................... 414 48.7 The UICollectionViewDataSource Protocol ...................................................................... 414 48.8 The UICollectionViewDelegate Protocol .......................................................................... 415 48.9 The UICollectionViewDelegateFlowLayout Protocol ........................................................ 416 48.10 Cell and View Reuse ....................................................................................................... 417 48.11 Summary ........................................................................................................................ 419 49. An iOS 7 Storyboard-based Collection View Tutorial.........................................................421 49.1 Creating the Collection View Example Project ................................................................. 421 49.2 Removing the Template View Controller ......................................................................... 421 49.3 Adding a Collection View Controller to the Storyboard ................................................... 422 49.4 Adding the Collection View Cell Class to the Project........................................................ 424 49.5 Designing the Cell Prototype ............................................................................................ 424 49.6 Implementing the Data Model ......................................................................................... 426 49.7 Implementing the Data Source ........................................................................................ 428 49.8 Testing the Application..................................................................................................... 430 49.9 Setting Sizes for Cell Items ............................................................................................... 431 49.10 Changing Scroll Direction ............................................................................................... 433 49.11 Implementing a Supplementary View ............................................................................ 435 49.12 Implementing the Supplementary View Protocol Methods........................................... 438 49.13 Deleting Collection View Items ...................................................................................... 439 49.14 Summary .............................................................................................................
本源码包内暂不包含可直接显示的源代码文件,请下载源码包。