beginning ios 7 development exploring the ios sdk

705 1.1K 2
beginning ios 7 development exploring the ios sdk

Đ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

COMPANION eBOOK US $49.99 Shelve in Mobile Computing User level: Beginning–Intermediate www.apress.com Books for professionals By professionals ® SOURCE CODE ONLINE Companion eBook Available Nutting Olsson Mark LaMarche Jack Nutting | Fredrik Olsson | Dave Mark | Jeff LaMarche iOS 7 Development Beginning Design and develop your app from concept and vision to code Beginning iOS 7 Development Exploring the iOS SDK T he team that brought you the bestselling Beginning iPhone Development is back again for Beginning iOS 7 Development, bringing this definitive guide up-to-date with Apple’s latest and greatest iOS 7 SDK, as well as with the latest version of Xcode. There’s coverage of brand-new technologies, with chapters on storyboards and iCloud, for example, as well as significant updates to existing material. You’ll have everything you need to create your very own apps for the latest iOS devices. Every single sample app in the book has been rebuilt from scratch using latest Xcode and the latest 64-bit iOS 7-specific project templates and designed to take advantage of the latest Xcode features. Assuming only a minimal working knowledge of Objective-C, and written in a friendly, easy-to-follow style, Beginning iOS 7 Development offers a complete soup-to-nuts course in iPhone, iPad, and iPod touch programming. The book starts with the basics, walking through the process of downloading and installing Xcode and the iOS 7 SDK, and then guides you though the creation of your first simple application. From there, you’ll learn how to integrate all the interface elements Apple touch users have come to know and love, such as buttons, switches, pickers, toolbars, and sliders. You’ll master a variety of design patterns, from the simplest single view to complex hierarchical drill-downs. The confusing art of table building will be demystified, and you’ll learn how to save your data using the iPhone file system. You’ll also learn how to save and retrieve your data using a variety of persistence techniques, including Core Data and SQLite. And there’s much more! What You’ll Learn: • Everything you need to know to develop your own bestselling iPhone and iPad apps • Best practices for optimizing your code and delivering great user experiences • What is data persistence and why is it important • Get started with building cool, crisp user interfaces • How to use Table Views • How to do graphics with Quartz and OpenGL ES • What geo app development features the new iOS brings to the iPhone • How to get your app to work with the Cloud and more 9781430 260226 54999 ISBN 978-1-4302-6022-6 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. v Contents at a Glance About the Authors ��������������������������������������������������������������������������������������������������������������xxi About the Technical Reviewer �����������������������������������������������������������������������������������������xxiii Acknowledgments ������������������������������������������������������������������������������������������������������������ xxv Introduction �������������������������������������������������������������������������������������������������������������������� xxvii Chapter 1: Welcome to the Jungle ■ �������������������������������������������������������������������������������������1 Chapter 2: Appeasing the Tiki Gods ■ ���������������������������������������������������������������������������������13 Chapter 3: Handling Basic Interaction ■ �����������������������������������������������������������������������������45 Chapter 4: More User Interface Fun ■ ��������������������������������������������������������������������������������71 Chapter 5: Autorotation and Autosizing ■ ������������������������������������������������������������������������119 Chapter 6: Multiview Applications ■ ��������������������������������������������������������������������������������141 Chapter 7: Tab Bars and Pickers ■ �����������������������������������������������������������������������������������171 Chapter 8: Introduction to Table Views ■ �������������������������������������������������������������������������215 Chapter 9: Navigation Controllers and Table Views ■ �������������������������������������������������������263 Chapter 10: Collection View ■ ������������������������������������������������������������������������������������������299 Chapter 11: iPad Considerations ■ �����������������������������������������������������������������������������������313 Chapter 12: Application Settings and User Defaults ■ ������������������������������������������������������343 Chapter 13: Basic Data Persistence ■ ������������������������������������������������������������������������������379 vi Contents at a Glance Chapter 14: Documents and iCloud ■ �������������������������������������������������������������������������������425 Chapter 15: Grand Central Dispatch, Background Processing, and You ■ ������������������������455 Chapter 16: Drawing with Core Graphics ■ ����������������������������������������������������������������������489 Chapter 17: Getting Started with Sprite Kit ■ �������������������������������������������������������������������519 Chapter 18: Taps, Touches, and Gestures ■ ����������������������������������������������������������������������563 Chapter 19: Where Am I? Finding Your Way with Core Location and Map Kit ■ ���������������595 Chapter 20: Whee! Gyro and Accelerometer! ■ ����������������������������������������������������������������613 Chapter 21: The Camera and Photo Library ■ �������������������������������������������������������������������641 Chapter 22: Application Localization ■ �����������������������������������������������������������������������������653 Index ���������������������������������������������������������������������������������������������������������������������������������675 xxvii Introduction This book is enormous. If you are reading a paper copy, you’re well aware of this, but if you’ve got a digital copy, you might not understand the size of this thing. I mean, it’s larger than most bibles I’ve seen. Not that I’m an expert on bibles or anything, but you know: they’re big, fat books. If you asked, “Is Beginning iOS 7 Development bigger than a breadbox?” I’d have to say, “Almost.” And yet, this book is made of smaller pieces, portioned out so that you should be able to tackle the contents of any single chapter in a delightful afternoon, learning things about iOS development that you probably never imagined. When I say “tackle,” I don’t mean just read each chapter. I mean you need to actually sit down in front of a Mac and work your way through it. Building all the example apps as you go through each chapter will help imprint all the usage patterns and concepts into your brain in a way that reading alone could never do. If you work your way through this book, you will come away with a great understanding of the foundations of iOS app development, and you will be more than ready to build iOS apps all on your own. Many years ago, I met the late Torfrid Olsson, a Swedish sculptor from a rural area of northern Sweden. I expressed to him some envy and admiration about one aspect of his life, and his reply stuck with me: “Ah, that’s just something you’ve read about in books. You have your own life that is uniquely yours. What makes you think that it’s missing anything?” My hope is that you don’t let the knowledge contained in these pages just be something you read in a book. Read it, of course, but also work through it, understand it, and wrestle it to the ground if you must. Make it yours. —Jack Nutting Stockholm 2014 1 Chapter 1 Welcome to the Jungle So, you want to write iPhone, iPod touch, and iPad applications? Well, we can’t say that we blame you. iOS, the core software of all of these devices, is an exciting platform that has been seeing explosive growth since it first came out in 2007. The rise of the mobile software platform means that people are using software everywhere they go. With the release of iOS 7, Xcode 5, and the latest incarnation of the iOS software development kit (SDK), things have only gotten better and more interesting. What this Book Is This book is a guide to help you get started down the path to creating your own iOS applications. Our goal is to get you past the initial difficulties, to help you understand the way iOS applications work and how they are built. As you work your way through this book, you will create a number of small applications, each designed to highlight specific iOS features and to show you how to control or interact with those features. If you combine the foundation you’ll gain through this book with your own creativity and determination, and then add in the extensive and well-written documentation provided by Apple, you’ll have everything you need to build your own professional iPhone and iPad applications. Tip Jack, Dave, Jeff, and Fredrik have set up a forum for this book. It’s a great place to meet like-minded folks, get your questions answered, and even answer other people’s questions. The forum is at http://forum.learncocoa.org. Be sure to check it out! 2 CHAPTER 1: Welcome to the Jungle What You Need Before you can begin writing software for iOS, you’ll need a few items. For starters, you’ll need an Intel-based Macintosh, running Mountain Lion (OS X 10.8), Mavericks (OS X 10.9) or later. Any recent Intel-based Macintosh computer—laptop or desktop—should work just fine. To get access to the latest and greatest from Apple, you’ll also really need to sign up to become a registered iOS developer. To create your developer account, just navigate to http://developer.apple.com/ios/. That will bring you to a page similar to the one shown in Figure 1-1. Figure 1-1. Apple’s iOS Dev Center website 3CHAPTER 1: Welcome to the Jungle First, click Log in. You’ll be prompted for your Apple ID. If you don’t have an Apple ID, click Join now, create such an ID, and then log in. Once you are logged in, you’ll be taken to the main iOS development page. You’ll find links to a wealth of documentation, videos, sample code, and the like—all dedicated to teaching you the finer points of iOS application development. The most important tool you’ll be using to develop iOS applications is called Xcode. Xcode is Apple’s integrated development environment (IDE). Xcode includes tools for creating and debugging source code, compiling applications, and performance tuning the applications you’ve written. You can download Xcode from the Mac App Store, which you can access from your Mac’s Apple menu. SDK VERSIONS AND SOURCE CODE FOR THE EXAMPLES As the versions of the SDK and Xcode evolve, the mechanism for downloading them will also change. For the past few years, Apple has been publishing the current “stable” version of Xcode and the iOS SDK on the Mac App Store, while simultaneously often providing developers the ability to download preview versions of upcoming releases from its developer site. Bottom line: you want to download the latest released (non-beta) version of Xcode and the iOS SDK, so use the Mac App Store. This book has been written to work with the latest version of the SDK. In some places, we have chosen to use new functions or methods introduced with iOS 7 that may prove incompatible with earlier versions of the SDK. We’ll be sure to point those situations out as they arise in this book. Be sure to download the latest and greatest source code archives from http://learncocoa.org or from the book’s forum at http://forum.learncocoa.org. We’ll update the code as new versions of the SDK are released, so be sure to check the site periodically. Developer Options The free Xcode download includes a simulator that will allow you to build and run iPhone and iPad apps on your Mac. This is perfect for learning how to program for iOS. However, the simulator does not support many hardware-dependent features, such as the accelerometer and camera. Also, the free option will not allow you to install your applications onto a real iPhone or other device, and it does not give you the ability to distribute your applications on Apple’s App Store. For those capabilities, you’ll need to sign up for one of the other options, which aren’t free: The Standard program costs $99/year. It provides a host of development tools and resources, technical support, distribution of your application via Apple’s App Store, and, most importantly, the ability to test and debug your code on an iOS device, rather than just in the simulator. The Enterprise program costs $299/year. It is designed for companies developing proprietary, in-house iOS applications. For more details on these programs, visit http://developer.apple.com/programs/ios and http://developer.apple.com/programs/ios/enterprise to compare the two. 4 CHAPTER 1: Welcome to the Jungle Because iOS supports an always-connected mobile device that uses other companies’ wireless infrastructure, Apple has needed to place far more restrictions on iOS developers than it ever has on Mac developers (who are able—at the moment, anyway—to write and distribute programs with absolutely no oversight or approval from Apple). Even though the iPod touch and the Wi-Fi- only versions of the iPad don’t use anyone else’s infrastructure, they’re still subject to these same restrictions. Apple has not added restrictions to be mean, but rather as an attempt to minimize the chances of malicious or poorly written programs being distributed that could degrade performance on the shared network. Developing for iOS may appear to present a lot of hoops to jump through, but Apple has expended quite an effort to make the process as painless as possible. And also consider that $99 is still much less expensive than buying, for example, Visual Studio, which is Microsoft’s software development IDE. This may seem obvious, but you’ll also need an iPhone, iPod touch, or iPad. While much of your code can be tested using the iOS simulator, not all programs can be. And even those that can run on the simulator really need to be thoroughly tested on an actual device before you ever consider releasing your application to the public. Note If you are going to sign up for the Standard or Enterprise program, you should do it right now. The approval process can take a while, and you’ll need that approval to be able to run your applications on an actual device. Don’t worry, though, because all the projects in the first several chapters and the majority of the applications in this book will run just fine on the iOS simulator. What You Need to Know This book assumes that you already have some programming knowledge. It assumes that you understand the fundamentals of programming in general and object-oriented programming in particular (you know what classes, objects, loops, and variables are, for example). It also assumes that you are familiar with the Objective-C programming language. Cocoa Touch, the part of the SDK that you will be working with through most of this book, uses the latest version of Objective-C, which contains several new features not present in earlier versions. But don’t worry if you’re not familiar with the more recent additions to the Objective-C language. We highlight any of the new language features we take advantage of, and explain how they work and why we are using them. You should also be familiar with iOS itself, as a user. Just as you would with any platform for which you wanted to write an application, get to know the nuances and quirks of the iPhone, iPad, or iPod touch. Take the time to get familiar with the iOS interface and with the way Apple’s iPhone and/or iPad applications look and feel. 5CHAPTER 1: Welcome to the Jungle NEW TO OBJECTIVE-C? If you have not programmed in Objective-C before, here are a few resources to help you get started: • Learn Objective-C on the Mac: For OS X and iOS (2nd edition, Apress, 2012): this is an excellent and approachable introduction to Objective-C by Mac-programming experts Scott Knaster, Waqar Malik, and Mark Dalrymple. You can find more information at http://www.apress.com/book/ view/9781430241881. • Programming with Objective-C: this is Apple’s introduction to the language. You can find more information at https://developer.apple.com/library/mac/documentation/cocoa/ conceptual/ProgrammingWithObjectiveC. What’s Different About Coding for iOS? If you have never programmed in Cocoa or its predecessors NeXTSTEP or OpenStep, you may find Cocoa Touch—the application framework you’ll be using to write iOS applications—a little alien. It has some fundamental differences from other common application frameworks, such as those used when building .NET or Java applications. Don’t worry too much if you feel a little lost at first. Just keep plugging away at the exercises, and it will all start to fall into place after a while. If you have written programs using Cocoa or NeXTSTEP, a lot in the iOS SDK will be familiar to you. A great many classes are unchanged from the versions that are used to develop for OS X. Even those that are different tend to follow the same basic principles and similar design patterns. However, several differences exist between Cocoa and Cocoa Touch. Regardless of your background, you need to keep in mind some key differences between iOS development and desktop application development. These differences are discussed in the following sections. Only One Active Application On iOS, only one application can be active and displayed on the screen at any given time. Since iOS 4, applications have been able to run in the background after the user presses the “home” button, but even that is limited to a narrow set of situations, and you must code for it, specifically. When your application isn’t active or running in the background, it doesn’t receive any attention whatsoever from the CPU, which will wreak havoc with open network connections and the like. iOS allows background processing, but making your apps play nicely in this situation will require some effort on your part. Only One Window Desktop and laptop operating systems allow many running programs to coexist, each with the ability to create and control multiple windows. However, iOS gives your application just one “window” to work with. All of your application’s interaction with the user takes place inside this one window, and its size is fixed at the size of the screen. [...]... go from here? In the appendix, we’ll explore the logical next steps for you to take on your journey to master the iOS SDK What’s New in this Update? Since the first edition of this book hit the bookstores, the growth of the iOS development community has been phenomenal The SDK has continually evolved, with Apple releasing a steady stream of SDK updates Well, we’ve been busy, too! iOS 7 contains a lot... more about how these are used soon 20 CHAPTER 2: Appeasing the Tiki Gods The Navigator Just below the toolbar, on the left side of the project window, is the navigator If you used the navigator toggle button to hide this earlier, tap the button again to show the navigator The navigator offers eight views that show you different aspects your project Click one of the icons at the top of the navigator... click any segment showing the name of a group or a file to see all the other files and groups located at the same point in the hierarchy The final segment shows a list of items within the selected file In Figure 2-15, you’ll see that the tail end of the jump bar is a pop-up that shows the methods and other symbols contained within the currently selected file The jump bar shows the file BIDAppDelegate.m,... includes, and more! The submenus shown here will take you to just about any other code that touches the code currently open in the editor  To the right of the über menu are left and right arrows that take you back to the previous file and return you to the next file, respectively  The jump bar includes a segmented pop-up that displays the hierarchical path to reach the selected file in the project You... specific log, and the build command and any build issues are displayed in the edit pane Figure 2-14.  The Xcode log navigator The log navigator displays a list of builds, with the details associated with a selected view displayed in the edit pane 26 CHAPTER 2: Appeasing the Tiki Gods The Jump Bar Across the top of the editor, you’ll find a special control called the jump bar With a single click, the jump bar... View” section earlier in the chapter) or by pressing 1 Tip  The eight navigator configurations can be accessed using the keyboard shortcuts 1 to 8 The numbers correspond to the icons starting on the left, so 1 is the project navigator, 2 is the symbol navigator, and so on up to 8, which takes you to the log navigator The first item in the project navigator list bears the same name as your project—in... which allows you to specify the level of debug detail you want to see The debug navigator lists the stack frame for each active thread A stack frame is a list of the functions or methods that have been called previously, in the order they were called Click a method, and the associated code appears in the editor pane In the editor, there will be a second pane that lets you control the debugging process,... and access the low-level debugger A slider at the bottom of the debug navigator allows you to control the level of detail it tracks Slide to the extreme right to see everything, including all the system calls Slide to the extreme left to see only your calls The default setting of right in the middle is a good place to start  Breakpoint navigator: The breakpoint navigator lets you see all the breakpoints... use the prefix BID, which stands for Beginning iOS Development While there are likely to be many classes named, for example, MyViewController, far fewer classes are likely to be named BIDMyViewController This will significantly reduce the chance of conflicts CHAPTER 2: Appeasing the Tiki Gods 17 We also need to specify the Devices In other words, Xcode wants to know if we’re building an app for the. .. editing the implementation of an Objective-C class (the m file), Xcode will automatically show you that class’s header file (the h file) in the right pane If you’re designing your user interface on the left, Xcode will show you the code that user interface is able to interact with on the right You’ll see the assistant editor at work throughout the book  The version editor button converts the editor . LaMarche iOS 7 Development Beginning Design and develop your app from concept and vision to code Beginning iOS 7 Development Exploring the iOS SDK T he team that brought you the bestselling Beginning. 20 07. The rise of the mobile software platform means that people are using software everywhere they go. With the release of iOS 7, Xcode 5, and the latest incarnation of the iOS software development. of the SDK and Xcode evolve, the mechanism for downloading them will also change. For the past few years, Apple has been publishing the current “stable” version of Xcode and the iOS SDK on the

Ngày đăng: 07/04/2014, 15:01

Từ khóa liên quan

Mục lục

  • Contents at a Glance

  • Contents

  • About the Authors

  • About the Technical Reviewer

  • Acknowledgments

  • Introduction

  • Chapter 1: Welcome to the Jungle

    • What this Book Is

    • What You Need

      • Developer Options

      • What You Need to Know

      • What’s Different About Coding for iOS?

        • Only One Active Application

        • Only One Window

        • Limited Access

        • Limited Response Time

        • Limited Screen Size

        • Limited System Resources

        • No Garbage Collection, but . . .

        • Some New Stuff

        • A Different Approach

        • What’s in This Book

        • What’s New in this Update?

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

Tài liệu liên quan