Beginning iphone development, 7th edition

794 413 0
Beginning iphone development, 7th edition

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

Learn to build iPhone and iPad apps using the iOS SDK and Objective-C Beginning iPhone Development Exploring the iOS SDK David Mark | Jack Nutting | Kim Topley | Fredrik Olsson | Jeff LaMarche www.it-ebooks.info For your convenience Apress has placed some of the front matter material after the index Please use the Bookmarks and Contents at a Glance links to access them www.it-ebooks.info Contents at a Glance About the Authors�������������������������������������������������������������������������������������������������������������� xxi About the Technical Reviewer����������������������������������������������������������������������������������������� xxiii ■■Chapter 1: Welcome to the Jungle�������������������������������������������������������������������������������������1 ■■Chapter 2: Appeasing the Tiki Gods���������������������������������������������������������������������������������13 ■■Chapter 3: Handling Basic Interaction�����������������������������������������������������������������������������55 ■■Chapter 4: More User Interface Fun��������������������������������������������������������������������������������95 ■■Chapter 5: Rotation and Adaptive Layout����������������������������������������������������������������������145 ■■Chapter 6: Multiview Applications��������������������������������������������������������������������������������183 ■■Chapter 7: Tab Bars and Pickers�����������������������������������������������������������������������������������213 ■■Chapter 8: Introduction to Table Views�������������������������������������������������������������������������257 ■■Chapter 9: Navigation Controllers and Table Views�������������������������������������������������������313 ■■Chapter 10: Collection View������������������������������������������������������������������������������������������349 ■■Chapter 11: Using Split Views and Popovers�����������������������������������������������������������������363 ■■Chapter 12: Application Settings and User Defaults������������������������������������������������������405 ■■Chapter 13: Basic Data Persistence������������������������������������������������������������������������������445 ■■Chapter 14: Documents and iCloud�������������������������������������������������������������������������������491 v www.it-ebooks.info vi Contents at a Glance ■■Chapter 15: Grand Central Dispatch, Background Processing, and You������������������������529 ■■Chapter 16: Drawing with Core Graphics����������������������������������������������������������������������565 ■■Chapter 17: Getting Started with Sprite Kit�������������������������������������������������������������������595 ■■Chapter 18: Taps, Touches, and Gestures����������������������������������������������������������������������645 ■■Chapter 19: Where Am I? Finding Your Way with Core Location and Map Kit���������������677 ■■Chapter 20: Whee! Gyro and Accelerometer!����������������������������������������������������������������699 ■■Chapter 21: The Camera and Photo Library�������������������������������������������������������������������727 ■■Chapter 22: Application Localization�����������������������������������������������������������������������������741 Index���������������������������������������������������������������������������������������������������������������������������������767 www.it-ebooks.info Chapter Welcome to the Jungle So, you want to write iPhone, iPod touch, and iPad applications? Well, we can’t say that we blame you iOS—the core software of all of these devices—is an exciting platform that has been seeing explosive growth since it first came out in 2007 The rise of the mobile software platform means that people are using software everywhere they go With the release of iOS 8, Xcode 6, and the latest incarnation of the iOS software development kit (SDK), things have only gotten better and more interesting What This Book Is This book is a guide to help you get started down the path to creating your own iOS applications Our goal is to get you past the initial difficulties to help you understand the way iOS applications work and how they are built As you work your way through this book, you will create a number of small applications, each designed to highlight specific iOS features and to show you how to control or interact with those features If you combine the foundation you’ll gain through this book with your own creativity and determination, and then add in the extensive and well-written documentation provided by Apple, you’ll have everything you need to build your own professional iPhone and iPad applications Tip  Dave, Jack, Jeff, and Fredrik have set up a forum for this book It’s a great place to meet like-minded folks, get your questions answered, and even answer other people’s questions The forum is at http://forum.learncocoa.org Be sure to check it out! www.it-ebooks.info CHAPTER 1: Welcome to the Jungle What You Need Before you can begin writing software for iOS, you’ll need a few items For starters, you’ll need an Intel-based Macintosh running Mavericks (OS X 10.9) or Yosemite (OS X 10.10) or later Any recent Intel-based Macintosh computer—laptop or desktop—should work just fine To get access to the latest and greatest from Apple, you’ll also really need to sign up to become a registered iOS developer To create your developer account, just navigate to http://developer.apple.com/ios/ That will bring you to a page similar to the one shown in Figure 1-1 Figure 1-1.  Apple’s iOS Dev Center web site First, click Log In You’ll be prompted for your Apple ID If you don’t have an Apple ID, click Register, create such an ID, and then log in Once you are logged in, you’ll be taken to the main iOS development page You’ll find links to a wealth of documentation, videos, sample code, and the like—all dedicated to teaching you the finer points of iOS application development The most important tool you’ll be using to develop iOS applications is called Xcode Xcode is Apple’s integrated development environment (IDE) Xcode includes tools for creating and debugging source code, compiling applications, and performance-tuning the applications you’ve written You can download Xcode from the Mac App Store, which you can access from your Mac’s Apple menu www.it-ebooks.info CHAPTER 1: Welcome to the Jungle SDK VERSIONS AND SOURCE CODE FOR THE EXAMPLES As the versions of the SDK and Xcode evolve, the mechanism for downloading them has also been changing For the past few years, Apple has been publishing the current “stable” version of Xcode and the iOS SDK on the Mac App Store, while simultaneously providing developers the ability to download preview versions of upcoming releases from its developer site Bottom line: you usually want to download the latest released (non-beta) version of Xcode and the iOS SDK, so use the Mac App Store This book has been written to work with the latest version of Xcode and the SDK In some places, we have chosen to use new functions or methods introduced with iOS that are not available in earlier versions of the SDK We’ll be sure to point those situations out as they arise in this book Be sure to download the latest and greatest source code archive for examples from this book’s page at http://apress.com We’ll update the code as new versions of the SDK are released, so be sure to check the site periodically Developer Options The free Xcode download includes a simulator that will allow you to build and run iPhone and iPad apps on your Mac This is perfect for learning how to program for iOS However, the simulator does not support many hardware-dependent features, such as the accelerometer and camera Also, the free option will not allow you to install your applications onto a real iPhone or other device, and it does not give you the ability to distribute your applications on Apple’s App Store For those capabilities, you’ll need to sign up for one of the other options, which aren’t free:  The Standard program costs $99/year It provides a host of development tools and resources, technical support, distribution of your application via Apple’s App Store, and, most importantly, the ability to test and debug your code on an iOS device, rather than just in the simulator  The Enterprise program costs $299/year It is designed for companies developing proprietary, in-house iOS applications For more details on these programs, visit http://developer.apple.com/programs/ios and http://developer.apple.com/programs/ios/enterprise to compare the two Because iOS supports an always-connected mobile device that uses other companies’ wireless infrastructures, Apple has needed to place far more restrictions on iOS developers than it ever has on Mac developers (who are able—at the moment, anyway—to write and distribute programs with absolutely no oversight or approval from Apple) Even though the iPod touch and the Wi-Fi-only versions of the iPad don’t use anyone else’s infrastructure, they’re still subject to these same restrictions Apple has not added restrictions to be mean, but rather as an attempt to minimize the chances of malicious or poorly written programs being distributed that could degrade performance on the shared network Developing for iOS may appear to present a lot of hoops to jump through, but Apple has expended quite an effort to make the process as painless as possible And also consider that $99 is still much less expensive than buying, for example, any of the paid versions of Visual Studio, which is Microsoft’s software development IDE www.it-ebooks.info CHAPTER 1: Welcome to the Jungle This may seem obvious, but you’ll also need an iPhone, iPod touch, or iPad While much of your code can be tested using the iOS simulator, not all programs can be And even those that can run on the simulator really need to be thoroughly tested on an actual device before you ever consider releasing your application to the public Note  If you are going to sign up for the Standard or Enterprise program, you should it right now The approval process can take a while, and you’ll need that approval to be able to run your applications on an actual device Don’t worry, though, because while you are waiting, you can run all the projects in the first several chapters and the majority of the applications in this book on the iOS simulator What You Need to Know This book assumes that you already have some programming knowledge It assumes that you understand the fundamentals of programming in general and object-oriented programming in particular (you know what classes, objects, loops, and variables are, for example) It also assumes that you are familiar with the Objective-C programming language Cocoa Touch—the part of the SDK that you will be working with through most of this book—uses the latest version of Objective-C, which contains several new features not present in earlier versions But don’t worry if you’re not familiar with the more recent additions to the Objective-C language We highlight any of the new language features that we take advantage of, and explain how they work and why we are using them You should also be familiar with iOS itself, as a user Just as you would with any platform for which you wanted to write an application, get to know the nuances and quirks of the iPhone, iPad, or iPod touch Take the time to get familiar with the iOS interface and with the way Apple’s iPhone and/or iPad applications look and feel NEW TO OBJECTIVE-C? If you have not programmed in Objective-C before, here are a few resources to help you get started: • Learn Objective-C on the Mac: For OS X and iOS (Apress, 2012): This is an excellent and approachable introduction to Objective-C by Mac-programming experts Scott Knaster, Waqar Malik, and Mark Dalrymple You can find more information at www.apress.com/book/ view/9781430241881 • Programming with Objective-C: A Primer : This is Apple’s introduction to the language You can find more information at https://developer.apple.com/library/mac/documentation/ cocoa/conceptual/ProgrammingWithObjectiveC www.it-ebooks.info CHAPTER 1: Welcome to the Jungle What’s Different About Coding for iOS? If you have never programmed in Cocoa or its predecessors NeXTSTEP or OpenStep, you may find Cocoa Touch—the application framework you’ll be using to write iOS applications—a little alien It has some fundamental differences from other common application frameworks, such as those used when building NET or Java applications Don’t worry too much if you feel a little lost at first Just keep plugging away at the exercises, and it will all start to fall into place after a while If you have written programs using Cocoa or NeXTSTEP, a lot in the iOS SDK will be familiar to you A great many classes are unchanged from the versions that are used to develop for OS X Even those that are different tend to follow the same basic principles and similar design patterns However, several differences exist between Cocoa and Cocoa Touch Regardless of your background, you need to keep in mind some key differences between iOS development and desktop application development These differences are discussed in the following sections Only One Active Application On iOS, only one application can be active and displayed on the screen at any given time Since iOS 4, applications have been able to run in the background after the user presses the Home button, but even that is limited to a narrow set of situations, and you must code for it specifically (you’ll see exactly how to that in Chapter 15) When your application isn’t active or running in the background, it doesn’t receive any attention whatsoever from the CPU, which will wreak havoc with open network connections and the like iOS allows background processing, but making your apps play nicely in this situation will require some effort on your part Only One Window Desktop and laptop operating systems allow many running programs to coexist, each with the ability to create and control multiple windows However, unless you attach an external screen or use AirPlay, and your application is coded to handle more than one screen, iOS gives your application just one “window” to work with All of your application’s interaction with the user takes place inside this one window, and its size is fixed at the size of the screen Limited Access Programs on a desktop or laptop computer pretty much have access to everything the user who launched them does However, iOS seriously restricts what your application can access You can read and write files only from the part of iOS’s file system that was created for your application This area is called your application’s sandbox Your sandbox is where your application will store documents, preferences, and every other kind of data it may need to retain Your application is also constrained in some other ways You will not be able to access low-number network ports on iOS, for example, or anything else that would typically require root or administrative access on a desktop computer www.it-ebooks.info CHAPTER 1: Welcome to the Jungle Limited Response Time Because of the way it is used, iOS needs to be snappy, and it expects the same of your application When your program is launched, you need to get your application open, the preferences and data loaded, and the main view shown on the screen as fast as possible—in no more than a few seconds At any time when your program is running, it may have the rug pulled out from under it If the user presses the Home button, iOS goes home, and you must quickly save everything before iOS suspends your application in the background If you take longer than five seconds to save and give up control, your application process will be killed, regardless of whether you finished saving There is an API that allows your app to ask for additional time to work when it’s about to go dark, but you’ve got to know how to use it Limited Screen Size The iPhone’s screen is really nice When introduced, it was the highest resolution screen available on a handheld consumer device, by far But even today, the iPhone display isn’t all that big, and as a result, you have a lot less room to work with than on modern computers The screen was just 320 × 480 on the first few iPhone generations, and it was later doubled in both directions to 640 × 960 with the introduction of the iPhone 4’s Retina display Today, the screen of the largest iPhone (the iPhone Plus) measures 1080 × 1920 pixels That sounds like a decent number of pixels, but keep in mind that these high-density displays (for which Apple uses the term “Retina”) are crammed into pretty small form factors, which has a big impact on the kinds of applications and interactivity you can offer on an iPhone and even an iPad Table 1-1 lists the sizes of the screens of all of the devices that are supported by iOS at the time of writing Table 1-1.  iOS Device Screen Sizes Device Hardware Size Software Size Scaling Factor iPhone 4s 640 × 960 320 × 480 2x iPhone and 5s 640 × 1136 320 × 568 2x iPhone 750 × 1334 375 × 667 2x 1080 × 1920 414 × 736 3x 768 × 1024 768 × 1024 1x 1536 × 2048 768 × 1024 2x iPhone Plus iPad and iPad mini iPad Air, iPad Retina, and iPad mini Retina The hardware size is the actual physical size of the screen in pixels However, when writing software, the size that really matters is the one in the Software Size column As you can see, in most cases, the software size is only half that of the actual hardware This situation came about when Apple introduced the first Retina device, which had twice as many pixels in each direction as its predecessor If Apple had done nothing special, all existing applications would have been drawn at half-scale on the new Retina screen, which would have made them unusable So Apple chose to internally scale everything that applications draw by a factor of 2, so that they would fill the new screen without any code changes This internal scaling by a factor of applies to all devices with a Retina display, apart from the iPhone Plus, which has a higher-density screen that requires a www.it-ebooks.info Contents ix ■■Chapter 4: More User Interface Fun��������������������������������������������������������������������������������95 A Screen Full of Controls�������������������������������������������������������������������������������������������������������������95 Active, Static, and Passive Controls���������������������������������������������������������������������������������������������99 Creating the Application������������������������������������������������������������������������������������������������������������101 Implementing the Image View and Text Fields��������������������������������������������������������������������������101 Adding the Image View�������������������������������������������������������������������������������������������������������������������������������������� 102 Resizing the Image View����������������������������������������������������������������������������������������������������������������������������������� 104 Setting View Attributes�������������������������������������������������������������������������������������������������������������������������������������� 106 Adding the Text Fields���������������������������������������������������������������������������������������������������������������������������������������� 109 Adding Constraints�������������������������������������������������������������������������������������������������������������������������������������������� 116 Creating and Connecting Outlets����������������������������������������������������������������������������������������������������������������������� 117 Closing the Keyboard����������������������������������������������������������������������������������������������������������������119 Closing the Keyboard When Done Is Tapped������������������������������������������������������������������������������������������������������ 121 Touching the Background to Close the Keyboard���������������������������������������������������������������������������������������������� 121 Adding the Slider and Label������������������������������������������������������������������������������������������������������������������������������ 124 Adding More Constraints����������������������������������������������������������������������������������������������������������������������������������� 126 Creating and Connecting the Actions and Outlets��������������������������������������������������������������������������������������������� 126 Implementing the Action Method����������������������������������������������������������������������������������������������������������������������� 126 Implementing the Switches, Button, and Segmented Control���������������������������������������������������127 Adding Two Labeled Switches��������������������������������������������������������������������������������������������������������������������������� 129 Connecting and Creating Outlets and Actions��������������������������������������������������������������������������������������������������� 131 Implementing the Switch Actions���������������������������������������������������������������������������������������������������������������������� 131 Spiffing Up the Button���������������������������������������������������������������������������������������������������������������133 Stretchable Images�������������������������������������������������������������������������������������������������������������������������������������������� 134 Control States���������������������������������������������������������������������������������������������������������������������������������������������������� 135 Connecting and Creating the Button Outlets and Actions���������������������������������������������������������������������������������� 136 Implementing the Segmented Control Action����������������������������������������������������������������������������136 Implementing the Action Sheet and Alert����������������������������������������������������������������������������������137 Showing an Action Sheet����������������������������������������������������������������������������������������������������������������������������������� 138 Showing an Alert����������������������������������������������������������������������������������������������������������������������������������������������� 141 Crossing the Finish Line������������������������������������������������������������������������������������������������������������143 www.it-ebooks.info x Contents ■■Chapter 5: Rotation and Adaptive Layout����������������������������������������������������������������������145 The Mechanics of Rotation��������������������������������������������������������������������������������������������������������146 Points, Pixels, and the Retina Display���������������������������������������������������������������������������������������������������������������� 147 Handling Rotation���������������������������������������������������������������������������������������������������������������������������������������������� 148 Choosing Your View Orientations�����������������������������������������������������������������������������������������������148 Supported Orientations at the App Level����������������������������������������������������������������������������������������������������������� 148 Per-Controller Rotation Support������������������������������������������������������������������������������������������������������������������������� 150 Designing an Interface Using Constraints���������������������������������������������������������������������������������152 Overriding Default Constraints�������������������������������������������������������������������������������������������������������������������������� 156 Full-Width Labels����������������������������������������������������������������������������������������������������������������������������������������������� 158 Creating Adaptive Layouts���������������������������������������������������������������������������������������������������������161 The Restructure Application������������������������������������������������������������������������������������������������������������������������������ 162 Size Classes������������������������������������������������������������������������������������������������������������������������������������������������������� 166 Size Classes and Storyboards��������������������������������������������������������������������������������������������������������������������������� 169 Creating the iPhone Landscape Layout������������������������������������������������������������������������������������������������������������� 172 Adding the iPad Layout�������������������������������������������������������������������������������������������������������������������������������������� 179 Rotating Out of Here������������������������������������������������������������������������������������������������������������������182 ■■Chapter 6: Multiview Applications��������������������������������������������������������������������������������183 Common Types of Multiview Apps���������������������������������������������������������������������������������������������183 The Architecture of a Multiview Application������������������������������������������������������������������������������188 The Root Controller�������������������������������������������������������������������������������������������������������������������������������������������� 191 Anatomy of a Content View�������������������������������������������������������������������������������������������������������������������������������� 192 Building View Switcher�������������������������������������������������������������������������������������������������������������192 Renaming the View Controller��������������������������������������������������������������������������������������������������������������������������� 193 Adding the Content View Controllers����������������������������������������������������������������������������������������������������������������� 195 Modifying SwitchingViewController.m��������������������������������������������������������������������������������������������������������������� 196 Building a View with a Toolbar��������������������������������������������������������������������������������������������������������������������������� 196 Linking the Toolbar Button to the View Controller��������������������������������������������������������������������������������������������� 199 Writing the Root View Controller������������������������������������������������������������������������������������������������������������������������ 201 www.it-ebooks.info Contents xi Implementing the Content Views����������������������������������������������������������������������������������������������������������������������� 206 Animating the Transition������������������������������������������������������������������������������������������������������������������������������������ 210 Switching Off�����������������������������������������������������������������������������������������������������������������������������212 ■■Chapter 7: Tab Bars and Pickers�����������������������������������������������������������������������������������213 The Pickers Application�������������������������������������������������������������������������������������������������������������214 Delegates and Data Sources�����������������������������������������������������������������������������������������������������219 Creating the Pickers Application�����������������������������������������������������������������������������������������������220 Creating the View Controllers���������������������������������������������������������������������������������������������������������������������������� 220 Creating the Tab Bar Controller�������������������������������������������������������������������������������������������������������������������������� 221 The Initial Test Run�������������������������������������������������������������������������������������������������������������������������������������������� 225 Implementing the Date Picker���������������������������������������������������������������������������������������������������226 Implementing the Single-Component Picker�����������������������������������������������������������������������������229 Building the View����������������������������������������������������������������������������������������������������������������������������������������������� 229 Implementing the Controller As a Data Source and Delegate���������������������������������������������������������������������������� 231 Implementing a Multicomponent Picker�����������������������������������������������������������������������������������235 Declaring Outlets and Actions��������������������������������������������������������������������������������������������������������������������������� 235 Building the View����������������������������������������������������������������������������������������������������������������������������������������������� 236 Implementing the Controller������������������������������������������������������������������������������������������������������������������������������ 236 Implementing Dependent Components�������������������������������������������������������������������������������������239 Creating a Simple Game with a Custom Picker�������������������������������������������������������������������������247 Writing the Controller Header File��������������������������������������������������������������������������������������������������������������������� 247 Building the View����������������������������������������������������������������������������������������������������������������������������������������������� 248 Implementing the Controller������������������������������������������������������������������������������������������������������������������������������ 249 Final Details������������������������������������������������������������������������������������������������������������������������������������������������������� 253 Final Spin����������������������������������������������������������������������������������������������������������������������������������256 ■■Chapter 8: Introduction to Table Views�������������������������������������������������������������������������257 Table View Basics����������������������������������������������������������������������������������������������������������������������258 Table Views and Table View Cells���������������������������������������������������������������������������������������������������������������������� 258 Grouped and Plain Tables���������������������������������������������������������������������������������������������������������������������������������� 260 www.it-ebooks.info xii Contents Implementing a Simple Table����������������������������������������������������������������������������������������������������261 Designing the View�������������������������������������������������������������������������������������������������������������������������������������������� 261 Writing the Controller���������������������������������������������������������������������������������������������������������������������������������������� 264 Adding an Image������������������������������������������������������������������������������������������������������������������������������������������������ 268 Using Table View Cell Styles������������������������������������������������������������������������������������������������������������������������������ 271 Setting the Indent Level������������������������������������������������������������������������������������������������������������������������������������� 273 Handling Row Selection������������������������������������������������������������������������������������������������������������������������������������� 274 Changing the Font Size and Row Height����������������������������������������������������������������������������������������������������������� 277 Customizing Table View Cells����������������������������������������������������������������������������������������������������280 Adding Subviews to the Table View Cell ����������������������������������������������������������������������������������������������������������� 280 Creating a UITableViewCell Subclass����������������������������������������������������������������������������������������������������������������� 281 Loading a UITableViewCell from a Nib��������������������������������������������������������������������������������������������������������������� 286 Grouped and Indexed Sections��������������������������������������������������������������������������������������������������292 Building the View����������������������������������������������������������������������������������������������������������������������������������������������� 292 Importing the Data��������������������������������������������������������������������������������������������������������������������������������������������� 293 Implementing the Controller������������������������������������������������������������������������������������������������������������������������������ 294 Adding an Index������������������������������������������������������������������������������������������������������������������������������������������������� 298 Implementing a Search Bar�������������������������������������������������������������������������������������������������������299 How Many Tables?: View Debugging�����������������������������������������������������������������������������������������309 Putting It All on the Table�����������������������������������������������������������������������������������������������������������312 ■■Chapter 9: Navigation Controllers and Table Views�������������������������������������������������������313 Navigation Controller Basics�����������������������������������������������������������������������������������������������������314 Stacky Goodness����������������������������������������������������������������������������������������������������������������������������������������������� 314 A Stack of Controllers���������������������������������������������������������������������������������������������������������������������������������������� 314 Fonts: A Simple Font Browser���������������������������������������������������������������������������������������������������316 Meet the Subcontrollers������������������������������������������������������������������������������������������������������������������������������������ 317 The Fonts Application’s Skeleton����������������������������������������������������������������������������������������������������������������������� 319 Creating the Root View Controller���������������������������������������������������������������������������������������������324 Initial Storyboard Setup�������������������������������������������������������������������������������������������������������������328 www.it-ebooks.info Contents xiii First Subcontroller: The Font List View��������������������������������������������������������������������������������������330 Storyboarding the Font List������������������������������������������������������������������������������������������������������������������������������� 332 Making the Root View Controller Prepare for Segues���������������������������������������������������������������������������������������� 333 Creating the Font Sizes View Controller������������������������������������������������������������������������������������334 Storyboarding the Font Sizes View Controller��������������������������������������������������������������������������������������������������� 336 Making the Font List View Controller Prepare for Segues��������������������������������������������������������������������������������� 336 Creating the Font Info View Controller���������������������������������������������������������������������������������������337 Storyboarding the Font Info View Controller������������������������������������������������������������������������������������������������������ 338 Setting Up Constraints��������������������������������������������������������������������������������������������������������������������������������������� 340 Adapting the Font List View Controller for Multiple Segues������������������������������������������������������������������������������ 341 My Favorite Fonts���������������������������������������������������������������������������������������������������������������������������������������������� 342 Table View Niceties�������������������������������������������������������������������������������������������������������������������343 Implementing Swipe-to-Delete������������������������������������������������������������������������������������������������������������������������� 343 Implementing Drag-to-Reorder������������������������������������������������������������������������������������������������������������������������� 345 Breaking the Tape����������������������������������������������������������������������������������������������������������������������347 ■■Chapter 10: Collection View������������������������������������������������������������������������������������������349 Creating the DialogViewer Project���������������������������������������������������������������������������������������������349 Fixing the View Controller’s Class���������������������������������������������������������������������������������������������351 Defining Custom Cells���������������������������������������������������������������������������������������������������������������351 Configuring the View Controller�������������������������������������������������������������������������������������������������354 Providing Content Cells�������������������������������������������������������������������������������������������������������������356 Making the Layout Flow������������������������������������������������������������������������������������������������������������357 Providing Header Views�������������������������������������������������������������������������������������������������������������360 ■■Chapter 11: Using Split Views and Popovers�����������������������������������������������������������������363 Building Master-Detail Applications with UISplitViewController������������������������������������������������366 The Storyboard Defines the Structure��������������������������������������������������������������������������������������������������������������� 368 The Code Defines the Functionality������������������������������������������������������������������������������������������������������������������� 370 How the Master-Detail Template Application Works������������������������������������������������������������������������������������������ 377 Here Come the Presidents���������������������������������������������������������������������������������������������������������379 Creating Your Own Popover�������������������������������������������������������������������������������������������������������387 www.it-ebooks.info xiv Contents Split Views on the iPhone����������������������������������������������������������������������������������������������������������394 Split Views on the iPhone Plus����������������������������������������������������������������������������������������������������������������������� 397 Getting the iPhone Plus Behavior on All iPhones�������������������������������������������������������������������������������������������� 398 Customizing the Split View��������������������������������������������������������������������������������������������������������402 Time to Wrap Up and Split���������������������������������������������������������������������������������������������������������404 ■■Chapter 12: Application Settings and User Defaults������������������������������������������������������405 Getting to Know Your Settings Bundle���������������������������������������������������������������������������������������405 The Bridge Control Application��������������������������������������������������������������������������������������������������407 Creating the Project������������������������������������������������������������������������������������������������������������������������������������������� 412 Working with the Settings Bundle��������������������������������������������������������������������������������������������������������������������� 413 Reading Settings in Our Application������������������������������������������������������������������������������������������������������������������ 431 Changing Defaults from Our Application����������������������������������������������������������������������������������������������������������� 435 Registering Default Values��������������������������������������������������������������������������������������������������������������������������������� 438 Keeping It Real�������������������������������������������������������������������������������������������������������������������������������������������������� 439 Switching to the Settings Application���������������������������������������������������������������������������������������442 Beam Me Up, Scotty������������������������������������������������������������������������������������������������������������������443 ■■Chapter 13: Basic Data Persistence������������������������������������������������������������������������������445 Your Application’s Sandbox�������������������������������������������������������������������������������������������������������446 Getting the Documents and Library Directories������������������������������������������������������������������������������������������������� 448 Getting the tmp Directory���������������������������������������������������������������������������������������������������������������������������������� 449 File-Saving Strategies���������������������������������������������������������������������������������������������������������������449 Single-File Persistence�������������������������������������������������������������������������������������������������������������������������������������� 449 Multiple-File Persistence����������������������������������������������������������������������������������������������������������������������������������� 450 Using Property Lists������������������������������������������������������������������������������������������������������������������450 Property List Serialization���������������������������������������������������������������������������������������������������������������������������������� 450 The First Version of the Persistence Application����������������������������������������������������������������������������������������������� 452 Archiving Model Objects������������������������������������������������������������������������������������������������������������458 Conforming to NSCoding����������������������������������������������������������������������������������������������������������������������������������� 458 Implementing NSCopying���������������������������������������������������������������������������������������������������������������������������������� 459 Archiving and Unarchiving Data Objects����������������������������������������������������������������������������������������������������������� 460 The Archiving Application���������������������������������������������������������������������������������������������������������������������������������� 461 www.it-ebooks.info Contents xv Using iOS’s Embedded SQLite3�������������������������������������������������������������������������������������������������465 Creating or Opening the Database��������������������������������������������������������������������������������������������������������������������� 466 Using Bind Variables������������������������������������������������������������������������������������������������������������������������������������������ 467 The SQLite3 Application������������������������������������������������������������������������������������������������������������������������������������� 468 Using Core Data�������������������������������������������������������������������������������������������������������������������������475 Entities and Managed Objects��������������������������������������������������������������������������������������������������������������������������� 476 The Core Data Application��������������������������������������������������������������������������������������������������������������������������������� 480 Persistence Rewarded��������������������������������������������������������������������������������������������������������������490 ■■Chapter 14: Documents and iCloud�������������������������������������������������������������������������������491 Managing Document Storage with UIDocument�����������������������������������������������������������������������492 Building TinyPix������������������������������������������������������������������������������������������������������������������������������������������������� 492 Creating TinyPixDocument��������������������������������������������������������������������������������������������������������������������������������� 493 Code Master������������������������������������������������������������������������������������������������������������������������������������������������������ 497 Initial Storyboarding������������������������������������������������������������������������������������������������������������������������������������������ 505 Creating TinyPixView����������������������������������������������������������������������������������������������������������������������������������������� 508 Storyboard Detailing������������������������������������������������������������������������������������������������������������������������������������������ 513 Adding iCloud Support���������������������������������������������������������������������������������������������������������������518 Creating a Provisioning Profile�������������������������������������������������������������������������������������������������������������������������� 518 How to Query����������������������������������������������������������������������������������������������������������������������������������������������������� 521 Save Where?������������������������������������������������������������������������������������������������������������������������������������������������������ 523 Storing Preferences on iCloud��������������������������������������������������������������������������������������������������������������������������� 524 What We Didn’t Cover����������������������������������������������������������������������������������������������������������������526 ■■Chapter 15: Grand Central Dispatch, Background Processing, and You������������������������529 Grand Central Dispatch�������������������������������������������������������������������������������������������������������������529 Introducing SlowWorker������������������������������������������������������������������������������������������������������������530 Threading Basics�����������������������������������������������������������������������������������������������������������������������534 Units of Work�����������������������������������������������������������������������������������������������������������������������������535 GCD: Low-Level Queuing�����������������������������������������������������������������������������������������������������������535 Becoming a Blockhead�������������������������������������������������������������������������������������������������������������������������������������� 536 Improving SlowWorker�������������������������������������������������������������������������������������������������������������������������������������� 537 www.it-ebooks.info xvi Contents Background Processing������������������������������������������������������������������������������������������������������������543 Application Life Cycle���������������������������������������������������������������������������������������������������������������������������������������� 544 State-Change Notifications�������������������������������������������������������������������������������������������������������������������������������� 545 Creating State Lab��������������������������������������������������������������������������������������������������������������������������������������������� 547 Exploring Execution States�������������������������������������������������������������������������������������������������������������������������������� 548 Using Execution State Changes������������������������������������������������������������������������������������������������������������������������� 550 Handling the Inactive State������������������������������������������������������������������������������������������������������������������������������� 551 Handling the Background State������������������������������������������������������������������������������������������������������������������������� 555 Grand Central Dispatch, Over and Out���������������������������������������������������������������������������������������564 ■■Chapter 16: Drawing with Core Graphics����������������������������������������������������������������������565 Paint the World��������������������������������������������������������������������������������������������������������������������������565 The Quartz 2D Approach to Drawing�����������������������������������������������������������������������������������������566 Quartz 2D’s Graphics Contexts�������������������������������������������������������������������������������������������������������������������������� 566 The Coordinate System������������������������������������������������������������������������������������������������������������������������������������� 567 Specifying Colors����������������������������������������������������������������������������������������������������������������������������������������������� 568 Drawing Images in Context�������������������������������������������������������������������������������������������������������������������������������� 571 Drawing Shapes: Polygons, Lines, and Curves�������������������������������������������������������������������������������������������������� 571 Quartz 2D Tool Sampler: Patterns, Gradients, and Dash Patterns���������������������������������������������������������������������� 571 The QuartzFun Application��������������������������������������������������������������������������������������������������������572 Setting Up the QuartzFun Application���������������������������������������������������������������������������������������������������������������� 573 Adding Quartz 2D Drawing Code����������������������������������������������������������������������������������������������������������������������� 584 Optimizing the QuartzFun Application��������������������������������������������������������������������������������������������������������������� 590 Drawing to a Close��������������������������������������������������������������������������������������������������������������������594 ■■Chapter 17: Getting Started with Sprite Kit�������������������������������������������������������������������595 Simple Beginnings��������������������������������������������������������������������������������������������������������������������596 Initial Scene Customization������������������������������������������������������������������������������������������������������������������������������� 602 Player Movement����������������������������������������������������������������������������������������������������������������������606 Adding the Player to the Scene������������������������������������������������������������������������������������������������������������������������� 607 Handling Touches: Player Movement����������������������������������������������������������������������������������������������������������������� 609 Geometry Calculations��������������������������������������������������������������������������������������������������������������������������������������� 610 Wobbly Bits�������������������������������������������������������������������������������������������������������������������������������������������������������� 611 www.it-ebooks.info Contents xvii Creating Your Enemies��������������������������������������������������������������������������������������������������������������612 Putting Enemies in the Scene���������������������������������������������������������������������������������������������������613 Start Shooting���������������������������������������������������������������������������������������������������������������������������614 Defining Your Physics Categories���������������������������������������������������������������������������������������������������������������������� 615 Creating the BulletNode Class��������������������������������������������������������������������������������������������������������������������������� 615 Applying Physics������������������������������������������������������������������������������������������������������������������������������������������������ 617 Adding Bullets to the Scene������������������������������������������������������������������������������������������������������������������������������ 617 Attacking Enemies with Physics�����������������������������������������������������������������������������������������������620 Finishing Levels�������������������������������������������������������������������������������������������������������������������������621 Keeping Tabs on the Enemies���������������������������������������������������������������������������������������������������������������������������� 621 Transitioning to the Next Levels������������������������������������������������������������������������������������������������������������������������ 622 Customizing Collisions��������������������������������������������������������������������������������������������������������������623 Adding a Category to SKNode���������������������������������������������������������������������������������������������������������������������������� 625 Adding Custom Collision Behavior to Enemies�������������������������������������������������������������������������������������������������� 627 Showing Accurate Player Lives������������������������������������������������������������������������������������������������������������������������� 627 Spicing Things Up with Particles�����������������������������������������������������������������������������������������������629 Your First Particle���������������������������������������������������������������������������������������������������������������������������������������������� 630 Putting Particles into the Scene������������������������������������������������������������������������������������������������������������������������ 632 The End Game���������������������������������������������������������������������������������������������������������������������������634 At Last, a Beginning: Create a StartScene���������������������������������������������������������������������������������637 A Sound Is Worth a Thousand Pictures��������������������������������������������������������������������������������������639 Making the Game a Little Harder: Force Fields�������������������������������������������������������������������������640 Game On������������������������������������������������������������������������������������������������������������������������������������644 ■■Chapter 18: Taps, Touches, and Gestures����������������������������������������������������������������������645 Multitouch Terminology�������������������������������������������������������������������������������������������������������������645 The Responder Chain����������������������������������������������������������������������������������������������������������������646 Responding to Events���������������������������������������������������������������������������������������������������������������������������������������� 646 Forwarding an Event: Keeping the Responder Chain Alive�������������������������������������������������������������������������������� 648 The Multitouch Architecture������������������������������������������������������������������������������������������������������648 The Four Touch Notification Methods����������������������������������������������������������������������������������������649 www.it-ebooks.info xviii Contents The TouchExplorer Application��������������������������������������������������������������������������������������������������650 The Swipes Application�������������������������������������������������������������������������������������������������������������654 Using Touch Events to Detect Swipes���������������������������������������������������������������������������������������������������������������� 655 Automatic Gesture Recognition������������������������������������������������������������������������������������������������������������������������� 658 Implementing Multiple Swipes�������������������������������������������������������������������������������������������������������������������������� 659 Detecting Multiple Taps�������������������������������������������������������������������������������������������������������������662 Detecting Pinch and Rotation����������������������������������������������������������������������������������������������������667 Defining Custom Gestures���������������������������������������������������������������������������������������������������������671 The CheckPlease Application����������������������������������������������������������������������������������������������������������������������������� 672 The CheckPlease Touch Methods 674 Garỗon? Check, Please!676 Chapter 19: Where Am I? Finding Your Way with Core Location and Map Kit���������������677 The Location Manager���������������������������������������������������������������������������������������������������������������678 Setting the Desired Accuracy���������������������������������������������������������������������������������������������������������������������������� 678 Setting the Distance Filter��������������������������������������������������������������������������������������������������������������������������������� 679 Getting Permission to Use Location Services���������������������������������������������������������������������������������������������������� 679 Starting the Location Manager�������������������������������������������������������������������������������������������������������������������������� 679 Using the Location Manager Wisely������������������������������������������������������������������������������������������������������������������� 680 The Location Manager Delegate������������������������������������������������������������������������������������������������680 Getting Location Updates���������������������������������������������������������������������������������������������������������������������������������� 680 Getting Latitude and Longitude Using CLLocation��������������������������������������������������������������������������������������������� 680 Error Notifications���������������������������������������������������������������������������������������������������������������������������������������������� 682 Trying Out Core Location�����������������������������������������������������������������������������������������������������������683 Using Location Manager Updates���������������������������������������������������������������������������������������������������������������������� 689 Visualizing Your Movement on a Map����������������������������������������������������������������������������������������691 Changing Location Service Permissions����������������������������������������������������������������������������������������������������������� 695 Wherever You Go, There You Are������������������������������������������������������������������������������������������������697 www.it-ebooks.info Contents xix ■■Chapter 20: Whee! Gyro and Accelerometer!����������������������������������������������������������������699 Accelerometer Physics��������������������������������������������������������������������������������������������������������������699 Don’t Forget Rotation����������������������������������������������������������������������������������������������������������������701 Core Motion and the Motion Manager���������������������������������������������������������������������������������������701 Event-Based Motion������������������������������������������������������������������������������������������������������������������������������������������ 702 Proactive Motion Access����������������������������������������������������������������������������������������������������������������������������������� 707 Gyroscope and Attitude Results������������������������������������������������������������������������������������������������������������������������� 710 Accelerometer Results�������������������������������������������������������������������������������������������������������������������������������������� 711 Detecting Shakes����������������������������������������������������������������������������������������������������������������������712 Baked-In Shaking���������������������������������������������������������������������������������������������������������������������������������������������� 713 Shake and Break����������������������������������������������������������������������������������������������������������������������������������������������� 714 Accelerometer As Directional Controller�����������������������������������������������������������������������������������718 Rolling Marbles�������������������������������������������������������������������������������������������������������������������������������������������������� 718 Writing the Ball View����������������������������������������������������������������������������������������������������������������������������������������� 721 Calculating Ball Movement�������������������������������������������������������������������������������������������������������������������������������� 724 Rolling On����������������������������������������������������������������������������������������������������������������������������������726 ■■Chapter 21: The Camera and Photo Library�������������������������������������������������������������������727 Using the Image Picker and UIImagePickerController���������������������������������������������������������������727 Using the Image Picker Controller��������������������������������������������������������������������������������������������������������������������� 727 Implementing the Image Picker Controller Delegate����������������������������������������������������������������������������������������� 730 Road Testing the Camera and Library���������������������������������������������������������������������������������������731 Designing the Interface������������������������������������������������������������������������������������������������������������������������������������� 732 Implementing the Camera View Controller�������������������������������������������������������������������������������������������������������� 735 It’s a Snap!��������������������������������������������������������������������������������������������������������������������������������739 ■■Chapter 22: Application Localization�����������������������������������������������������������������������������741 Localization Architecture�����������������������������������������������������������������������������������������������������������741 Strings Files������������������������������������������������������������������������������������������������������������������������������743 What’s in a Strings File?������������������������������������������������������������������������������������������������������������������������������������ 743 The Localized String Macro������������������������������������������������������������������������������������������������������������������������������� 744 www.it-ebooks.info xx Contents Real-World iOS: Localizing Your Application������������������������������������������������������������������������������745 Setting Up LocalizeMe��������������������������������������������������������������������������������������������������������������������������������������� 746 Trying Out LocalizeMe��������������������������������������������������������������������������������������������������������������������������������������� 749 Localizing the Project���������������������������������������������������������������������������������������������������������������������������������������� 751 Localizing the Storyboard���������������������������������������������������������������������������������������������������������������������������������� 754 Generating and Localizing a Strings File����������������������������������������������������������������������������������������������������������� 757 Previewing Localizations in Xcode��������������������������������������������������������������������������������������������762 Localizing the App Display Name���������������������������������������������������������������������������������������������������������������������� 764 Adding Another Localization������������������������������������������������������������������������������������������������������765 Auf Wiedersehen�����������������������������������������������������������������������������������������������������������������������766 Index���������������������������������������������������������������������������������������������������������������������������������767 www.it-ebooks.info About the Authors Dave Mark is a longtime Mac developer and author who has written a number of books on Mac and iOS development, including Beginning iOS Development (Apress, 2013), More iOS Development (Apress, 2013), Learn C on the Mac (Apress, 2013), Ultimate Mac Programming (Wiley, 1995), and the Macintosh Programming Primer series (Addison-Wesley, 1992) Dave was one of the founders of MartianCraft, an iOS and Android development house Dave loves the water and spends as much time as possible on it, in it, or near it He lives with his wife and three children in Virginia On Twitter, he’s @davemark Jack Nutting has been using Cocoa since the olden days, long before it was even called Cocoa He has used Cocoa and its predecessors to develop software for a wide range of industries and applications, including gaming, graphic design, online digital distribution, telecommunications, finance, publishing, and travel Jack has written several books on iOS and Mac development, including Beginning iOS Development (Apress, 2013), Learn Cocoa on the Mac (Apress, 2013), and Beginning iPad Development for iPhone Developers (Apress, 2010) Besides writing software and books, he also leads developer training and blogs from time to time at www.nuthole.com He’s @jacknutting on Twitter xxi www.it-ebooks.info xxii About the Authors Kim Topley is a software engineer with over 30 years of experience, ranging from mainframe microcode and the UNIX kernel to graphical user interfaces and mobile applications He is the author of five books on various aspects of Java and JavaFX and has been working with iOS since reading one of the first books published on the subject—the first edition of Beginning iPhone Development Fredrik Olsson has been using Cocoa since Mac OS X 10.1 and for iPhone since the unofficial toolchain He has had a long and varied career, ranging from real-time assembly to enterprise Java He is passionate about Objective-C for its elegance, Cocoa frameworks for its clarity, and both for creating a greater whole than their parts When away from a keyboard, Fredrik has spoken at conferences and led developer training You’ll find Fredrik on Twitter as @peylow Jeff LaMarche is a Mac and an iOS developer with more than 20 years of programming experience Jeff has written a number of iOS and Mac development books, including Beginning iOS Development (Apress, 2013) and More iOS Development (Apress, 2013) Jeff is a principal at MartianCraft, an iOS and Android development house He has written about Cocoa and Objective-C for MacTech magazine, as well as articles for Apple’s developer web site Jeff also writes about iOS development for his widely read blog at www.iphonedevelopment blogspot.com He can be found on Twitter as @jeff_lamarche www.it-ebooks.info About the Technical Reviewer Felipe Laso Marsetti is an iOS programmer working at Lextech Global Services He loves everything related to Apple, video games, cooking, and playing the violin, piano, or guitar In his spare time, Felipe loves to read and learn new programming languages or technologies Felipe likes to write on his blog at http://iFe.li, create iOS tutorials and articles as a member of www.raywenderlich.com, and work as a technical reviewer for Objective-C and iOS–related books You can find him on Twitter as @Airjordan12345, on Facebook under his name, or on App.net as @iFeli xxiii www.it-ebooks.info ... available right now have either 512MB (iPhone 4S, iPad 2, the original iPad mini, the latest iPod touch), or 1024MB of physical RAM (iPhone 5c, iPhone 5s, iPhone 6, iPhone Plus, iPad Air, iPad mini... Factor iPhone 4s 640 × 960 320 × 480 2x iPhone and 5s 640 × 1136 320 × 568 2x iPhone 750 × 1334 375 × 667 2x 1080 × 1920 414 × 736 3x 768 × 1024 768 × 1024 1x 1536 × 2048 768 × 1024 2x iPhone. .. first few iPhone generations, and it was later doubled in both directions to 640 × 960 with the introduction of the iPhone 4’s Retina display Today, the screen of the largest iPhone (the iPhone

Ngày đăng: 13/03/2019, 10:35

Mục lục

  • Beginning iPhone Development

    • Contents at a Glance

    • Contents

    • About the Authors

    • About the Technical Reviewer

    • Chapter 1: Welcome to the Jungle

      • What This Book Is

      • What You Need

        • Developer Options

        • What You Need to Know

        • What’s Different About Coding for iOS?

          • Only One Active Application

          • Only One Window

          • Limited Access

          • Limited Response Time

          • Limited Screen Size

          • Limited System Resources

          • No Garbage Collection, But…

          • Some New Stuff

          • A Different Approach

          • What’s in This Book

          • What’s New in This Update?

          • Are You Ready?

          • Chapter 2: Appeasing the Tiki Gods

            • Setting Up Your Project in Xcode

              • The Xcode Project Window

                • The Toolbar

Tài liệu cùng người dùng

Tài liệu liên quan