Learn iOS 7 app development

772 101 0
Learn iOS 7 app development

Đ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 everything you need to know to program iPhone and iPad Apps Learn iOS App Development 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�����������������������������������������������������������������������������������������������������57 ■■Chapter 4: Coming Events�����������������������������������������������������������������������������������������������97 ■■Chapter 5: Table Manners����������������������������������������������������������������������������������������������137 ■■Chapter 6: Object Lesson�����������������������������������������������������������������������������������������������177 ■■Chapter 7: Smile!�����������������������������������������������������������������������������������������������������������189 ■■Chapter 8: Model Citizen�����������������������������������������������������������������������������������������������217 ■■Chapter 9: Sweet, Sweet Music�������������������������������������������������������������������������������������255 ■■Chapter 10: Got Views?�������������������������������������������������������������������������������������������������291 ■■Chapter 11: Draw Me a Picture�������������������������������������������������������������������������������������321 ■■Chapter 12: There and Back Again��������������������������������������������������������������������������������367 ■■Chapter 13: Networking, the Social Kind�����������������������������������������������������������������������411 v www.it-ebooks.info vi Contents at a Glance ■■Chapter 14: Networking, The Nerdy Kind����������������������������������������������������������������������429 ■■Chapter 15: If You Build It ������������������������������������������������������������������������������������������485 ■■Chapter 16: Apps with Attitude�������������������������������������������������������������������������������������507 ■■Chapter 17: Where Are You?������������������������������������������������������������������������������������������529 ■■Chapter 18: Remember Me?������������������������������������������������������������������������������������������551 ■■Chapter 19: Doc, You Meant Storage�����������������������������������������������������������������������������581 ■■Chapter 20: Being Objective������������������������������������������������������������������������������������������617 ■■Chapter 21: The Elephant in the Room��������������������������������������������������������������������������653 ■■Chapter 22: Êtes-vous Polyglotte?��������������������������������������������������������������������������������671 ■■Chapter 23: Faster, Faster���������������������������������������������������������������������������������������������691 ■■Chapter 24: Twice As Nice���������������������������������������������������������������������������������������������713 Index���������������������������������������������������������������������������������������������������������������������������������735 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 & 5th Street station in 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 create an interactive virtual reality of the gathering, 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 literally 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 Objective‑C, that’s perfect If you know C, Java, C#, or C++ you shouldn’t have too much trouble following along, and there’s an Objective‑C primer chapter that you’ll want to read If you are completely new to programming computers, I suggest getting a basic xxxi www.it-ebooks.info xxxii Introduction Objective‑C programming book—say, Objective-C for Absolute Beginners, by Gary Bennett, Mitchell Fisher, and Brad Lees—and read that first, or in parallel All iOS app development is done using the Objective‑C language 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 head start 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, communicating in real-time with other devices, 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! Old School vs Too Cool for School I’m an Old School programmer I learned programming from the bit up (literally) The first program I wrote was on a 4-bit micro-controller 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 I knew C backwards and forwards before I dipped my toe into C++, and I was an expert in C++ before I wrote my first graphical user interface (GUI) application for the (revolutionary) Macintosh computer While I value this accumulated knowledge, and much of it is still useful, I realize that a “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 skilled you are in programming, 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, memory management, and the Objective C 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’ve already learned about Objective‑C memory management, 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 will walk you through the core steps in 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, 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 will jazz up your mix by showing you how to add music and iTunes to your apps nn Got Tools? takes you on a brief survey of the tools (views, controls, and objects) available in the Cocoa Touch framework So when you need a tool, you’ll know how to find it nn Draw Me a Picture will show you how to create custom 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 Networking, the Social Kind will get your app plugged into Facebook, Twitter, email, and more nn Networking, the Nerdy Kind demonstrates real-time communications between multiple iOS devices using Game Kit nn If You Build It explains some of the magic behind Interface Builder www.it-ebooks.info xxxiv Introduction nn Apps With Attitude shakes up your apps with the accelerometer and compass nn Where Are You? will draw 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, exchanged, and synchronized nn Being Objective is a crash course in the Objective C programming language nn The Elephant in the Room explores the technology, techniques, and traps of memory management nn Êtes-vous polyglotte? will show you how to create apps that speak multiple languages nn Faster, Faster! will explain the basics of performance analysis and optimization, so your apps will run like bunnies nn Twice as Nice will up your game with multi-tasking, showing your how your app can two, or three, or four things at the same time 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.8 (a.k.a Mountain Lion), which requires an Intel-based Mac Did you get all of that? Here’s your complete checklist:  Intel-based Mac  OS X 10.8 (or later)  A few gigabytes of free disk space  An Internet connection  At least one iOS device (iPad Touch, iPhone, or iPad) running iOS 7.0 (or later) Make sure you have an Intel-based Mac computer with OS X 10.8 (Mountain Lion), or later, 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 www.it-ebooks.info CHAPTER 1: Got Tools? Note  As a general rule, later versions are better The examples in this book were developed for iOS 7.0, built using Xcode 5.0, running on OS X 10.8.5 (Mountain Lion) By the time you read this there will probably be a newer version of all of these, and that’s OK Installing Xcode Apple has made installing Xcode as easy as possible On your Mac, launch the App Store application and search for Xcode, as shown in Figure 1-1 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 download www.it-ebooks.info Contents xiii Being a K-V Observer����������������������������������������������������������������������������������������������������������������244 Key Value Observing������������������������������������������������������������������������������������������������������������������������������������������ 244 Observing Key Value Changes��������������������������������������������������������������������������������������������������������������������������� 244 Creating KVO Dependencies������������������������������������������������������������������������������������������������������������������������������ 246 Multi-Vector Data Model Changes���������������������������������������������������������������������������������������������248 Handling Touch Events��������������������������������������������������������������������������������������������������������������������������������������� 248 Binding The Sliders�������������������������������������������������������������������������������������������������������������������������������������������� 250 Final Touches����������������������������������������������������������������������������������������������������������������������������������������������������� 251 Cheating������������������������������������������������������������������������������������������������������������������������������������251 Summary�����������������������������������������������������������������������������������������������������������������������������������252 ■■Chapter 9: Sweet, Sweet Music�������������������������������������������������������������������������������������255 Making Your Own iPod��������������������������������������������������������������������������������������������������������������256 Design���������������������������������������������������������������������������������������������������������������������������������������������������������������� 256 Adding a Music Picker��������������������������������������������������������������������������������������������������������������������������������������� 257 Using a Music Player����������������������������������������������������������������������������������������������������������������������������������������� 260 Adding Playback Control������������������������������������������������������������������������������������������������������������������������������������ 262 Receiving Music Player Notifications���������������������������������������������������������������������������������������������������������������� 264 Adding Media Metadata������������������������������������������������������������������������������������������������������������������������������������� 266 Observing the Playing Item�������������������������������������������������������������������������������������������������������������������������������� 269 Make Some Noise���������������������������������������������������������������������������������������������������������������������271 Living in a Larger World������������������������������������������������������������������������������������������������������������������������������������� 272 Configuring Your Audio Session������������������������������������������������������������������������������������������������������������������������� 273 Playing Audio Files�������������������������������������������������������������������������������������������������������������������������������������������� 275 Creating AVAudioPlayer objects������������������������������������������������������������������������������������������������������������������������� 277 Adding the Sound Buttons��������������������������������������������������������������������������������������������������������������������������������� 280 Activating Your Audio Session��������������������������������������������������������������������������������������������������������������������������� 283 Interruptions and Detours���������������������������������������������������������������������������������������������������������284 Dealing with Interruptions��������������������������������������������������������������������������������������������������������������������������������� 285 Adding Your Interruption Handlers��������������������������������������������������������������������������������������������������������������������� 285 Dealing with Audio Route Changes�������������������������������������������������������������������������������������������������������������������� 287 Other Audio Topics���������������������������������������������������������������������������������������������������������������������288 Summary�����������������������������������������������������������������������������������������������������������������������������������289 www.it-ebooks.info xiv Contents ■■Chapter 10: Got Views?�������������������������������������������������������������������������������������������������291 Learning by Example�����������������������������������������������������������������������������������������������������������������291 Buttons��������������������������������������������������������������������������������������������������������������������������������������294 The Responder and View Classes���������������������������������������������������������������������������������������������������������������������� 295 The Control Class����������������������������������������������������������������������������������������������������������������������������������������������� 296 Button Types������������������������������������������������������������������������������������������������������������������������������������������������������ 296 Control States���������������������������������������������������������������������������������������������������������������������������������������������������� 299 Button Code������������������������������������������������������������������������������������������������������������������������������������������������������� 300 Switches and Sliders�����������������������������������������������������������������������������������������������������������������300 Page Control������������������������������������������������������������������������������������������������������������������������������302 Steppers������������������������������������������������������������������������������������������������������������������������������������303 Segmented Controls������������������������������������������������������������������������������������������������������������������304 Progress Indicators�������������������������������������������������������������������������������������������������������������������305 Text Views���������������������������������������������������������������������������������������������������������������������������������307 Labels���������������������������������������������������������������������������������������������������������������������������������������������������������������� 307 Text Fields���������������������������������������������������������������������������������������������������������������������������������������������������������� 308 Text Editing Behavior����������������������������������������������������������������������������������������������������������������������������������������� 310 Text Views���������������������������������������������������������������������������������������������������������������������������������������������������������� 311 Pickers��������������������������������������������������������������������������������������������������������������������������������������312 Date Picker�������������������������������������������������������������������������������������������������������������������������������������������������������� 313 Anything Picker������������������������������������������������������������������������������������������������������������������������������������������������� 314 Image Views������������������������������������������������������������������������������������������������������������������������������315 Grouped Tables��������������������������������������������������������������������������������������������������������������������������316 The View You Never See������������������������������������������������������������������������������������������������������������317 Summary�����������������������������������������������������������������������������������������������������������������������������������320 ■■Chapter 11: Draw Me a Picture�������������������������������������������������������������������������������������321 Creating a Custom View Class���������������������������������������������������������������������������������������������������321 View Coordinates����������������������������������������������������������������������������������������������������������������������������������������������� 322 When Views Are Drawn�������������������������������������������������������������������������������������������������������������������������������������� 325 Drawing a View�������������������������������������������������������������������������������������������������������������������������������������������������� 326 www.it-ebooks.info Contents xv Shapely�������������������������������������������������������������������������������������������������������������������������������������329 Creating Views Programmatically���������������������������������������������������������������������������������������������������������������������� 330 The -drawRect: Method������������������������������������������������������������������������������������������������������������������������������������� 332 More Shapes, More Colors��������������������������������������������������������������������������������������������������������������������������������� 340 Transforms��������������������������������������������������������������������������������������������������������������������������������345 Applying a Translate Transform������������������������������������������������������������������������������������������������������������������������� 346 Applying a Scale Transform������������������������������������������������������������������������������������������������������������������������������� 349 Animation: It’s Not Just for Manga��������������������������������������������������������������������������������������������351 Using Core Animation���������������������������������������������������������������������������������������������������������������������������������������� 352 Adding Animation to Shapely����������������������������������������������������������������������������������������������������������������������������� 352 OpenGL�������������������������������������������������������������������������������������������������������������������������������������������������������������� 354 The Order of Things�������������������������������������������������������������������������������������������������������������������355 Images and Bitmaps������������������������������������������������������������������������������������������������������������������359 Creating Images from Bitmaps�������������������������������������������������������������������������������������������������������������������������� 360 Creating Bitmaps From Drawings���������������������������������������������������������������������������������������������������������������������� 362 Advanced Graphics��������������������������������������������������������������������������������������������������������������������363 Text�������������������������������������������������������������������������������������������������������������������������������������������������������������������� 363 Shadows, Gradients, and Patterns��������������������������������������������������������������������������������������������������������������������� 364 Blend Modes������������������������������������������������������������������������������������������������������������������������������������������������������ 364 The Context Stack���������������������������������������������������������������������������������������������������������������������������������������������� 364 Summary�����������������������������������������������������������������������������������������������������������������������������������365 ■■Chapter 12: There and Back Again��������������������������������������������������������������������������������367 Measure Twice, Cut Once����������������������������������������������������������������������������������������������������������367 What is Navigation?������������������������������������������������������������������������������������������������������������������������������������������� 368 View Controller Roles���������������������������������������������������������������������������������������������������������������������������������������� 368 Designing Wonderland��������������������������������������������������������������������������������������������������������������369 Weighing Your Navigation Options��������������������������������������������������������������������������������������������������������������������� 369 Wonderland Navigation������������������������������������������������������������������������������������������������������������������������������������� 371 Creating Wonderland�����������������������������������������������������������������������������������������������������������������372 Adding Wonderland’s Resources����������������������������������������������������������������������������������������������������������������������� 374 Configuring a Tab Bar Item�������������������������������������������������������������������������������������������������������������������������������� 375 www.it-ebooks.info xvi Contents The First Content View Controller����������������������������������������������������������������������������������������������376 Presenting a Modal View Controller������������������������������������������������������������������������������������������������������������������� 378 Dismissing a View Controller����������������������������������������������������������������������������������������������������������������������������� 381 Creating a Navigable Table View�����������������������������������������������������������������������������������������������383 Breathing Data Into Your Table View������������������������������������������������������������������������������������������������������������������ 386 Pushing the Detail View Controller�������������������������������������������������������������������������������������������������������������������� 393 Creating a Page View Controller������������������������������������������������������������������������������������������������395 Adding the Page View Controllers��������������������������������������������������������������������������������������������������������������������� 396 Designing a Prototype Page������������������������������������������������������������������������������������������������������������������������������ 397 Coding the One Page View��������������������������������������������������������������������������������������������������������������������������������� 400 The Paginator���������������������������������������������������������������������������������������������������������������������������������������������������� 402 Coding the Page View Data Source������������������������������������������������������������������������������������������������������������������� 405 Initializing a Page View Controller��������������������������������������������������������������������������������������������������������������������� 407 Using Pop-Over Controllers�������������������������������������������������������������������������������������������������������409 Advanced Navigation�����������������������������������������������������������������������������������������������������������������409 Summary�����������������������������������������������������������������������������������������������������������������������������������410 ■■Chapter 13: Networking, the Social Kind�����������������������������������������������������������������������411 Color My (Social) World�������������������������������������������������������������������������������������������������������������411 Having Something to Share������������������������������������������������������������������������������������������������������������������������������� 412 Presenting the Activity View Controller�������������������������������������������������������������������������������������������������������������� 413 Sharing More�����������������������������������������������������������������������������������������������������������������������������416 Extracting Code������������������������������������������������������������������������������������������������������������������������������������������������� 416 Providing More Items to Share�������������������������������������������������������������������������������������������������������������������������� 419 Excluding Activities�������������������������������������������������������������������������������������������������������������������������������������������� 420 The Curse of the Lowest Common Denominator�����������������������������������������������������������������������421 Providing Activity Specific Data������������������������������������������������������������������������������������������������������������������������� 422 Promises, Promises������������������������������������������������������������������������������������������������������������������������������������������� 424 Big Data������������������������������������������������������������������������������������������������������������������������������������������������������������� 425 Sharing with Specific Services�������������������������������������������������������������������������������������������������425 Other Social Network Interactions��������������������������������������������������������������������������������������������426 Summary�����������������������������������������������������������������������������������������������������������������������������������427 www.it-ebooks.info Contents xvii ■■Chapter 14: Networking, The Nerdy Kind����������������������������������������������������������������������429 SunTouch�����������������������������������������������������������������������������������������������������������������������������������430 Creating SunTouch��������������������������������������������������������������������������������������������������������������������������������������������� 431 Designing the Initial Screens����������������������������������������������������������������������������������������������������������������������������� 431 Creating the Single Player Version��������������������������������������������������������������������������������������������434 Loading STGameViewController������������������������������������������������������������������������������������������������������������������������ 435 How SunTouch Works���������������������������������������������������������������������������������������������������������������������������������������� 436 Customizing Core Animations���������������������������������������������������������������������������������������������������������������������������� 437 Playing the Game����������������������������������������������������������������������������������������������������������������������������������������������� 438 Plugging into Game Center��������������������������������������������������������������������������������������������������������440 Configuring a Game Center–aware app������������������������������������������������������������������������������������������������������������� 440 Enabling Game Center��������������������������������������������������������������������������������������������������������������������������������������� 442 Creating an App in the iTunes Store������������������������������������������������������������������������������������������������������������������ 444 Configuring Game Center���������������������������������������������������������������������������������������������������������������������������������� 446 Adding GameKit to Your App������������������������������������������������������������������������������������������������������������������������������ 448 Obtaining the Local Player��������������������������������������������������������������������������������������������������������������������������������� 449 Adding a Game Center Button��������������������������������������������������������������������������������������������������������������������������� 450 Recording Leaderboard Scores������������������������������������������������������������������������������������������������������������������������� 452 Creating a Test Player���������������������������������������������������������������������������������������������������������������������������������������� 453 Peer-To-Peer Networking����������������������������������������������������������������������������������������������������������454 Turning SunTouch Into a Two-Player Game������������������������������������������������������������������������������������������������������� 455 Matchmaking����������������������������������������������������������������������������������������������������������������������������������������������������� 461 Exchanging Data with Another Device��������������������������������������������������������������������������������������������������������������� 465 Advanced Networking���������������������������������������������������������������������������������������������������������������479 One Last Detail��������������������������������������������������������������������������������������������������������������������������480 Summary�����������������������������������������������������������������������������������������������������������������������������������482 ■■Chapter 15: If You Build It  . .�����������������������������������������������������������������������������������������485 How Interface Builder Files Work����������������������������������������������������������������������������������������������486 Compiling Interface Builder Files����������������������������������������������������������������������������������������������������������������������� 486 Loading a Scene������������������������������������������������������������������������������������������������������������������������������������������������ 486 Loading an xib File�������������������������������������������������������������������������������������������������������������������������������������������� 488 www.it-ebooks.info xviii Contents Placeholder Objects and the File’s Owner��������������������������������������������������������������������������������������������������������� 489 Creating Objects������������������������������������������������������������������������������������������������������������������������������������������������ 490 Editing Attributes����������������������������������������������������������������������������������������������������������������������������������������������� 493 Connections������������������������������������������������������������������������������������������������������������������������������������������������������� 493 Sending Action Messages��������������������������������������������������������������������������������������������������������������������������������� 496 Taking Control of Interface Builder Files�����������������������������������������������������������������������������������496 Declaring Placeholders�������������������������������������������������������������������������������������������������������������������������������������� 497 Designing SYShapeView������������������������������������������������������������������������������������������������������������������������������������ 498 Connecting the Gesture Recognizers����������������������������������������������������������������������������������������������������������������� 499 Build Your Shape Factory����������������������������������������������������������������������������������������������������������������������������������� 501 Loading an Interface Builder File����������������������������������������������������������������������������������������������������������������������� 504 Replacing Code�������������������������������������������������������������������������������������������������������������������������������������������������� 505 Summary�����������������������������������������������������������������������������������������������������������������������������������506 ■■Chapter 16: Apps with Attitude�������������������������������������������������������������������������������������507 Leveler���������������������������������������������������������������������������������������������������������������������������������������508 Creating Leveler������������������������������������������������������������������������������������������������������������������������������������������������ 508 Getting Motion Data������������������������������������������������������������������������������������������������������������������514 Creating CMMotionManager������������������������������������������������������������������������������������������������������������������������������ 515 Starting and Stopping Updates�������������������������������������������������������������������������������������������������������������������������� 516 Push Me, Pull You����������������������������������������������������������������������������������������������������������������������������������������������� 516 Timing is Everything������������������������������������������������������������������������������������������������������������������������������������������ 517 Herky-Jerky������������������������������������������������������������������������������������������������������������������������������������������������������� 519 Getting Other Kinds of Motion Data�������������������������������������������������������������������������������������������524 Gyroscope Data������������������������������������������������������������������������������������������������������������������������������������������������� 524 Magnetometer Data������������������������������������������������������������������������������������������������������������������������������������������� 525 Device Motion and Attitude�������������������������������������������������������������������������������������������������������������������������������� 525 Measuring Change��������������������������������������������������������������������������������������������������������������������������������������������� 527 Summary�����������������������������������������������������������������������������������������������������������������������������������528 ■■Chapter 17: Where Are You?������������������������������������������������������������������������������������������529 Creating Pigeon�������������������������������������������������������������������������������������������������������������������������529 Collecting Location Data�����������������������������������������������������������������������������������������������������������531 www.it-ebooks.info Contents xix Using a Map View����������������������������������������������������������������������������������������������������������������������533 Decorating Your Map�����������������������������������������������������������������������������������������������������������������537 Adding an Annotation���������������������������������������������������������������������������������������������������������������������������������������� 537 Map Coordinates������������������������������������������������������������������������������������������������������������������������������������������������ 539 Adding a Little Bounce��������������������������������������������������������������������������������������������������������������������������������������� 540 Pointing the Way������������������������������������������������������������������������������������������������������������������������542 Location Monitoring������������������������������������������������������������������������������������������������������������������545 Approximate Location and Non-GPS Devices���������������������������������������������������������������������������������������������������� 546 Monitoring Regions������������������������������������������������������������������������������������������������������������������������������������������� 546 Reducing Location Change Messages��������������������������������������������������������������������������������������������������������������� 547 Movement and Heading������������������������������������������������������������������������������������������������������������������������������������� 547 Geocoding���������������������������������������������������������������������������������������������������������������������������������547 Getting Directions����������������������������������������������������������������������������������������������������������������������548 Summary�����������������������������������������������������������������������������������������������������������������������������������548 ■■Chapter 18: Remember Me?������������������������������������������������������������������������������������������551 Property Lists����������������������������������������������������������������������������������������������������������������������������551 Serializing Property Lists����������������������������������������������������������������������������������������������������������552 User Defaults�����������������������������������������������������������������������������������������������������������������������������552 Making Pigeon Remember��������������������������������������������������������������������������������������������������������553 Minimizing Updates and Code��������������������������������������������������������������������������������������������������������������������������� 554 Defining Your Keys��������������������������������������������������������������������������������������������������������������������������������������������� 554 Writing Values to User Defaults������������������������������������������������������������������������������������������������������������������������� 555 Getting Values from User Defaults��������������������������������������������������������������������������������������������������������������������� 555 Testing User Defaults����������������������������������������������������������������������������������������������������������������������������������������� 556 Registering Default Values��������������������������������������������������������������������������������������������������������557 Turning Objects into Property List Objects��������������������������������������������������������������������������������559 Preserving and Restoring savedLocation���������������������������������������������������������������������������������������������������������� 560 Persistent Views������������������������������������������������������������������������������������������������������������������������563 Fading Into the Background������������������������������������������������������������������������������������������������������������������������������ 564 Preserving View Controllers������������������������������������������������������������������������������������������������������������������������������ 565 www.it-ebooks.info xx Contents Assigning Restoration Identifiers����������������������������������������������������������������������������������������������������������������������� 566 Customizing Restoration������������������������������������������������������������������������������������������������������������������������������������ 568 Deeper Restoration�������������������������������������������������������������������������������������������������������������������������������������������� 569 Pigeons in the Cloud������������������������������������������������������������������������������������������������������������������569 Storing Values in the Cloud�������������������������������������������������������������������������������������������������������������������������������� 570 Cloud Watching�������������������������������������������������������������������������������������������������������������������������������������������������� 571 Enabling iCloud�������������������������������������������������������������������������������������������������������������������������������������������������� 573 Testing the Cloud����������������������������������������������������������������������������������������������������������������������������������������������� 574 Bundle Up Your Settings������������������������������������������������������������������������������������������������������������575 Creating a Settings Bundle�������������������������������������������������������������������������������������������������������������������������������� 575 Using Your Settings Bundle Values�������������������������������������������������������������������������������������������������������������������� 578 Testing Your Settings Bundle����������������������������������������������������������������������������������������������������������������������������� 579 Summary�����������������������������������������������������������������������������������������������������������������������������������580 ■■Chapter 19: Doc, You Meant Storage�����������������������������������������������������������������������������581 Document Overview������������������������������������������������������������������������������������������������������������������581 Where, Oh Where, Do My Documents Go?���������������������������������������������������������������������������������582 MyStuff on Documents��������������������������������������������������������������������������������������������������������������583 Supplying Your Document’s Data����������������������������������������������������������������������������������������������585 Wrapping Up Your Data�������������������������������������������������������������������������������������������������������������������������������������� 587 Using Wrappers������������������������������������������������������������������������������������������������������������������������������������������������� 587 Incremental Document Updates������������������������������������������������������������������������������������������������������������������������ 588 Constructing Your Wrappers������������������������������������������������������������������������������������������������������������������������������ 588 Interpreting Your Wrappers�������������������������������������������������������������������������������������������������������������������������������� 589 Archiving Objects����������������������������������������������������������������������������������������������������������������������591 Adopting NSCoding�������������������������������������������������������������������������������������������������������������������������������������������� 591 Archiving and Unarchiving Objects�������������������������������������������������������������������������������������������������������������������� 593 The Archiving Serialization Smackdown����������������������������������������������������������������������������������������������������������� 593 Serialization, Meet Archiving����������������������������������������������������������������������������������������������������������������������������� 594 Document, Your Data Model������������������������������������������������������������������������������������������������������595 Tracking Changes����������������������������������������������������������������������������������������������������������������������598 Testing Your Document��������������������������������������������������������������������������������������������������������������600 www.it-ebooks.info Contents xxi Setting Breakpoints������������������������������������������������������������������������������������������������������������������������������������������� 600 Stepping Through Code and Examining Variables��������������������������������������������������������������������������������������������� 601 Storing Image Files�������������������������������������������������������������������������������������������������������������������608 Odds and Ends��������������������������������������������������������������������������������������������������������������������������615 iCloud Storage��������������������������������������������������������������������������������������������������������������������������������������������������� 615 Archive Versioning��������������������������������������������������������������������������������������������������������������������������������������������� 615 Summary�����������������������������������������������������������������������������������������������������������������������������������616 ■■Chapter 20: Being Objective������������������������������������������������������������������������������������������617 Objective‑C is C�������������������������������������������������������������������������������������������������������������������������618 Objective‑C Classes������������������������������������������������������������������������������������������������������������������619 Implementing Your Class����������������������������������������������������������������������������������������������������������������������������������� 620 Creating and Destroying Objects����������������������������������������������������������������������������������������������������������������������� 621 Class Clusters���������������������������������������������������������������������������������������������������������������������������������������������������� 622 Referring to Objects������������������������������������������������������������������������������������������������������������������������������������������� 622 Can I See Your id?���������������������������������������������������������������������������������������������������������������������������������������������� 623 Method Names��������������������������������������������������������������������������������������������������������������������������624 Method Name Construction������������������������������������������������������������������������������������������������������������������������������� 625 The +initialize Method��������������������������������������������������������������������������������������������������������������������������������������� 626 Properties����������������������������������������������������������������������������������������������������������������������������������627 Instance Variables��������������������������������������������������������������������������������������������������������������������������������������������� 627 Using Getters and Setters���������������������������������������������������������������������������������������������������������������������������������� 627 Declared Properties������������������������������������������������������������������������������������������������������������������������������������������� 628 Automatic Properties����������������������������������������������������������������������������������������������������������������������������������������� 630 The Anatomy of a Property�������������������������������������������������������������������������������������������������������������������������������� 631 Keeping Your Promises�������������������������������������������������������������������������������������������������������������������������������������� 633 Introspection�����������������������������������������������������������������������������������������������������������������������������634 Class������������������������������������������������������������������������������������������������������������������������������������������������������������������ 634 Method�������������������������������������������������������������������������������������������������������������������������������������������������������������� 635 Protocol������������������������������������������������������������������������������������������������������������������������������������������������������������� 635 Protocols�����������������������������������������������������������������������������������������������������������������������������������636 Adopting Protocols�������������������������������������������������������������������������������������������������������������������������������������������� 636 Referring to Conforming Objects����������������������������������������������������������������������������������������������������������������������� 637 www.it-ebooks.info xxii Contents Categories���������������������������������������������������������������������������������������������������������������������������������638 Single Responsibility����������������������������������������������������������������������������������������������������������������������������������������� 639 Module Organization������������������������������������������������������������������������������������������������������������������������������������������ 639 Private Methods������������������������������������������������������������������������������������������������������������������������������������������������� 640 Extensions��������������������������������������������������������������������������������������������������������������������������������������������������������� 640 nil is Your Friend������������������������������������������������������������������������������������������������������������������������641 The Unbearable Lightness of nil������������������������������������������������������������������������������������������������������������������������ 641 The Virtues of Being Positive����������������������������������������������������������������������������������������������������������������������������� 641 When nil Is Bad�������������������������������������������������������������������������������������������������������������������������������������������������� 642 Copying Objects������������������������������������������������������������������������������������������������������������������������642 Adopting NSCopying������������������������������������������������������������������������������������������������������������������������������������������ 643 Inheriting NSCopying����������������������������������������������������������������������������������������������������������������������������������������� 644 Copying Something Special������������������������������������������������������������������������������������������������������������������������������� 644 Copying an Object���������������������������������������������������������������������������������������������������������������������������������������������� 645 Mutable Copies�������������������������������������������������������������������������������������������������������������������������������������������������� 645 Attributed Strings����������������������������������������������������������������������������������������������������������������������645 Collections���������������������������������������������������������������������������������������������������������������������������������648 Collection Classes��������������������������������������������������������������������������������������������������������������������������������������������� 648 Enumeration������������������������������������������������������������������������������������������������������������������������������������������������������ 649 Fast Object Enumeration����������������������������������������������������������������������������������������������������������������������������������� 649 Collection Enumeration�������������������������������������������������������������������������������������������������������������������������������������� 650 Shortcuts�����������������������������������������������������������������������������������������������������������������������������������650 Summary�����������������������������������������������������������������������������������������������������������������������������������651 ■■Chapter 21: The Elephant in the Room��������������������������������������������������������������������������653 Memory Management���������������������������������������������������������������������������������������������������������������654 Your Grandfather’s Memory Management��������������������������������������������������������������������������������655 Garbage Collection��������������������������������������������������������������������������������������������������������������������656 Reference Counting�������������������������������������������������������������������������������������������������������������������657 Manual Reference Counting������������������������������������������������������������������������������������������������������659 Jumping into the Pool���������������������������������������������������������������������������������������������������������������������������������������� 660 Quick Summary������������������������������������������������������������������������������������������������������������������������������������������������� 662 www.it-ebooks.info Contents xxiii Breaking the Cycle��������������������������������������������������������������������������������������������������������������������������������������������� 663 Scared Straight�������������������������������������������������������������������������������������������������������������������������������������������������� 665 Automatic Reference Counting��������������������������������������������������������������������������������������������������665 Enabling ARC����������������������������������������������������������������������������������������������������������������������������������������������������� 666 Strong and Weak References���������������������������������������������������������������������������������������������������������������������������� 667 What ARC Doesn’t Do����������������������������������������������������������������������������������������������������������������������������������������� 668 Summary�����������������������������������������������������������������������������������������������������������������������������������669 ■■Chapter 22: Êtes-vous Polyglotte?��������������������������������������������������������������������������������671 The Localization Process�����������������������������������������������������������������������������������������������������������671 Language Bundles���������������������������������������������������������������������������������������������������������������������672 Programmatic Localization�������������������������������������������������������������������������������������������������������������������������������� 673 Localize Now!����������������������������������������������������������������������������������������������������������������������������674 Internationalizing Your App��������������������������������������������������������������������������������������������������������680 Internationalizing String Constants������������������������������������������������������������������������������������������������������������������� 680 Using the genstrings Tool���������������������������������������������������������������������������������������������������������������������������������� 681 Localizing Your Strings File�������������������������������������������������������������������������������������������������������������������������������� 683 Testing Your String Localization������������������������������������������������������������������������������������������������������������������������� 684 Localizing Interfaces Using Localizable Strings������������������������������������������������������������������������685 Localizing Settings.bundle��������������������������������������������������������������������������������������������������������685 Other Code Considerations��������������������������������������������������������������������������������������������������������688 Localizing Your App’s Name������������������������������������������������������������������������������������������������������689 Summary�����������������������������������������������������������������������������������������������������������������������������������690 ■■Chapter 23: Faster, Faster���������������������������������������������������������������������������������������������691 Performance Optimization���������������������������������������������������������������������������������������������������������692 Fixing a Slow Tap����������������������������������������������������������������������������������������������������������������������693 Launching Instruments�������������������������������������������������������������������������������������������������������������������������������������� 694 Finding the Hot Spots����������������������������������������������������������������������������������������������������������������696 The Hubris of Experience����������������������������������������������������������������������������������������������������������������������������������� 699 Picking the Low Hanging Fruit��������������������������������������������������������������������������������������������������������������������������� 700 Deferring Notifications��������������������������������������������������������������������������������������������������������������������������������������� 700 Once More into The Breach������������������������������������������������������������������������������������������������������������������������������� 701 www.it-ebooks.info xxiv Contents Precious Memory����������������������������������������������������������������������������������������������������������������������702 Breaking MyStuff����������������������������������������������������������������������������������������������������������������������������������������������� 703 Measuring Memory������������������������������������������������������������������������������������������������������������������������������������������� 704 Memory Instruments����������������������������������������������������������������������������������������������������������������������������������������� 706 Heed the Warnings�������������������������������������������������������������������������������������������������������������������������������������������� 708 Stress Test, Round #2���������������������������������������������������������������������������������������������������������������������������������������� 710 Summary�����������������������������������������������������������������������������������������������������������������������������������712 ■■Chapter 24: Twice As Nice���������������������������������������������������������������������������������������������713 Concurrent Programming����������������������������������������������������������������������������������������������������������714 Threads�������������������������������������������������������������������������������������������������������������������������������������������������������������� 715 Synchronization������������������������������������������������������������������������������������������������������������������������������������������������� 715 Running Multiple Threads���������������������������������������������������������������������������������������������������������716 Creating an Operation Queue���������������������������������������������������������������������������������������������������������������������������� 717 Adding an Operation������������������������������������������������������������������������������������������������������������������������������������������ 718 Measuring the Effects��������������������������������������������������������������������������������������������������������������������������������������� 718 Execution Order�������������������������������������������������������������������������������������������������������������������������720 Thread Safety����������������������������������������������������������������������������������������������������������������������������721 Don’t Talk About Thread Safety�������������������������������������������������������������������������������������������������������������������������� 722 Not Sharing Is Caring����������������������������������������������������������������������������������������������������������������������������������������� 722 Promise Me You’ll Never Change����������������������������������������������������������������������������������������������������������������������� 723 The Atomic Age�������������������������������������������������������������������������������������������������������������������������������������������������� 723 Concurrency Roundup���������������������������������������������������������������������������������������������������������������729 The Thread-Safe Landscape������������������������������������������������������������������������������������������������������������������������������ 729 Sending Messages To Main������������������������������������������������������������������������������������������������������������������������������� 729 Lock Objects������������������������������������������������������������������������������������������������������������������������������������������������������ 730 Deadlocks���������������������������������������������������������������������������������������������������������������������������������������������������������� 731 Spin Locks��������������������������������������������������������������������������������������������������������������������������������������������������������� 733 Further Reading������������������������������������������������������������������������������������������������������������������������������������������������� 734 Summary�����������������������������������������������������������������������������������������������������������������������������������734 Index���������������������������������������������������������������������������������������������������������������������������������735 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, Android, and Windows Phone platforms He graduated from Stanford University with B.S and M.S degrees in engineering He lives in Southern California and runs a photography business with his wife (www.facebook.com/BellaLenteStudios) 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 Clay Andres started this ball rolling by introducing me to Apress Steve Anglin is largely responsible for deciding what Apress prints, and I was genuinely flattered when he asked me to write this book Tom Welsh, my editor, kept a watchful eye on every paragraph, keeping the message clear and comprehensible Tom was great to work with and immeasurably improved the quality of this book Charles Cruz checked every line of code and symbol to ensure complete accuracy Any technical errors are ultimately my responsibility, but there are significantly fewer thanks to Charles Lori Cavanaugh dotted my i’s, crossed my t’s, and corrected my (egregious) spelling If you find this book easy to read, you have Lori’s blue pencil to thank The entire project was stewarded by a phalanx of coordinating editors Anamika Panchoo, Katie Sullivan, and Christine Ricketts juggled schedules, liaised between editors, tracked production, and herded everyone towards a common goal 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 iOS development would be nearly impossible without it xxix www.it-ebooks.info ... give away, your apps through Apple’s App Store  Gain access to Apple’s Developer Forums and other resources  Give your apps to people directly (outside of the App Store)  Develop apps that use... Xcode development tools You’ll need those nn Boom! App will walk you through the core steps in creating an iOS app no programming needed nn Spin a Web creates an app that leverages the power of iOS s... test, 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 iOS Developer You

Ngày đăng: 27/03/2019, 10:39

Mục lục

  • Learn iOS App Development

    • Contents at a Glance

    • About the Technical Reviewer

    • Chapter 1: Got Tools?

      • Requirements

      • Becoming an iOS Developer

      • Launching Xcode the First Time

      • Welcome To Xcode

        • Navigation Area

        • Running Your First App

        • Building an Interface

          • Adding Objects

          • Deleting and Connecting Objects

          • Adding Views to a View

          • Using Storyboards

            • Adding New Screens

            • Debugging Your App

              • Adding Constraints

              • Chapter 3: Spin a Web

                • Design

                • Building a Web Browser

                • Coding a Web Browser

                  • Adding Outlets to SUViewController

                  • Adding Actions to SUViewController

                  • Testing the Web Browser

                  • Debugging the Web View

                  • Adding URL Shortening

                    • Designing the URL Shortening Code

                    • Becoming a Web View Delegate

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

  • Đang cập nhật ...

Tài liệu liên quan