Learn ios 7 App Development James Bucanek Apress*
Contents About the Author About the Technical Reviewer Acknowledgments Introduction xxv xxvii xxix xxxi Chapter 1: Got Tools? 1 Requirements 1 Installing Xcode 2 What is Xcode? 3 Becoming an ios Developer 4 Getting the Projects 5 Launching Xcode the First Time 5 Welcome To Xcode 9 Navigation Area 10 Editor Area 11 Utility Area 12 Debug Area 14 Toolbar 14 Running Your First App 14 Summary 16 vil
vili Contents Chapter 2: Boom! App 17 Design 17 Creating the Project 18 Setting Project Properties 20 Building an Interface 21 Adding Objects 23 Deleting and Connecting Objects 24 Adding Views to a View 27 Editing Object Properties 30 Adding Resources 31 Customizing Buttons 33 Using Storyboards 34 Adding New Screens 35 Creating a Segue 40 Setting Navigation Titles 41 Testing Your Interface 42 Finishing Your App 43 Debugging Your App 45 Adding Constraints 46 Testing Your App 54 Summary 55 Chapter 3: Spin a Web 57 Design 58 Creating the Project 58 Building a Web Browser Coding a Web Browser... Adding Outlets to SUViewController Connecting Custom Outlets Adding Actions to SUViewController 59 63 64 64 66
Setting Action Connections 69 Testing the Web Browser 71 Debugging the Web View 72 Adding URL Shortening 73 Designing the URL Shortening Code 76 Becoming a Web View Delegate 77 Shortening an URL 81 Final Touches 89 Cleaning Up the Interface 90 Creating the ipad Version 93 Summary 96 Chapter 4: Coming Events 97 Run Loop 98 Event Queue 98 Event Delivery 100 Direct Delivery ; 100 Hit Testing 100 The First Responder 102 Event Handling 103 The Responder Chain 104 High- vs. Low-Level Events 107 Eight Ball 107 Design 108 Create the Project 108 Create the Interface 109 Writing the Code 114 Handling Shake Events 116 Testing Your EightBall App 117 Finishing Touches 120 Testing on a Physical ios Device 121 Other Uses for The Responder Chain 123
xcontents Touchy Design 124 125 Creating the Project 125 Creating a Custom View 126 Handling Touch Events 128 Drawing Your View 131 Adding Custom Objects in Interface Builder 132 Testing Touchy 133 Advanced Event Handling 134 Summary 135 Chapter 5: Table Manners 137 Table Views 137 Plain Tables 138 Grouped Tables 138 Cell Styles 139 Cell Accessories 141 Custom Cells 142 How Table Views Work 142 Table Cells and Rubber Stamps 143 MyStuff 144 Design 144 Creating The Project 145 Creating Your Data Model 146 Creating a Data Source 149 Implementing Your Rubber Stamp 152 Table Cell Caching 153 Where's the Data? 156 Testing MyStuff 156 Adding the Detail View 157 Creating the Detail View Configuring the Detail View 157 159
Contents xi Editing 163 Inserting and Removing Items 164 Enabling Table Editing 166 Editing Details 168 Observing Changes to MyWhatsit 170 Modal vs. Modeless Editing 174 Little Touches 174 Advanced Table View Topics 174 Summary 175 Chapter 6: Object Lesson 177 Two Houses, Both Alike in Dignity 177 Romeo Meets Juliet 179 Classes and Cookies 179 Classes and Objects and Methods, Oh My! 180 Inheritance 182 Abstract and Concrete Classes 183 Overriding Methods 183 Design Patterns and Principles 184 Encapsulation 184 Singularity of Purpose 185 Stability 185 Open Closed 185 Delegation 186 Other Patterns 187 Summary 188 Chapter 7: Smile! 189 Design 190 Extending Your Design 190 Revising the Data Model 191 Adding an Image View 193
xll Contents Updating the View Controller 196 Connecting a Choose Image Action 197 Taking Pictures 201 You Can't Always Get What You Want 202 Presenting the Image Picker 203 Importing the Image 205 Cropping and Resizing 206 Winding Up 208 Testing the Camera 209 Building the ipad Interface 210 Adding a Popover 212 Sticky Keyboards 213 Advanced Camera Techniques 216 Summary 216 Chapter 8: Model Citizen 217 The Model-View-Controller Design Pattern 217 Data Model Objects 218 View Objects 219 Controller Objects 219 MVC Communications 220 Color Model 221 Creating Your Data Model 223 Creating View Objects 224 Writing Your Controller 227 Wiring Your Interface 228 Having Multiple Views 231 Consolidating Updates 235 Complex View Objects 239 Replacing UlView with CMColorView 239 Connecting the View to Your Data Model 240 Drawing CMColorView 240
Being a K-V Observer 244 Key Value Observing 244 Observing Key Value Changes 244 Creating KVO Dependencies 246 Multi-Vector Data Model Changes 248 Handling Touch Events 248 Binding The Sliders 250 Final Touches 251 Cheating 251 Summary 252 Chapter 9: Sweet, Sweet Music. 255 Making Your Own ipod 256 Design 256 Adding a Music Picker 257 Using a Music Player 260 Adding Playback Control 262 Receiving Music Player Notifications 264 Adding Media Metadata 266 Observing the Playing Item 269 Make Some Noise 271 Living in a Larger World 272 Configuring Your Audio Session 273 Playing Audio Files 275 Creating AVAudioPlayer objects 277 Adding the Sound Buttons 280 Activating Your Audio Session 283 Interruptions and Detours 284 Dealing with Interruptions 285 Adding Your Interruption Handlers 285 Dealing withaudio Route Changes 287 Other Audio Topics 288 Summary 289
_ xhi Contents Chapter 10: Got Views? 291 Learning by Example 291 Buttons 294 The Responder and View Classes 295 The Control Class 296 Button Types 296 Control States 299 Button Code 300 Switches and Sliders 300 Page Control 302 Steppers 303 Segmented Controls 304 Progress Indicators 305 Text Views 307 Labels 307 Text Fields 308 Text Editing Behavior 310 Text Views 311 Pickers 312 Date Picker 313 Anything Picker 314 Image Views 315 Grouped Tables 316 The View You Never See..., 317 Summary 320 Chapter 11: Draw Me a Picture 321 Creating a Custom View Class 321 View Coordinates When Views Are Drawn Drawing a View 322 325 326
Shapely 329 Creating Views Programmatically 330 The -drawrect: Method 332 More Shapes, More Colors 340 Transforms 345 Applying a Translate Transform 346 Applying a Scale Transform 349 Animation: It's Not Just for Manga 351 Using Core Animation 352 Adding Animation to Shapely 352 OpenGL 354 The Order of Things 355 Images and Bitmaps 359 Creating Images from Bitmaps 360 Creating Bitmaps From Drawings 362 Advanced Graphics 363 Text 363 Shadows, Gradients, and Patterns 364 Blend Modes 364 The Context Stack 364 Summary 365 MChapter 12: There and Back Again 367 Measure Twice, Cut Once 367 What is Navigation? 368 View Controller Roles 368 Designing Wonderland 369 Weighing Your Navigation Options 369 Wonderland Navigation 371 Creating Wonderland 372 Adding Wonderland's Resources 374 Configuring a Tab Bar Item 375
xvl Contents The First Content View Controller 376 Presenting a Modal View Controller 378 Dismissing a View Controller 381 Creating a Navigable Table View 383 Breathing Data Into Your Table View 386 Pushing the Detail View Controller 393 Creating a Page View Controller 395 Adding the Page View Controllers 396 Designing a Prototype Page 397 Coding the One Page View 400 The Paginator 402 Coding the Page View Data Source 405 Initializing a Page View Controller 407 Using Pop-Over Controllers 409 Advanced Navigation 409 Summary 410 Chapter 13: Networking, the Social Kind 411 Color My (Social) World 411 Having Something to Share, 412 Presenting the Activity View Controller 413 Sharing More 416 Extracting Code 416 Providing More Items to Share 419 Excluding Activities 420 The Curse of the Lowest Common Denominator 421 Providing Activity Specific Data 422 Promises, Promises 424 Big Data 425 Sharing with Specific Services 425 Other Social Network Interactions 426 Summary 427
Chapter 14: Networking, The Nerdy Kind 429 Sunlbuch 430 Creating SunTouch 431 Designing the Initial Screens 431 Creating the Single Playerversion 434 Loading STGameViewController 435 How SunTouch Works 436 Customizing Core Animations 437 Playing the Game 438 Plugging into Game Center 440 Configuring a Game Center-aware app 440 Enabling Game Center 442 Creating an App in the itunes Store 444 Configuring Game Center 446 Adding GameKit to Your App 448 Obtaining the Local Player 449 Adding a Game Center Button 450 Recording Leaderboard Scores 452 Creating a Test Player 453 Peer-To-Peer Networking 454 Turning SunTouch Into a Two-Player Game 455 Matchmaking 461 Exchanging Data with Another Device 465 Advanced Networking 479 One Last Detail 480 Summary 482 Chapter 15: If You Build It 485 How Interface Builder Files Work 486 Compiling Interface Builder Files 486 Loading a Scene 486 Loading an.xib File 488
_. xviil Contents Placeholder Objects and the File's Owner 489 Creating Objects 490 Editing Attributes 493 Connections 493 Sending Action Messages 496 Taking Control of Interface Builder Files 496 Declaring Placeholders 497 Designing SYShapeView 498 Connecting the Gesture Recognizers 499 Build Your Shape Factory 501 Loading an Interface Builder File 504 Replacing Code 505 Summary 506 Chapter 16: Apps with Attitude 507 Leveler 508 Creating Leveler 508 Getting Motion Data 514 Creating CMMotionManager 515 Starting and Stopping Updates 516 Push Me, Pull You 516 Timing is Everything 517 Herky-Jerky 519 Getting Other Kinds of Motion Data 524 Gyroscope Data Magnetometer Data Device Motion and Attitude 524 525 525 Measuring Change 527 Summary 528 Chapter 17: Where Are You? 529 Creating Pigeon 529 Collecting Location Data 531
Contents xix Using a Map View 533 Decorating Your Map 537 Adding an Annotation 537 Map Coordinates 539 Adding a Little Bounce 540 Pointing the Way 542 Location Monitoring 545 Approximate Location and Non-GPS Devices 546 Monitoring Regions 546 Reducing Location Change Messages 547 Movement and Heading 547 Geocoding 547 Getting Directions 548 Summary 548 Chapter 18: Remember Me? 551 Property Lists 551 Serializing Property Lists 552 User Defaults 552 Making Pigeon Remember 553 Minimizing Updates and Code 554 Defining Your Keys 554 Writing Values to User Defaults 555 Getting Values from User Defaults 555 Testing User Defaults 556 Registering Default Values 557 Turning Objects into Property List Objects 559 Preserving and Restoring savedlocation 560 Persistent Views 563 Fading Into the Background 564 Preserving View Controllers 565
xx Contents. Assigning Restoration Identifiers 566 Customizing Restoration 568 Deeper Restoration 569 Pigeons in the Cloud 569 Storing Values in the Cloud 570 Cloud Watching 571 Enabling icioud 573 Testing the Cloud 574 Bundle Up Your Settings 575 Creating a Settings Bundle 575 Using Your Settings Bundle Values 578 Testing Your Settings Bundle 579 Summary 580 Chapter 19: Doc, You Meant Storage 581 Document Overview 581 Where, Oh Where, Do My Documents Go? 582 MyStuff on Documents 583 Supplying Your Document's Data 585 Wrapping Up Your Data 587 Using Wrappers ; 587 Incremental Document Updates 588 Constructing Your Wrappers 588 Interpreting Your Wrappers 589 Archiving Objects 591 Adopting NSCoding 591 Archiving and Unarchiving Objects 593 The Archiving Serialization Smackdown 593 Serialization, Meet Archiving 594 Document, Your Data Model 595 Tracking Changes 598 Testing Your Document 600
Setting Breakpoints 600 Stepping Through Code and Examining Variables 601 Storing Image Files 608 Odds and Ends 615 icioud Storage Archive Versioning Summary 615 615 616 Chapter 20: Being Objective 617 Objective-C isc 618 Objective-C Classes 619 Implementing Your Class 620 Creating and Destroying Objects 621 Class Clusters 622 Referring to Objects Can I See Your id? Method Names : Method Name Construction The +initialize Method Properties Instance Variables 622 623 624 625 626 627 627 Using Getters and Setters 627 Declared Properties 628 Automatic Properties 630 The Anatomy of a Property Keeping Your Promises Introspection Class Method Protocol Protocols 631 633 634 634 635 635 636 Adopting Protocols 636 Referring to Conforming Objects 637
xxli Contents Categories 638 Single Responsibility 639 Module Organization 639 Private Methods 640 Extensions 640 nil is Your Friend 641 The Unbearable Lightness of nil 641 The Virtues of Being Positive 641 When nil Is Bad 642 Copying Objects 642 Adopting NSCopying 643 Inheriting NSCopying 644 Copying Something Special 644 Copying an Object 645 Mutable Copies 645 Attributed Strings 645 Collections 648 Collection Classes 648 Enumeration 649 Fast Object Enumeration 649 Collection Enumeration 650 Shortcuts 650 Summary 651 Chapter 21: The Elephant in the Room 653 Memory Management 654 Your Grandfather's Memory Management 655 Garbage Collection 656 Reference Counting 657 Manual Reference Counting 659 Jumping into the Pool 660 Quick Summary 662
Contents xxiii Breaking the Cycle 663 Scared Straight 665 Automatic Reference Counting 665 Enabling ARC 666 Strong and Weak References 667 What ARC Doesn't Do 668 Summary 669 Chapter 22: Etes-vous Polyglotte? 671 The Localization Process 671 Language Bundles 672 Programmatic Localization 673 Localize Now! 674 Internationalizing Your App 680 Internationalizing String Constants 680 Using the genstrings Tool 681 Localizing Your Strings File 683 Testing Your String Localization 684 Localizing Interfaces Using Localizable Strings 685 Localizing Settings.bundle 685 Other Code Considerations 688 Localizing Your App's Name 689 Summary 690 Chapter 23: Faster, Faster 691 Performance Optimization 692 Fixing a Slow Tap 693 Launching Instruments 694 Finding the Hot Spots 696 The Hubris of Experience 699 Picking the Low Hanging Fruit 700 Deferring Notifications 700 Once More into The Breach 701
xxhr Contents Precious Memory 702 Breaking MyStuff 703 Measuring Memory 704 Memory Instruments 706 Heed the Warnings 708 Stress Test, Round #2 710 Summary 712 Chapter 24: Twice As Nice 713 Concurrent Programming 714 Threads 715 Synchronization 715 Running Multiple Threads 716 Creating an Operation Queue 717 Adding an Operation 718 Measuring the Effects 718 Execution Order 720 Thread Safety 721 Don't Talk About Thread Safety 722 Not Sharing Is Caring, 722 Promise Me You'll Never Change 723 The Atomic Age 723 Concurrency Roundup 729 The Thread-Safe Landscape 729 Sending Messages To Main 729 Lock Objects 730 Deadlocks 731 Spin Locks 733 Further Reading 734 Summary 734 Index 735