Learn iOS 8 app development, 2nd edition

751 357 0
Learn iOS 8 app development, 2nd 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 Swift, iOS SDK, and Cocoa Touch to start creating exciting iPhone and iPad apps Learn iOS App Development SECOND EDITION James Bucanek 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 Author�������������������������������������������������������������������������������������������������������������� xxv About the Technical Reviewer���������������������������������������������������������������������������������������� xxvii Acknowledgments����������������������������������������������������������������������������������������������������������� xxix Introduction��������������������������������������������������������������������������������������������������������������������� xxxi ■■Chapter 1: Got Tools?���������������������������������������������������������������������������������������������������������1 ■■Chapter 2: Boom! App�����������������������������������������������������������������������������������������������������17 ■■Chapter 3: Spin a Web�����������������������������������������������������������������������������������������������������53 ■■Chapter 4: Coming Events�����������������������������������������������������������������������������������������������87 ■■Chapter 5: Table Manners����������������������������������������������������������������������������������������������123 ■■Chapter 6: Object Lesson�����������������������������������������������������������������������������������������������163 ■■Chapter 7: Smile!�����������������������������������������������������������������������������������������������������������177 ■■Chapter 8: Model Citizen�����������������������������������������������������������������������������������������������203 ■■Chapter 9: Sweet, Sweet Music�������������������������������������������������������������������������������������239 ■■Chapter 10: Got Views?�������������������������������������������������������������������������������������������������279 ■■Chapter 11: Draw Me a Picture�������������������������������������������������������������������������������������317 ■■Chapter 12: There and Back Again��������������������������������������������������������������������������������361 v www.it-ebooks.info vi Contents at a Glance ■■Chapter 13: Sharing Is Caring���������������������������������������������������������������������������������������421 ■■Chapter 14: Game On!���������������������������������������������������������������������������������������������������437 ■■Chapter 15: If You Build It ���������������������������������������������������������������������������������������483 ■■Chapter 16: Apps with Attitude�������������������������������������������������������������������������������������505 ■■Chapter 17: Where Are You?������������������������������������������������������������������������������������������529 ■■Chapter 18: Remember Me?������������������������������������������������������������������������������������������553 ■■Chapter 19: Doc, You Meant Storage�����������������������������������������������������������������������������585 ■■Chapter 20: See Swift, See Swift Run����������������������������������������������������������������������������619 ■■Chapter 21: Frame Up����������������������������������������������������������������������������������������������������693 Index���������������������������������������������������������������������������������������������������������������������������������715 www.it-ebooks.info Introduction I’m standing on a street corner in San Francisco, a city I visit far too infrequently In my hand I hold an electronic device The device is receiving status updates about the city’s public transportation system in real time It is telling me that the F-line rail will arrive at the Market and 5th Street station in seven minutes It displays a map of the city and, by timing radio waves it receives from outer space, triangulates and displays my exact location on that map A magnetometer determines which direction I’m holding the device and uses that information to indicate the direction I should walk to meet the rail car in time to board it My friends call me, wondering when I will arrive A tiny video camera and microphone share my image and voice with them as I walk I’m meeting them at a gallery opening It’s an exhibition of new artwork, by artists from all over the world, created entirely using devices similar to the one I hold in my hand When I arrive, I use my device to share my experiences with friends and family back home, exchange contact information with people I meet, and look up restaurant suggestions for where we might eat later This is a true story A couple of decades ago, it would have been science fiction We live in a time in which personal electronics are changing how we work, travel, communicate, and experience the world A day doesn’t go by without someone discovering another novel use for them And while I’m sure you enjoy benefiting from this new technology, you’re reading this book because you want to participate in this revolution You want to create apps You’ve come to the right place Who Is This Book For? This book is for anyone who wants to learn the basic tools and techniques for creating exciting, dynamic applications for Apple products that run the iOS operating system As of this writing, that includes the iPad, iPhone, and iPod Touch This book assumes you are new to developing iOS apps and that you have limited programming experience If you’ve been learning Swift—Apple’s new programming language—that’s perfect If you know Objective-C, C, Java, C#, or C++, you shouldn’t have too much trouble following along, and there’s a Swift primer in Chapter 20 that you’ll want to read If you are completely new to xxxi www.it-ebooks.info xxxii Introduction programming computers, I suggest getting a basic Swift programming book—say, Swift for Absolute Beginners by Gary Bennett and Brad Lees—and read that first or in parallel iOS app are developed using Objective-C or Swift, but this book uses Swift exclusively This book will explain the fundamentals of how iOS apps are designed, built, and deployed You’ll pick up some good design habits, get some core programming skills, and learn your way around the development tools used to create apps This book is not an in-depth treatise on any one technology It’s designed to stimulate your imagination by giving you a firm grounding in building apps that use a variety of device capabilities, such as finding your location on a map, using the accelerometer, taking pictures with the built-in camera, creating dynamic animation, participating in social networks, and storing information in the cloud From there, you can leap beyond these examples to create the next great iOS app! Too Cool for School I’m an “old-school” programmer I learned programming from the bit up (literally) At the risk of dating myself, the first program I wrote was on a 4-bit microcontroller using toggle switches to input the machine instructions So, I pretty much knew everything there was to know about machine code before I started to program in “high-level” languages like BASIC and C All that was well behind me before I wrote my first graphical user interface (GUI) application for the (then-revolutionary) Macintosh computer While I value this accumulated knowledge, and much of it is still useful, I realize that a strict “ground-up” approach isn’t necessary to develop great apps for iOS today Many of the advances in software development over the past few decades have been in insulating the developer—that’s you—from the nitty-gritty details of CPU instructions, hardware interfaces, and software design This frees you to concentrate on harnessing these technologies to turn your idea into reality, rather than spending all of your time worrying about register allocations and memory management So, the exciting news is that you can jump right in and create full-featured iOS apps with only a minimal knowledge of computer programming or the underlying technologies that make them possible And that’s what this book is going to in the first couple of chapters—show you how to create an iOS app without any traditional programming whatsoever That’s not to say you don’t need these skills in order to master iOS development On the contrary; the more you know about programming, performance, and memory management, the more proficient you’re going to be What’s changed is that these skills aren’t the prerequisites that they once were Now, you can learn them in parallel while you explore new avenues of iOS development How to Use This Book This book embraces an “explore as go” approach Some chapters will walk you through the process of creating an iOS app that uses the camera or plays music These chapters may gloss over many of the finer details In between, you’ll find chapters on basic software development skills There are chapters on good software design, making the most of sample code and the Swift programming language www.it-ebooks.info Introduction xxxiii So, instead of the “traditional” order of first learning all of the basic skills and then building apps using those skills, this book starts out building apps and then explores the details of how that happened You can read the chapters in any order, skipping or returning to chapters as you need If you really want to know more about objects in an earlier chapter, jump ahead and read the chapter on objects If you’re already familiar with the Model-View-Controller design pattern, skip that chapter when you get to it Treat this book as a collection of skills to learn, not a series of lessons that have to be taken in order Here’s a preview of the chapters ahead: nn Got Tools? shows you how to download and install the Xcode development tools You’ll need those nn Boom! App walks you through the core steps for creating an iOS app—no programming needed nn Spin a Web creates an app that leverages the power of iOS’s built-in web browser nn Coming Events discusses how events (touches, gestures, and movement) get from the device into your app and how you use them to make your app respond to the user nn Table Manners shows you how data gets displayed in an app and how it gets edited nn Object Lesson dishes the straight dope on objects and object-oriented programming nn Smile! shows you how to integrate the camera and photo library into your app nn Model Citizen explains the magic incantation that software engineers call Model-View-Controller nn Sweet, Sweet Music jazzes up your mix by showing you how to add music, sounds, and iTunes to your apps nn Got Views? takes you on a brief survey of the view objects (buttons, sliders, and so on) available in the Cocoa Touch framework nn Draw Me a Picture shows you how to create your own views, unlocking the power to draw just about anything in an iOS app nn There and Back Again lays out the basics of app navigation: how your users get from one screen to another and back again nn Sharing Is Caring is all about getting your content out to the ’net, through services such as Twitter, SMS, and e-mail nn Game On! dishes out a fun game with real-time animation nn If You Build It explains some of the magic behind Interface Builder nn Apps with Attitude shakes up your apps with the accelerometer www.it-ebooks.info xxxiv Introduction nn Where Are You? draws you a map—literally nn Remember Me? shows you how user preferences are set and saved and how to share them with other iOS devices using iCloud nn Doc, You Meant Storage explains how app documents are stored, read, and updated nn See Swift, See Swift Run is a crash course on the Swift programming language nn Frame Up escapes the confines of your app to create an extension and teaches you a little about frameworks in the process www.it-ebooks.info Chapter Got Tools? If you want to build something, you are probably going to need some tools: hammer, nails, laser, crane, and one of those IKEA hex wrenches Building iOS apps requires a collection of tools called Xcode This chapter will show you how to get and install Xcode and give you a brief tour of it, so you’ll know your way around If you’ve already installed and used Xcode, check the “Requirements” section to make sure you have everything you need, but you can probably skip most of this chapter Requirements In this book, you will create apps that run on iOS version Creating an app for iOS requires Xcode version Xcode requires OS X version 10.9 (a.k.a Mavericks), which requires an Intel-based Mac Did you get all of that? Here’s your complete checklist:  Intel-based Mac  OS X 10.9 (or newer)  A few gigabytes of free disk space  An Internet connection  At least one iOS device (iPad Touch, iPhone, or iPad) running iOS 8.0 (or newer) Make sure you have an Intel-based Mac computer with OS X 10.9 (Mavericks), or newer, installed, enough disk space, and an Internet connection You can all of your initial app development right on your Mac, but at some point you’ll want to run your apps on a real iOS device (iPhone, iPod Touch, or iPad), and for that you’ll need one Note  As a general rule, newer versions are better The examples in this book were developed for iOS 8.0, built using Xcode 6.1, running on OS X 10.10 (Yosemite) By the time you read this, there will probably be a newer version of all of these, and that’s OK Read the Xcode and iOS release notes to see what has changed www.it-ebooks.info CHAPTER 1: Got Tools? Installing Xcode Apple has made installing Xcode as easy as possible On your Mac, launch the App Store application—not to be confused with the App Store for iOS, which you find in iTunes Find the Developer Tools category or just search for Xcode Figure 1-1 show the Xcode app in the Mac App Store Figure 1-1.  Xcode in the App Store Click the Install button to start downloading Xcode This will take a while (see Figure 1-2) You can monitor its progress from the Purchases tab of the App Store Be patient Xcode is huge, and even with a fast Internet connection, it will take some time to install www.it-ebooks.info Contents xiii Having Multiple Views���������������������������������������������������������������������������������������������������������������216 Consolidating Updates��������������������������������������������������������������������������������������������������������������220 Complex View Objects���������������������������������������������������������������������������������������������������������������224 Replacing UIView with ColorView����������������������������������������������������������������������������������������������224 Connecting the View to Your Data Model����������������������������������������������������������������������������������������������������������� 225 Drawing ColorView�������������������������������������������������������������������������������������������������������������������������������������������� 226 Being a Keen Observer��������������������������������������������������������������������������������������������������������������228 Key-Value Observing����������������������������������������������������������������������������������������������������������������������������������������� 228 Observing Key Value Changes��������������������������������������������������������������������������������������������������������������������������� 229 Creating KVO Dependencies������������������������������������������������������������������������������������������������������������������������������ 231 Multivector Data Model Changes����������������������������������������������������������������������������������������������233 Handling Touch Events��������������������������������������������������������������������������������������������������������������������������������������� 233 Binding the Sliders�������������������������������������������������������������������������������������������������������������������������������������������� 235 Final Touches����������������������������������������������������������������������������������������������������������������������������������������������������� 236 Cheating������������������������������������������������������������������������������������������������������������������������������������236 Summary�����������������������������������������������������������������������������������������������������������������������������������237 ■■Chapter 9: Sweet, Sweet Music�������������������������������������������������������������������������������������239 Making Your Own iPod��������������������������������������������������������������������������������������������������������������240 Design���������������������������������������������������������������������������������������������������������������������������������������������������������������� 240 Adding a Music Picker��������������������������������������������������������������������������������������������������������������������������������������� 241 Using a Music Player����������������������������������������������������������������������������������������������������������������������������������������� 244 Adding Playback Control������������������������������������������������������������������������������������������������������������������������������������ 246 Receiving Music Player Notifications���������������������������������������������������������������������������������������������������������������� 247 Adding Media Metadata������������������������������������������������������������������������������������������������������������������������������������� 249 Observing the Playing Item�������������������������������������������������������������������������������������������������������������������������������� 257 Make Some Noise���������������������������������������������������������������������������������������������������������������������259 Living in a Larger World������������������������������������������������������������������������������������������������������������������������������������� 259 Configuring Your Audio Session������������������������������������������������������������������������������������������������������������������������� 260 Playing Audio Files�������������������������������������������������������������������������������������������������������������������������������������������� 262 Creating AVAudioPlayer Objects������������������������������������������������������������������������������������������������������������������������ 264 www.it-ebooks.info xiv Contents Adding the Sound Buttons��������������������������������������������������������������������������������������������������������������������������������� 266 Making Noise����������������������������������������������������������������������������������������������������������������������������������������������������� 271 Activating Your Audio Session��������������������������������������������������������������������������������������������������������������������������� 272 Interruptions and Detours���������������������������������������������������������������������������������������������������������274 Dealing with Interruptions��������������������������������������������������������������������������������������������������������������������������������� 274 Adding Your Interruption Handlers��������������������������������������������������������������������������������������������������������������������� 274 Dealing with Audio Route Changes�������������������������������������������������������������������������������������������������������������������� 276 Other Audio Topics���������������������������������������������������������������������������������������������������������������������277 Summary�����������������������������������������������������������������������������������������������������������������������������������277 ■■Chapter 10: Got Views?�������������������������������������������������������������������������������������������������279 Learning by Example�����������������������������������������������������������������������������������������������������������������280 Buttons��������������������������������������������������������������������������������������������������������������������������������������282 The Responder and View Classes���������������������������������������������������������������������������������������������������������������������� 283 The Control Class����������������������������������������������������������������������������������������������������������������������������������������������� 284 Button Types������������������������������������������������������������������������������������������������������������������������������������������������������ 285 Control States���������������������������������������������������������������������������������������������������������������������������������������������������� 287 Button Code������������������������������������������������������������������������������������������������������������������������������������������������������� 288 Switches and Sliders�����������������������������������������������������������������������������������������������������������������289 Page Control������������������������������������������������������������������������������������������������������������������������������290 Steppers������������������������������������������������������������������������������������������������������������������������������������291 Segmented Controls������������������������������������������������������������������������������������������������������������������293 Progress Indicators�������������������������������������������������������������������������������������������������������������������294 Text Views���������������������������������������������������������������������������������������������������������������������������������295 Labels���������������������������������������������������������������������������������������������������������������������������������������������������������������� 295 Text Fields���������������������������������������������������������������������������������������������������������������������������������������������������������� 296 Text Editing Behavior����������������������������������������������������������������������������������������������������������������������������������������� 298 Text Views���������������������������������������������������������������������������������������������������������������������������������������������������������� 299 www.it-ebooks.info Contents xv Search Bars�������������������������������������������������������������������������������������������������������������������������������301 Pickers��������������������������������������������������������������������������������������������������������������������������������������302 Date Picker�������������������������������������������������������������������������������������������������������������������������������������������������������� 303 Anything Picker������������������������������������������������������������������������������������������������������������������������������������������������� 304 Image Views������������������������������������������������������������������������������������������������������������������������������306 Grouped Tables��������������������������������������������������������������������������������������������������������������������������307 The View You Never See������������������������������������������������������������������������������������������������������������308 Alert Controllers������������������������������������������������������������������������������������������������������������������������312 Summary�����������������������������������������������������������������������������������������������������������������������������������316 ■■Chapter 11: Draw Me a Picture�������������������������������������������������������������������������������������317 Creating a Custom View Class���������������������������������������������������������������������������������������������������317 View Coordinates����������������������������������������������������������������������������������������������������������������������������������������������� 318 When Views Are Drawn�������������������������������������������������������������������������������������������������������������������������������������� 321 Drawing a View�������������������������������������������������������������������������������������������������������������������������������������������������� 322 Shapely�������������������������������������������������������������������������������������������������������������������������������������325 Creating Views Programmatically���������������������������������������������������������������������������������������������������������������������� 326 Initializing Your Object��������������������������������������������������������������������������������������������������������������������������������������� 328 The drawRect(_:) Function�������������������������������������������������������������������������������������������������������������������������������� 329 More Shapes, More Colors��������������������������������������������������������������������������������������������������������������������������������� 336 Transforms��������������������������������������������������������������������������������������������������������������������������������340 Applying a Translate Transform������������������������������������������������������������������������������������������������������������������������� 342 Applying a Scale Transform������������������������������������������������������������������������������������������������������������������������������� 344 Animation: It’s Not Just for Manga��������������������������������������������������������������������������������������������346 Using Core Animation���������������������������������������������������������������������������������������������������������������������������������������� 347 Adding Animation to Shapely����������������������������������������������������������������������������������������������������������������������������� 347 OpenGL, Sprite Kit, Scene Kit, and Metal����������������������������������������������������������������������������������������������������������� 349 The Order of Things�������������������������������������������������������������������������������������������������������������������351 Drawing Images������������������������������������������������������������������������������������������������������������������������356 www.it-ebooks.info xvi Contents Advanced Graphics��������������������������������������������������������������������������������������������������������������������357 Text�������������������������������������������������������������������������������������������������������������������������������������������������������������������� 357 Shadows, Gradients, and Patterns��������������������������������������������������������������������������������������������������������������������� 358 Blend Modes������������������������������������������������������������������������������������������������������������������������������������������������������ 358 The Context Stack���������������������������������������������������������������������������������������������������������������������������������������������� 358 Summary�����������������������������������������������������������������������������������������������������������������������������������359 ■■Chapter 12: There and Back Again��������������������������������������������������������������������������������361 Measure Twice, Cut Once����������������������������������������������������������������������������������������������������������361 What Is Navigation?������������������������������������������������������������������������������������������������������������������������������������������� 362 View Controller Roles���������������������������������������������������������������������������������������������������������������������������������������� 362 Designing Wonderland��������������������������������������������������������������������������������������������������������������363 Weighing Your Navigation Options��������������������������������������������������������������������������������������������������������������������� 363 Wonderland Navigation������������������������������������������������������������������������������������������������������������������������������������� 366 Creating Wonderland�����������������������������������������������������������������������������������������������������������������366 Adding Wonderland’s Resources����������������������������������������������������������������������������������������������������������������������� 369 Configuring a Tab Bar Item�������������������������������������������������������������������������������������������������������������������������������� 371 The First Content View Controller����������������������������������������������������������������������������������������������372 Creating a Navigable Table View�����������������������������������������������������������������������������������������������374 Breathing Data Into Your Table View������������������������������������������������������������������������������������������������������������������ 376 Pushing the Detail View Controller�������������������������������������������������������������������������������������������������������������������� 381 Creating a Page View Controller������������������������������������������������������������������������������������������������384 Adding the Page View Controllers��������������������������������������������������������������������������������������������������������������������� 385 Designing a Prototype Page������������������������������������������������������������������������������������������������������������������������������ 387 Coding the One Page View Controller���������������������������������������������������������������������������������������������������������������� 389 Coding the Page View Data Source������������������������������������������������������������������������������������������������������������������� 390 Initializing a Page View Controller��������������������������������������������������������������������������������������������������������������������� 392 Presenting View Controllers������������������������������������������������������������������������������������������������������394 Presentation Controllers������������������������������������������������������������������������������������������������������������������������������������ 394 Presenting a Modal View Controller������������������������������������������������������������������������������������������������������������������� 396 www.it-ebooks.info Contents xvii Adapting View Controller Content����������������������������������������������������������������������������������������������405 Smarter Than the Average Bear������������������������������������������������������������������������������������������������������������������������� 406 Using Trait Collections��������������������������������������������������������������������������������������������������������������������������������������� 411 Adding Adaptive Constraints (and Objects)������������������������������������������������������������������������������������������������������� 412 Adapting View Programmatically����������������������������������������������������������������������������������������������������������������������� 413 Animating Layout Changes�������������������������������������������������������������������������������������������������������������������������������� 416 Advanced View Controller Topics����������������������������������������������������������������������������������������������417 Custom Presentations and Transitions�������������������������������������������������������������������������������������������������������������� 418 Appearance Proxies������������������������������������������������������������������������������������������������������������������������������������������� 418 Summary�����������������������������������������������������������������������������������������������������������������������������������419 ■■Chapter 13: Sharing Is Caring���������������������������������������������������������������������������������������421 Color My (Social) World�������������������������������������������������������������������������������������������������������������421 Having Something to Share������������������������������������������������������������������������������������������������������������������������������� 423 Presenting the Activity View Controller�������������������������������������������������������������������������������������������������������������� 423 The More You Share, the More You Get�������������������������������������������������������������������������������������425 Refactoring drawRect(_:)����������������������������������������������������������������������������������������������������������������������������������� 425 Providing More Items to Share�������������������������������������������������������������������������������������������������������������������������� 427 Excluding Activities�������������������������������������������������������������������������������������������������������������������������������������������� 428 The Curse of the Lowest Common Denominator�����������������������������������������������������������������������429 Providing Activity-Specific Data������������������������������������������������������������������������������������������������������������������������ 430 Promises, Promises������������������������������������������������������������������������������������������������������������������������������������������� 431 Big Data������������������������������������������������������������������������������������������������������������������������������������������������������������� 432 Inventing Your Own Activities����������������������������������������������������������������������������������������������������433 Sharing with Specific Services�������������������������������������������������������������������������������������������������433 Other Social Network Interactions��������������������������������������������������������������������������������������������434 Summary�����������������������������������������������������������������������������������������������������������������������������������435 www.it-ebooks.info xviii Contents ■■Chapter 14: Game On!���������������������������������������������������������������������������������������������������437 SpaceSalad�������������������������������������������������������������������������������������������������������������������������������438 A Brief Tour of SpriteKit������������������������������������������������������������������������������������������������������������������������������������� 440 Making the Scene���������������������������������������������������������������������������������������������������������������������������������������������� 442 Adding Sprites���������������������������������������������������������������������������������������������������������������������������445 Resizing a Scene����������������������������������������������������������������������������������������������������������������������������������������������� 447 Let’s Get Physical����������������������������������������������������������������������������������������������������������������������451 Setting Boundaries�������������������������������������������������������������������������������������������������������������������������������������������� 453 Adding Obstacles����������������������������������������������������������������������������������������������������������������������������������������������� 456 Interacting with the Player��������������������������������������������������������������������������������������������������������459 Responding to Touch����������������������������������������������������������������������������������������������������������������������������������������� 461 Sprites on a Plane���������������������������������������������������������������������������������������������������������������������465 Nodes That Go Bump�����������������������������������������������������������������������������������������������������������������467 Contact!�������������������������������������������������������������������������������������������������������������������������������������468 Gamifying SpaceSalad��������������������������������������������������������������������������������������������������������������469 Actions in Action������������������������������������������������������������������������������������������������������������������������������������������������ 471 Traditional Animation����������������������������������������������������������������������������������������������������������������������������������������� 475 Presenting Scenes��������������������������������������������������������������������������������������������������������������������476 Beyond SpaceSalad������������������������������������������������������������������������������������������������������������������480 Summary�����������������������������������������������������������������������������������������������������������������������������������481 ■■Chapter 15: If You Build It ���������������������������������������������������������������������������������������483 How Interface Builder Files Work����������������������������������������������������������������������������������������������484 Compiling Interface Builder Files����������������������������������������������������������������������������������������������������������������������� 484 Loading a Scene������������������������������������������������������������������������������������������������������������������������������������������������ 484 Loading an xib File�������������������������������������������������������������������������������������������������������������������������������������������� 486 Placeholder Objects and the File’s Owner��������������������������������������������������������������������������������������������������������� 486 Creating Objects������������������������������������������������������������������������������������������������������������������������487 Editing Attributes����������������������������������������������������������������������������������������������������������������������������������������������� 488 www.it-ebooks.info Contents xix Creating Custom Objects�����������������������������������������������������������������������������������������������������������489 Connections�������������������������������������������������������������������������������������������������������������������������������491 Making Action Connections������������������������������������������������������������������������������������������������������������������������������� 492 Sending Action Messages��������������������������������������������������������������������������������������������������������������������������������� 493 Taking Control of Interface Builder Files�����������������������������������������������������������������������������������494 Declaring Placeholders�������������������������������������������������������������������������������������������������������������������������������������� 494 Designing ShapeView���������������������������������������������������������������������������������������������������������������������������������������� 497 Connecting the Gesture Recognizers����������������������������������������������������������������������������������������������������������������� 498 Building Your Shape Factory������������������������������������������������������������������������������������������������������������������������������ 500 Loading an Interface Builder File����������������������������������������������������������������������������������������������������������������������� 501 Replacing Code�������������������������������������������������������������������������������������������������������������������������������������������������� 502 Summary�����������������������������������������������������������������������������������������������������������������������������������504 ■■Chapter 16: Apps with Attitude�������������������������������������������������������������������������������������505 Leveler���������������������������������������������������������������������������������������������������������������������������������������506 Creating Leveler������������������������������������������������������������������������������������������������������������������������������������������������ 506 Getting Motion Data������������������������������������������������������������������������������������������������������������������513 Creating CMMotionManager������������������������������������������������������������������������������������������������������������������������������ 515 Starting and Stopping Updates�������������������������������������������������������������������������������������������������������������������������� 515 Push Me, Pull You����������������������������������������������������������������������������������������������������������������������������������������������� 516 Timing Is Everything������������������������������������������������������������������������������������������������������������������������������������������ 516 Herky-Jerky������������������������������������������������������������������������������������������������������������������������������������������������������� 519 Getting Other Kinds of Motion Data�������������������������������������������������������������������������������������������524 Gyroscope Data������������������������������������������������������������������������������������������������������������������������������������������������� 524 Magnetometer Data������������������������������������������������������������������������������������������������������������������������������������������� 525 Device Motion and Attitude�������������������������������������������������������������������������������������������������������������������������������� 526 Measuring Change��������������������������������������������������������������������������������������������������������������������������������������������� 528 Summary�����������������������������������������������������������������������������������������������������������������������������������528 www.it-ebooks.info xx Contents ■■Chapter 17: Where Are You?������������������������������������������������������������������������������������������529 Creating Pigeon�������������������������������������������������������������������������������������������������������������������������529 Collecting Location Data�����������������������������������������������������������������������������������������������������������532 Asking for Permission���������������������������������������������������������������������������������������������������������������������������������������� 534 Starting Location Collection������������������������������������������������������������������������������������������������������������������������������� 536 Using a Map View����������������������������������������������������������������������������������������������������������������������536 Decorating Your Map�����������������������������������������������������������������������������������������������������������������539 Adding an Annotation���������������������������������������������������������������������������������������������������������������������������������������� 539 Map Coordinates������������������������������������������������������������������������������������������������������������������������������������������������ 542 Adding a Little Bounce��������������������������������������������������������������������������������������������������������������������������������������� 542 Showing the Way Home������������������������������������������������������������������������������������������������������������544 Adding Overlays������������������������������������������������������������������������������������������������������������������������������������������������� 544 Providing the Renderer�������������������������������������������������������������������������������������������������������������������������������������� 546 Location Monitoring������������������������������������������������������������������������������������������������������������������547 Approximate Location and Non-GPS Devices���������������������������������������������������������������������������������������������������� 548 Monitoring Regions������������������������������������������������������������������������������������������������������������������������������������������� 549 Reducing Location Change Messages��������������������������������������������������������������������������������������������������������������� 549 Movement and Heading������������������������������������������������������������������������������������������������������������������������������������� 549 Geocoding���������������������������������������������������������������������������������������������������������������������������������550 Getting Directions����������������������������������������������������������������������������������������������������������������������550 Summary�����������������������������������������������������������������������������������������������������������������������������������551 ■■Chapter 18: Remember Me?������������������������������������������������������������������������������������������553 Property Lists����������������������������������������������������������������������������������������������������������������������������554 Serializing Property Lists����������������������������������������������������������������������������������������������������������554 User Defaults�����������������������������������������������������������������������������������������������������������������������������555 Making Pigeon Remember��������������������������������������������������������������������������������������������������������555 Minimizing Updates and Code��������������������������������������������������������������������������������������������������������������������������� 556 Defining Your Keys��������������������������������������������������������������������������������������������������������������������������������������������� 557 Writing Values to User Defaults������������������������������������������������������������������������������������������������������������������������� 557 www.it-ebooks.info Contents xxi Getting Values from User Defaults��������������������������������������������������������������������������������������������������������������������� 558 Testing User Defaults����������������������������������������������������������������������������������������������������������������������������������������� 559 Registering Default Values��������������������������������������������������������������������������������������������������������560 Turning Objects into Property List Objects��������������������������������������������������������������������������������561 Preserving and Restoring savedLocation���������������������������������������������������������������������������������������������������������� 563 Persistent Views������������������������������������������������������������������������������������������������������������������������565 Fading Into the Background������������������������������������������������������������������������������������������������������������������������������ 566 Preserving View Controllers������������������������������������������������������������������������������������������������������������������������������ 567 Assigning Restoration Identifiers����������������������������������������������������������������������������������������������������������������������� 568 Customizing Restoration������������������������������������������������������������������������������������������������������������������������������������ 570 Deeper Restoration�������������������������������������������������������������������������������������������������������������������������������������������� 571 Pigeons in the Cloud������������������������������������������������������������������������������������������������������������������571 Storing Values in the Cloud�������������������������������������������������������������������������������������������������������������������������������� 572 Cloud Watching�������������������������������������������������������������������������������������������������������������������������������������������������� 573 Enabling iCloud�������������������������������������������������������������������������������������������������������������������������������������������������� 575 Testing the Cloud����������������������������������������������������������������������������������������������������������������������������������������������� 576 Bundle Up Your Settings������������������������������������������������������������������������������������������������������������577 Creating a Settings Bundle�������������������������������������������������������������������������������������������������������������������������������� 578 Using Your Settings Bundle Values�������������������������������������������������������������������������������������������������������������������� 581 Testing Your Settings Bundle����������������������������������������������������������������������������������������������������������������������������� 581 Summary�����������������������������������������������������������������������������������������������������������������������������������582 ■■Chapter 19: Doc, You Meant Storage�����������������������������������������������������������������������������585 Document Overview������������������������������������������������������������������������������������������������������������������585 Where, Oh Where, Do My Documents Go?���������������������������������������������������������������������������������586 MyStuff on Documents��������������������������������������������������������������������������������������������������������������587 Supplying Your Document’s Data����������������������������������������������������������������������������������������������589 Wrapping Up Your Data�������������������������������������������������������������������������������������������������������������������������������������� 590 Using Wrappers������������������������������������������������������������������������������������������������������������������������������������������������� 591 Incremental Document Updates������������������������������������������������������������������������������������������������������������������������ 591 Constructing Your Wrappers������������������������������������������������������������������������������������������������������������������������������ 592 Interpreting Your Wrappers�������������������������������������������������������������������������������������������������������������������������������� 593 www.it-ebooks.info xxii Contents Archiving Objects����������������������������������������������������������������������������������������������������������������������594 Adopting NSCoding�������������������������������������������������������������������������������������������������������������������������������������������� 595 Archiving and Unarchiving Objects�������������������������������������������������������������������������������������������������������������������� 596 The Archiving Serialization Smackdown����������������������������������������������������������������������������������������������������������� 597 Serialization, Meet Archiving����������������������������������������������������������������������������������������������������������������������������� 597 Document, Meet Your Data Model���������������������������������������������������������������������������������������������598 Tracking Changes����������������������������������������������������������������������������������������������������������������������601 Testing Your Document��������������������������������������������������������������������������������������������������������������603 Setting Breakpoints������������������������������������������������������������������������������������������������������������������������������������������� 603 Stepping Through Code and Examining Variables��������������������������������������������������������������������������������������������� 604 Creating a ThingsDocument Delegate��������������������������������������������������������������������������������������������������������������� 607 Storing Image Files�������������������������������������������������������������������������������������������������������������������608 Odds and Ends��������������������������������������������������������������������������������������������������������������������������615 iCloud Storage��������������������������������������������������������������������������������������������������������������������������������������������������� 615 Archive Versioning��������������������������������������������������������������������������������������������������������������������������������������������� 616 Summary�����������������������������������������������������������������������������������������������������������������������������������617 ■■Chapter 20: See Swift, See Swift Run����������������������������������������������������������������������������619 The (Short) History of Swift�������������������������������������������������������������������������������������������������������620 Classes��������������������������������������������������������������������������������������������������������������������������������������622 Playgrounds������������������������������������������������������������������������������������������������������������������������������������������������������� 623 Properties���������������������������������������������������������������������������������������������������������������������������������������������������������� 624 Methods������������������������������������������������������������������������������������������������������������������������������������������������������������� 626 Inheritance��������������������������������������������������������������������������������������������������������������������������������������������������������� 629 Object Creation�������������������������������������������������������������������������������������������������������������������������������������������������� 631 Closures������������������������������������������������������������������������������������������������������������������������������������637 Capturing self���������������������������������������������������������������������������������������������������������������������������������������������������� 639 Using Closures��������������������������������������������������������������������������������������������������������������������������������������������������� 640 Closure Shortcuts���������������������������������������������������������������������������������������������������������������������������������������������� 640 Protocols�����������������������������������������������������������������������������������������������������������������������������������643 Extensions���������������������������������������������������������������������������������������������������������������������������������645 www.it-ebooks.info Contents xxiii Structures����������������������������������������������������������������������������������������������������������������������������������646 Tuples����������������������������������������������������������������������������������������������������������������������������������������648 Enumerations����������������������������������������������������������������������������������������������������������������������������649 Raw Values�������������������������������������������������������������������������������������������������������������������������������������������������������� 650 Associated Values���������������������������������������������������������������������������������������������������������������������������������������������� 651 Extended Enumerations������������������������������������������������������������������������������������������������������������������������������������� 653 Numeric Values�������������������������������������������������������������������������������������������������������������������������654 Implicit Type Conversion������������������������������������������������������������������������������������������������������������������������������������ 654 Numeric Literals������������������������������������������������������������������������������������������������������������������������������������������������ 655 Numbers Are Types Too������������������������������������������������������������������������������������������������������������������������������������� 656 Overflow and Underflow������������������������������������������������������������������������������������������������������������������������������������ 656 Strings and Characters��������������������������������������������������������������������������������������������������������������657 String and Character Literals����������������������������������������������������������������������������������������������������������������������������� 657 Unicode�������������������������������������������������������������������������������������������������������������������������������������������������������������� 658 String Interpolation�������������������������������������������������������������������������������������������������������������������������������������������� 659 Strings Are NSStrings���������������������������������������������������������������������������������������������������������������������������������������� 659 String Concatenation����������������������������������������������������������������������������������������������������������������������������������������� 660 Attributed Strings���������������������������������������������������������������������������������������������������������������������������������������������� 660 Collections���������������������������������������������������������������������������������������������������������������������������������663 Control Statements�������������������������������������������������������������������������������������������������������������������665 for Loop������������������������������������������������������������������������������������������������������������������������������������������������������������� 666 switch Statement���������������������������������������������������������������������������������������������������������������������������������������������� 667 Optionals�����������������������������������������������������������������������������������������������������������������������������������671 Using Optionals�������������������������������������������������������������������������������������������������������������������������������������������������� 672 Optionals Carry�������������������������������������������������������������������������������������������������������������������������������������������������� 673 Optional Chaining���������������������������������������������������������������������������������������������������������������������������������������������� 673 Failable Initializers��������������������������������������������������������������������������������������������������������������������������������������������� 675 Implicit Unwrapping������������������������������������������������������������������������������������������������������������������������������������������ 676 Type Casting������������������������������������������������������������������������������������������������������������������������������677 Downcasting������������������������������������������������������������������������������������������������������������������������������������������������������ 678 Downcasting Collections����������������������������������������������������������������������������������������������������������������������������������� 679 www.it-ebooks.info xxiv Contents Value vs Reference vs Constant vs Variable���������������������������������������������������������������������������680 let and var References��������������������������������������������������������������������������������������������������������������������������������������� 681 var Parameters�������������������������������������������������������������������������������������������������������������������������������������������������� 681 References to Values (inout)������������������������������������������������������������������������������������������������������������������������������ 682 Working with C and Objective-C������������������������������������������������������������������������������������������������682 The Toll-Free Bridge������������������������������������������������������������������������������������������������������������������������������������������ 683 Method Signatures�������������������������������������������������������������������������������������������������������������������������������������������� 684 Memory Management���������������������������������������������������������������������������������������������������������������685 Garbage Collection�������������������������������������������������������������������������������������������������������������������������������������������� 685 Reference Counting������������������������������������������������������������������������������������������������������������������������������������������� 687 Circular Retains������������������������������������������������������������������������������������������������������������������������������������������������� 689 Even More Swift������������������������������������������������������������������������������������������������������������������������691 Summary�����������������������������������������������������������������������������������������������������������������������������������692 ■■Chapter 21: Frame Up����������������������������������������������������������������������������������������������������693 Extensions���������������������������������������������������������������������������������������������������������������������������������694 Creating an Extension���������������������������������������������������������������������������������������������������������������������������������������� 695 Connecting Your Extension�������������������������������������������������������������������������������������������������������������������������������� 697 Running your Action������������������������������������������������������������������������������������������������������������������������������������������ 699 Lights, Code, Action�������������������������������������������������������������������������������������������������������������������701 Every Extension Is an Island������������������������������������������������������������������������������������������������������������������������������ 701 Obtaining the User’s Data���������������������������������������������������������������������������������������������������������������������������������� 704 Reusing Squeezer���������������������������������������������������������������������������������������������������������������������������������������������� 706 Action Icons�������������������������������������������������������������������������������������������������������������������������������708 Reusing Squeezer, for Real�������������������������������������������������������������������������������������������������������710 Creating a Framework��������������������������������������������������������������������������������������������������������������������������������������� 711 Adding Code to a Framework���������������������������������������������������������������������������������������������������������������������������� 712 Sharing a Framework with an Extension����������������������������������������������������������������������������������������������������������� 714 Summary�����������������������������������������������������������������������������������������������������������������������������������714 Index���������������������������������������������������������������������������������������������������������������������������������715 www.it-ebooks.info About the Author James Bucanek has spent the past 30 years programming and developing microprocessor systems He has experience with a broad range of computer hardware and software, from embedded consumer products to industrial robotics His development projects include the first local area network for the Apple II, distributed air-conditioning control systems, a piano teaching system, digital oscilloscopes, silicon wafer deposition furnaces, and collaborative writing tools for K-12 education James holds a Java Developer Certification from Sun Microsystems and was awarded a patent for optimizing local area networks James is currently focused on OS X and iOS software development, where he can combine his deep knowledge of UNIX and object-oriented languages with his passion for elegant design James holds an associate’s degree in classical ballet from the Royal Academy of Dance and can occasionally be found teaching at Adams Ballet Academy xxv www.it-ebooks.info About the Technical Reviewer Charles Cruz is a mobile application developer for the iOS, Windows Phone, and Android platforms He graduated from Stanford University with bachelor’s and master’s degrees in engineering He lives in Southern California and runs a photography business with his wife (www.bellalentestudios.com) When not doing technical things, he plays lead guitar in an original metal band (www.taintedsociety.com) Charles can be reached at codingandpicking@gmail.com and @CodingNPicking on Twitter xxvii www.it-ebooks.info Acknowledgments This edition of Learn iOS App Development is largely a continuation of the previous edition I would be remiss not to acknowledge the giants who created Learn iOS App Development, on whose shoulders we now stand Charles Cruz, my technical editor, checked every line of code and symbol to ensure complete accuracy This was made all the more challenging because the Swift language was literally changing from one day to the next Any technical errors are ultimately my responsibility, but there are significantly fewer thanks to Charles Kim Wimpsett dotted my i’s, crossed my t’s, and corrected my (egregious) spelling If you find this book easy to read, you have Kim’s blue pencil to thank The entire project was stewarded by my coordinating editor, Kevin Walter, who juggled schedules, liaised between editors, tracked production, and herded everyone toward a common goal Assisting us was a phalanx of production specialists, artists, and managing editors To all the folks at Apress, thank you, thank you, thank you! Finally, I want to shout a “thank-you” to Apple’s Xcode development team for creating the most advanced mobile app development tool in the world xxix www.it-ebooks.info ... an iOS developer if you want to any of the following:  Sell, or give away, your apps through Apple’s App Store  Gain access to Apple’s Developer Forums and support resources  Give your apps... (outside of the App Store)  Develop apps that use Game Kit, in -app purchases, push notifications, or other technologies that depend on Apple-operated services  Test your apps on a real iOS device... and run your apps in Xcode’s simulator If you never plan to distribute your apps through the App Store or run your app on an iOS device, you may never need to become an official iOS developer

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

Từ khóa liên quan

Mục lục

  • Learn iOS 8 App Development

    • Contents at a Glance

    • Contents

    • About the Author

    • About the Technical Reviewer

    • Acknowledgments

    • Introduction

    • Chapter 1: Got Tools?

      • Requirements

      • Installing Xcode

      • What Is Xcode?

      • Becoming an iOS Developer

      • Getting the Projects

      • Launching Xcode the First Time

      • Welcome to Xcode

        • Navigation Area

        • Editor Area

        • Utility Area

        • Debug Area

        • Toolbar

        • Running Your First App

        • Summary

        • Chapter 2: Boom! App

          • Design

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

Tài liệu liên quan