intTypePromotion=1
zunia.vn Tuyển sinh 2024 dành cho Gen-Z zunia.vn zunia.vn
ADSENSE

iPhone and iPad in Action: Introduction to SDK Development

Chia sẻ: Phung Tuyet | Ngày: | Loại File: PDF | Số trang:433

204
lượt xem
9
download
 
  Download Vui lòng tải xuống để xem tài liệu đầy đủ

one and iPad in Action is an introductory book, intended to teach the basics of iPhone SDK programming in a tutorial form. It’s an updated revision of iPhone in Action, which first appeared in 2008. We encourage you to read it straight through, from chapter 1 to 19. This will introduce the platform, how to program for the iPhone and iPad, and step you through the entire process in tu

Chủ đề:
Lưu

Nội dung Text: iPhone and iPad in Action: Introduction to SDK Development

  1. Introduction to SDK Development IN ACTION Revised edition of i Phone in Action Brandon Trebitowski Christopher Allen Shannon Appelcline MANNING
  2. Praise for iPhone in Action …iPhone in Action is another smash hit by Manning Publications. Whether you are a novice programmer or an advanced programmer new to the iPhone platform, you are going to finish this book feeling ready to take on the world. —Panah Rad, iPhoneAppsFinder.com If I were teaching a class on the basics of developing for Apple’s new mobile platform, I would def- initely use this as the textbook...iPhone in Action is a complete primer to iPhone development. —Victor Agreda, Jr., The Unofficial Apple Weblog (TUAW) ...well-suited for existing programmers who want comprehensive coverage of the iPhone platform, the development of webapps, and the key concepts in developing native apps with the SDK along with examples. —Peter Cooper, Mobile Orchard …excels at providing detailed, organized, and clear information on the development particular- ities of the iPhone...iPhone in Action is the must-have book for developers looking to enter the realm of iPhone development. —Cody Overcash, Founder, ModMyi.com This book is invaluable for beginners because it shows all the possibilities for both web and SDK and it introduces all the key topics—something no other single title does. Experts will want this title too for the detailed web development topics. —Mark Johnson, reader There is no other iPhone title that has such great coverage of both web and SDK topics under one roof, providing a well-rounded developer education. —Vladimir Pasman, Cocoacast.com Download from Wow! eBook
  3. Download from Wow! eBook
  4. iPhone and iPad in Action INTRODUCTION TO SDK DEVELOPMENT BRANDON TREBITOWSKI CHRISTOPHER ALLEN SHANNON APPELCLINE Revised Edition of iPhone in Action MANNING Greenwich (74° w. long.) Download from Wow! eBook
  5. For online information and ordering of this and other Manning books, please visit www.manning.com. The publisher offers discounts on this book when ordered in quantity. For more information, please contact Special Sales Department Manning Publications Co. 180 Broad St. Suite 1323 Stamford, CT 06901 Email: orders@manning.com ©2011 by Manning Publications Co. All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form or by means electronic, mechanical, photocopying, or otherwise, without prior written permission of the publisher. Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in the book, and Manning Publications was aware of a trademark claim, the designations have been printed in initial caps or all caps. Recognizing the importance of preserving what has been written, it is Manning’s policy to have the books we publish printed on acid-free paper, and we exert our best efforts to that end. Recognizing also our responsibility to conserve the resources of our planet, Manning books are printed on paper that is at least 15 percent recycled and processed without the use of elemental chlorine. Manning Publications Co. Development editor: Katharine Osborne 180 Broad St. Copyeditor: Tiffany Taylor Suite 1323 Proofreader: Katie Tennant Stamford, CT 06901 Typesetter: Dottie Marsico Cover designer: Marija Tudor ISBN 978-1-935182-58-0 Printed in the United States of America 1 2 3 4 5 6 7 8 9 10 – MAL – 15 14 13 12 11 10 Download from Wow! eBook
  6. brief contents Introducing the iPhone and iPad 1 ■ 1 Learning Objective-C and the iPhone OS SDK 13 ■ 2 Using Xcode 36 ■ 3 Using Interface Builder 53 ■ 4 Creating basic view controllers 68 ■ 5 Monitoring events and actions 87 ■ 6 Creating advanced view controllers 111 ■ 7 Data: actions, preferences, and files 139 ■ 8 Data: advanced techniques 159 ■ 9 Positioning: accelerometers, location, and the compass 188 ■ 10 Media: images and the camera 210 ■ 11 Media: audio and recording 224 ■ 12 Graphics: Quartz, Core Animation, and OpenGL 243 ■ 13 The web: web views and internet protocols 271 ■ 14 Peer-to-peer connections using Game Kit 295 ■ 15 Push notification services 316 ■ 16 The Map Kit framework 328 ■ 17 In-app purchasing using Store Kit 342 ■ 18 iPhone SDK enhancements 357 ■ 19 v Download from Wow! eBook
  7. Download from Wow! eBook
  8. contents preface xvii acknowledgments xviii about this book xx 1 Introducing the iPhone and iPad 1 1.1 A bit of history 2 1.2 All for one and one for all: the iPhone OS platform 2 1.3 Core hardware specifications 3 The iPhone 4 The iPad 4 The iPod Touch 4 ■ ■ iPhone and iPad input and output specifications 5 iPhone and iPad network specifications 5 iPhone OS■ browser specifications 6 Mobile web standards 8 ■ Other hardware features 8 1.4 How the iPhone and iPad are unique 9 1.5 Understanding iPhone and iPad touch interaction 11 1.6 Summary 12 2 Learning Objective-C and the iPhone OS SDK 13 2.1 Getting ready for the SDK 14 Installing the SDK 14 The anatomy of the SDK 15 ■ vii Download from Wow! eBook
  9. viii CONTENTS 2.2 Introducing Objective-C 17 The big picture 17 The message 18 Class definition 20 ■ ■ Properties 22 Other compiler directives 24 Categories and ■ ■ protocols 24 Wrapping up Objective-C 25 ■ 2.3 Introducing the iPhone OS 26 The anatomy of the iPhone OS 26 The object hierarchy of the ■ iPhone OS 27 Windows and views 28 ■ 2.4 The iPhone OS’s methods 30 Object creation 30 Memory management 32 Event ■ ■ response 33 Lifecycle management 34 ■ 2.5 Summary 35 3 Using Xcode 36 3.1 Introducing Xcode 36 The anatomy of Xcode 37 Compiling and executing in ■ Xcode 38 3.2 Creating a first iPhone project in Xcode: Hello, World! 39 Understanding main.m 39 Understanding the application ■ delegate 40 Writing Hello, World! 42 ■ 3.3 Creating a first iPad project in Xcode: Hello, World! 44 Writing Hello, World! 45 3.4 Creating a new class in Xcode 45 The new class how-to 46 The header file 46 The source ■ ■ code file 47 Linking it in 49 ■ 3.5 Other Xcode functionality 49 Adding frameworks with Xcode 49 Using alternate templates ■ with Xcode 50 Xcode tips and tricks 51 ■ 3.6 Summary 52 4 Using Interface Builder 53 4.1 An introduction to Interface Builder 54 The anatomy of Interface Builder 54 Simulating in Interface ■ Builder 56 4.2 A first project in Interface Builder: pictures and the web 57 Creating new objects 57 Manipulating objects graphically 58 ■ Using the inspector window 58 Working with pictures 60 ■ Download from Wow! eBook
  10. ix CONTENTS 4.3 Building connections in Interface Builder 61 Declaring an IBOutlet 62 Connecting an object 62 ■ Coding with IBOutlets 63 4.4 Other Interface Builder functionality 65 Building other connections 65 Creating external objects 65 ■ Initializing Interface Builder objects 66 Accessing .xib files 66 ■ Creating new .xib files 67 4.5 Summary 67 5 Creating basic view controllers 68 5.1 The view controller family 69 5.2 The standard view controller 70 The anatomy of a view controller 70 Creating a view ■ controller 71 Creating another view controller 71 Building ■ ■ up a view controller interface 72 Using your view controller 73 ■ 5.3 The table view controller 78 The anatomy of a table view controller 78 Creating a table view ■ controller 78 Building up a table interface 80 Using your ■ ■ table view controller 85 5.4 Summary 85 6 Monitoring events and actions 87 6.1 An introduction to events 88 The responder chain 88 Touches and events 89 ■ 6.2 A touching example: the event reporter 91 Setting things up in Interface Builder 92 Preparing a view for ■ touches 93 Controlling your events 95 ■ 6.3 Other event functionality 97 Regulating events 97 Other event methods and properties 98 ■ 6.4 An introduction to actions 99 The UIControl object 99 Control events and actions 99 ■ Using addTarget:action:forControlEvents: 101 6.5 Adding a button to an application 102 Using addTarget:action:forControlEvents: with a button 102 Using an IBAction with a button 103 6.6 Other action functionality 104 Accepting text input with UITextField 104 Allowing value ■ selection with UISlider 106 A TextField/Slider mashup 106 ■ Actions made easy 108 Actions in use 108 ■ Download from Wow! eBook
  11. x CONTENTS 6.7 Introducing notifications 109 6.8 Summary 110 7 Creating advanced view controllers 111 7.1 The tab bar view controller 112 The anatomy of a tab bar view controller 112 Creating a tab ■ bar view controller 113 Building a tab bar interface 114 ■ Using your tab bar controller 118 7.2 The navigation controller 119 The anatomy of a navigation controller 119 Creating a ■ navigation controller 120 Completing the navigation ■ controller 122 Using your navigation controller 124 ■ 7.3 Using the flipside controller 127 7.4 The split view controller 129 Creating a split view controller 129 Building the split view ■ controller 130 Using your split view controller 134 ■ Adjusting the interface for vertical and landscape modes 135 7.5 Popover and modal view controllers 136 Creating a popover view controller 136 Creating a modal view ■ controller 137 7.6 Summary 138 8 Data: actions, preferences, and files 139 8.1 Accepting user actions 140 8.2 Maintaining user preferences 141 Creating your own preferences 142 Using the system settings 147 8.3 Opening files 151 Accessing your bundle 152 Accessing other directories 153 ■ Manipulating files 154 Filesaver: a UITextView example 155 ■ 8.4 Summary 158 9 Data: advanced techniques 159 9.1 Using SQLite 160 Setting up an SQLite database 160 Accessing SQLite 162 ■ Accessing your SQLite database 162 Building a navigation ■ menu from a database 163 Expanding this example 170 ■ Download from Wow! eBook
  12. xi CONTENTS 9.2 Accessing the Address Book 170 An overview of the frameworks 170 Accessing Address Book ■ properties 171 Querying the Address Book 173 Using the ■ ■ Address Book UI 175 9.3 An introduction to Core Data 179 Background information about Core Data 180 Setting up Core ■ Data in your application 180 Initializing the Core Data ■ objects 182 Adding objects to the database 185 ■ Fetching, updating, and deleting objects in Core Data 185 9.4 Summary 187 10 Positioning: accelerometers, location, and the compass 188 10.1 The accelerometers and orientation 189 The orientation property 189 The orientation notification 189 ■ 10.2 The accelerometers and movement 190 Accessing the UIAccelerometer 190 Parsing the ■ UIAcceleration 191 Checking for gravity 191 Checking for ■ ■ movement 194 Recognizing simple accelerometer ■ movement 194 10.3 The accelerometers and gestures 197 10.4 All about Core Location 199 The location classes 200 An example using location and ■ distance 201 An example using altitude 204 Using the ■ ■ compass 207 Core Location and the internet 208 ■ 10.5 Summary 209 11 Media: images and the camera 210 11.1 An introduction to images 211 Loading a UIImage 211 Drawing a UIImageView 212 ■ Modifying an image in the UIKit 213 11.2 Drawing simple images with Core Graphics 213 11.3 Accessing photos 215 Using the image picker 215 Taking photos 216 ■ Saving to the photo album 217 11.4 Collage: an image example 217 The collage view controller 218 The collage temporary image ■ view 221 The collage view 222 Further exploration of this ■ ■ example 223 11.5 Summary 223 Download from Wow! eBook
  13. xii CONTENTS 12 Media: audio and recording 224 12.1 Playing audio from the iPod library 225 Retrieving audio items from the iPod media library 225 Getting information about an MPMediaItem 227 Playing ■ media items using MPMusicPlayerController 227 Example: ■ creating a simple media player application 229 12.2 Recording audio 232 Initializing the audio recorder 232 Controlling the ■ audio recorder 233 Responding to AVAudioRecorder ■ events 234 12.3 Playing sounds manually 234 Initializing the AVAudioPlayer 235 The AVAudioPlayerDelegate 235 Controlling the ■ AVAudioPlayer 236 12.4 Example: creating a simple audio recording/ playback application 237 Creating a view-based application 238 Adding the needed ■ frameworks 238 Setting up the IBActions 238 Creating ■ ■ the interface 239 Setting up the audio recorder and ■ implementing the IBActions 239 12.5 Recording, playing, and accessing video 240 12.6 Summary 242 13 Graphics: Quartz, Core Animation, and OpenGL 243 13.1 An introduction to Quartz 2D 244 13.2 The Quartz context 244 Drawing to a UIView 246 Drawing to a bitmap 247 ■ 13.3 Drawing paths 248 Finishing a path 249 Creating reusable paths 249 ■ Drawing rectangles 251 13.4 Setting the graphical state 251 Setting colors 252 Making transformations 253 ■ Setting clipping paths 255 Other settings 256 ■ Managing the state 257 13.5 Advanced drawing in Quartz 257 Drawing gradients 257 Drawing images 259 ■ Drawing words 260 What we didn’t cover 262 ■ Download from Wow! eBook
  14. xiii CONTENTS 13.6 Drawing on a picture: an example 262 The PhotoDraw view controller 262 The photodraw view 264 ■ Expanding on the example 266 13.7 An introduction to Core Animation 266 The fundamentals of Core Animation 267 Getting started with ■ Core Animation 268 Drawing a simple implicit ■ animation 268 Drawing a simple explicit animation 269 ■ 13.8 An introduction to OpenGL 269 13.9 Summary 270 14 The web: web views and internet protocols 271 14.1 The hierarchy of the internet 271 14.2 Low-level networking 272 The CFHost class 273 14.3 Working with URLs 274 Creating an NSURL 274 Building an NSURLRequest 275 ■ Manipulating HTML data by hand 275 14.4 Using UIWebView 276 Calling up the web view 276 Managing the web view ■ delegate 278 Thumbnails: a web view example 278 ■ 14.5 Parsing XML 280 Starting up NSXMLParser 281 Acting as a delegate 281 ■ Building a sample RSS reader 282 Altitude redux: a Core ■ Location example 287 14.6 POSTing to the web 289 POSTing by hand 289 Submitting forms 290 ■ 14.7 Accessing the social web 291 Using web protocols 291 Using TouchJSON 292 ■ 14.8 Summary 294 15 Peer-to-peer connections using Game Kit 295 15.1 Overview of Game Kit 295 15.2 Creating peer-to-peer applications using the peer picker 296 Using Apple’s built-in peer picker 296 Implementing the ■ GKSessionDelegate methods 300 Sending and receiving data ■ between peers 301 Download from Wow! eBook
  15. xiv CONTENTS 15.3 Example: creating a multiplayer table tennis game 303 Starting the GKTennis project 304 Creating the header ■ file 304 Creating the table tennis interface 306 Game ■ ■ initialization 306 Setting up the peer picker and getting ■ connected 307 Implementing the send and receive ■ methods 309 The game loop 311 User interaction 314 ■ ■ 15.4 Summary 315 16 Push notification services 316 16.1 What are push notifications? 316 16.2 An overview of Apple’s push notification system 318 16.3 Preparing your application to use push notifications 319 Setting up your application certificate 319 Setting up your ■ provisioning profile 321 The code for handling push ■ notifications 321 Preparing audio files 323 ■ 16.4 Creating a push notification provider in PHP 324 Creating the SSL certificate 324 Implementing the PHP push ■ notification provider 326 16.5 Summary 327 17 The Map Kit framework 328 17.1 Overview of Map Kit 328 17.2 Adding a map to an application 329 Adding the map using Interface Builder 329 Adding the map ■ programmatically 330 Controlling the map 330 ■ 17.3 Reverse geocoding 332 17.4 Annotating the map 334 Adding basic map annotations 335 Adding custom map ■ annotations 336 17.5 Summary 341 18 In-app purchasing using Store Kit 342 18.1 Setting up a sandbox testing environment 343 Creating an iTunes test user 343 Adding products 344 ■ 18.2 Creating a simple store interface 347 18.3 Summary 355 Download from Wow! eBook
  16. xv CONTENTS 19 iPhone SDK enhancements 357 19.1 Custom keyboard accessories 358 Extending UITextField 358 Implementing the custom ■ UITextField 360 Custom keyboards 361 ■ 19.2 Going forward: the iPhone 4.0 SDK 362 Multitasking 363 User experience enhancements 364 ■ Multimedia framework enhancements 365 Game Center 365 ■ 19.3 Summary 365 appendix A iPhone OS class reference 367 appendix B External sources and references 371 appendix C Publishing your SDK program 373 appendix D Updating current applications for the iPad 376 index 383 Download from Wow! eBook
  17. Download from Wow! eBook
  18. preface When it came time to write a revised edition of iPhone in Action, Shannon and Christo- pher had their hands tied with other projects. This is when Manning decided to seek out another author to perform the necessary updates. They eventually stumbled on me on Twitter. I had started programming when I was a junior in high school and spent most of my time writing web applications and silly Java games. After high school, I attended the University of New Mexico and acquired a Bachelor of Science degree in Com - puter Science. When Apple launched the original iPhone, I drooled over the device and longed to own one. It wasn’t until Apple came out with the iPhone 3G that I could actually afford it. Knowing that I wanted to write software for the iPhone, I began learning to program for it and started a popular development blog called iCodeBlog.com before I even had a device in hand. Because I was used to reading Javadocs in college, I was able to learn a lot from the Apple API documentation. When I began working on this book, I quickly realized that quite a few of the chap- ters from the first edition had to go. Although the web applications were a crucial part of the iPhone apps’ startup, they can no longer compete with native apps. So I removed the web chapters from the original text and added new ones based on the newest iPhone SDK features. Just as the book was completed, Apple made another big announcement: the com- pany revealed its newest device, the iPad. Manning and I had a decision to make: either spend another couple of months on the book, or release a book that would become a dinosaur the moment it hit the shelves. Fortunately, we chose the former option. BRANDON TREBITOWSKI xvii Download from Wow! eBook
  19. acknowledgments Any technical book is a massive undertaking, due to the number of people required to make sure that it reads well, looks good, and is technically correct. Thus, we have to thank the entire Manning staff, without whom this book would not exist. They did more than just correct our errors and polish our words; they also helped us make inte- gral decisions about the organization and the contents of the book—decisions that we believe improved it dramatically. In particular, we’d like to thank the three people at Manning who helped us at the most pivotal times: Troy Mott, our acquisitions editor, who initially agreed to take on the book and who stayed with us every step of the way; Katharine Osborne, our devel- opment editor, who put in an incredible amount of work to ensure the quality of writ- ing and offered tons of guidance and support along the way; and Marjan Bace, our publisher, who offered some of the biggest challenges regarding our content and organization and initiated some of the best improvements. We’d also like to thank Tiffany Taylor, our copyeditor, and Katie Tennant, our proofeader. Beyond that, tech editors are crucial to the success of a book like this, so we want to thank Kalle Alm and Matt Wyman who did the technical proofread of the manuscript a number of times as it was being revised and updated; they caught minu- tia that we weren’t even aware of. Though it’s clichéd to say, it’s true: any errors that sneaked in are ours, but many others were corrected by all of the people we men - tion—and many more who worked behind the scenes at Manning. They were crucial to the book, and we’d like to thank them all. Finally, we’d like to thank the reviewers who generously agreed to read our manu- script as we worked on it; they improved the book immensely: Ted Neward, Jason xviii Download from Wow! eBook
  20. xix ACKNOWLEDGMENTS Jung, Glenn Stokol, Gershon Kagan, Cos DiFazio, Clint Tredway, Christopher Haupt, Berndt Hamboeck, Rob Allen, Peter Scott, Lester Lobo, Frank Jania, Curtis Miller, Chuck Hudson, Carlton Gibson, Amos Bannister, Emeka Okereke, Pratik Patel, Kunal Mittal, Tyson Maxwell, TVS Murthy, Kevin Butler, David Hanson, Timothy Binkley- Jones, Carlo Bottiglieri, and Barry Ezell. BRANDON would also like to thank his wife, Ashley, for putting up with the long nights he spent working on this book. Without her love and support, he would not have been able to complete a single chapter. He would also like to thank Matt Woodward, who introduced him to Troy Mott and provided him with the opportunity to work on this book. CHRISTOPHER would like to thank Chris Messina for inviting him to be a founder of iPhoneDevCamp and to also thank his long-time MacHack and SmartFriends col- leagues for their support and assistance. SHANNON would like to thank Christopher, who got this book started in the first place, and Brandon for taking on the revised edition. Download from Wow! eBook
ADSENSE

CÓ THỂ BẠN MUỐN DOWNLOAD

 

Đồng bộ tài khoản
2=>2