Tải bản đầy đủ (.pdf) (602 trang)

Tài liệu Professional Windows Phone 7 Game Development: Creating Games using XNA Game Studio docx

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (33.01 MB, 602 trang )

Download from Wow! eBook <www.wowebook.com>
PROFESSIONAL
IPHONE AND IPAD APPLICATION DEVELOPMENT
INTRODUCTION xxiii
CHAPTER 1 Navigation 1
CHAPTER 2 Alerts, Action Sheets, and Modal Views . . . . . . . . . . . . . . . . . . . . . . . . . . 53
CHAPTER 3 Custom Table Views . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 2 1
CHAPTER 4 The Split View 1 37
CHAPTER 5 Touch Events 1 59
CHAPTER 6 Notification Processing 1 99
CHAPTER 7 Networking Concepts 227
CHAPTER 8 Multimedia 263
CHAPTER 9 Application Preferences 313
CHAPTER 10 Data Storage 339
CHAPTER 11 The Pasteboard . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 395
CHAPTER 12 Unit Testing 429
CHAPTER 13 Performance Tuning and Optimization 453
CHAPTER 14 Integrating iAds . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 469
CHAPTER 15 Multitasking 48 1
APPENDIX A Your Initial App — First Steps 505
APPENDIX B iPhone Developer Center 51 5
APPENDIX C Cocoa Touch Static Libraries 52 1
APPENDIX D Apple Developer Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 543
INDEX 547
Download from Wow! eBook <www.wowebook.com>
Download from Wow! eBook <www.wowebook.com>
PROFESSIONAL
iPhone
®
and iPad



Application
Development
Download from Wow! eBook <www.wowebook.com>
PROFESSIONAL
iPhone
®
and iPad

Application
Development
Gene Backlin
Download from Wow! eBook <www.wowebook.com>
Professional iPhone® and iPad™ Application Development
Published by
Wiley Publishing, Inc.
10475 Crosspoint Boulevard
Indianapolis, IN 46256
www.wiley.com
Copyright © 2011 by Wiley Publishing, Inc., Indianapolis, Indiana
Published simultaneously in Canada
ISBN: 978-0-470-87819-4
ISBN: 978-1-118-00834-8 (ebk)
ISBN: 978-1-118-00835-5 (ebk)
ISBN: 978-1-118-00706-8 (ebk)
Manufactured in the United States of America
10 9 8 7 6 5 4 3 2 1
No part of this publication may be reproduced, stored in a retrieval system or transmitted in any form or by any means,
electronic, mechanical, photocopying, recording, scanning or otherwise, except as permitted under Sections 107 or 108 of
the 1976 United States Copyright Act, without either the prior written permission of the Publisher, or authorization through

payment of the appropriate per-copy fee to the Copyright Clearance Center, 222 Rosewood Drive, Danvers, MA 01923,
(978) 750-8400, fax (978) 646-8600. Requests to the Publisher for permission should be addressed to the Permissions
Department, John Wiley & Sons, Inc., 111 River Street, Hoboken, NJ 07030, (201) 748-6011, fax (201) 748-6008, or
online at />Limit of Liability/Disclaimer of Warranty: The publisher and the author make no representations or warranties with
respect to the accuracy or completeness of the contents of this work and specifically disclaim all warranties, including
without limitation warranties of fitness for a particular purpose. No warranty may be created or extended by sales or pro-
motional materials. The advice and strategies contained herein may not be suitable for every situation. This work is sold
with the understanding that the publisher is not engaged in rendering legal, accounting, or other professional services.
If professional assistance is required, the services of a competent professional person should be sought. Neither the pub-
lisher nor the author shall be liable for damages arising herefrom. The fact that an organization or Web site is referred to
in this work as a citation and/or a potential source of further information does not mean that the author or the publisher
endorses the information the organization or Web site may provide or recommendations it may make. Further, readers
should be aware that Internet Web sites listed in this work may have changed or disappeared between when this work was
written and when it is read.
For general information on our other products and services please contact our Customer Care Department within the
United States at (877) 762-2974, outside the United States at (317) 572-3993 or fax (317) 572-4002.
Wiley also publishes its books in a variety of electronic formats. Some content that appears in print may not be available
in electronic books.
Library of Congress Control Number: 2010934751
Trademarks: Wiley, the Wiley logo, Wrox, the Wrox logo, Wrox Programmer to Programmer, and related trade dress are
trademarks or registered trademarks of John Wiley & Sons, Inc. and/or its affiliates, in the United States and other coun-
tries, and may not be used without written permission. iPhone and iPad are trademarks or registered trademarks of Apple,
Inc. All other trademarks are the property of their respective owners. Wiley Publishing, Inc., is not associated with any
product or vendor mentioned in this book.
Download from Wow! eBook <www.wowebook.com>
This book is being dedicated to the
family unit; past, present, and future.
May it never lose sight of its existence.
Download from Wow! eBook <www.wowebook.com>
Download from Wow! eBook <www.wowebook.com>

ABOUT THE AUTHOR
GENE BACKLIN is owner and principal consultant of MariZack Consulting, formed
in 1991 with one purpose — to help. He has been helping clients for over 30 years,
including IBM, McDonnell Douglas, Waste Management, U.S. Environmental
Protection Agency, Nations Bank, Bank of America, and Bank One to name a
few. He is also a faculty member of DePaul University’s College of Computing and
Digital Media.
His childhood interest in electronics helped him break into the computer industry.
He still has the Heathkit H-8 digital computer and H-9 video terminal that he built
in 1978. He taught himself programming using Extended Benton Harbor Basic. After
the IBM-PC was introduced, Gene built the Heathkit H-151 PC-compatible computer, which is still running
today. If you ask him about the information revolution, his response would be “Fascinating.” He has devel-
oped on computers that loaded programs from paper tape, to the revolutionary NeXT computer (he still has
two), to the iPhone and iPad. Gene feels very fortunate to have not only seen an industry evolve, but also to
have been an active participant in it.
He is thrilled to have penned a companion text to his Developing NeXTSTEP Applications, which he wrote
back in 1995.
Download from Wow! eBook <www.wowebook.com>
ABOUT THE TECHNICAL EDITOR
DANIEL W. MEEKS has been grep, awk and sed(ing) on Unix systems since Version 9 from Bell Labs and 2.8
BSD. He was at Bell Labs in Naperville during the Cfront Version 1 days of C++. Since then he has been
managing technology teams developing in C, Objective-C, C++, Java, and C# in various financial institu-
tions and traveling the world in support of them. Today he spends his weekends putting up infrastructure
and develops mobile applications on Apple, Windows and Android as a hobby.
Download from Wow! eBook <www.wowebook.com>
CREDITS
EXECUTIVE EDITOR
Carol Long
SENIOR PROJECT EDITOR
Ami Frank Sullivan

TECHNICAL EDITOR
Daniel Meeks
PRODUCTION EDITOR
Daniel Scribner
COPY EDITOR
Luann Rou
EDITORIAL DIRECTOR
Robyn B. Siesky
EDITORIAL MANAGER
Mary Beth Wakefield
FREELANCER EDITORIAL MANAGER
Rosemarie Graham
ASSOCIATE DIRECTOR OF MARKETING
Ashley Zurcher
PRODUCTION MANAGER
Tim Tate
VICE PRESIDENT AND EXECUTIVE GROUP
PUBLISHER
Richard Swadley
VICE PRESIDENT AND EXECUTIVE PUBLISHER
Barry Pruett
ASSOCIATE PUBLISHER
Jim Minatel
PROJECT COORDINATOR, COVER
Lynsey Stanford
COMPOSITOR
Je Lytle,
Happenstance Type-O-Rama
PROOFREADER
Nancy Bell

INDEXER
Ron Strauss
COVER DESIGNER
Michael E. Trent
COVER IMAGE
© Hedda Gjerpen
Download from Wow! eBook <www.wowebook.com>
Download from Wow! eBook <www.wowebook.com>
ACKNOWLEDGMENTS
I WOULD FIRST LIKE TO THANK my wife Roseann, my son Zachary, and my daughter Marissa for putting
up with me while I was writing this book; and my son Ethan for keeping me young (remember what you are
to do if this idea ever comes up again).
I would also like to thank my mother Mary Louise and daughter Hannah Angel, for giving me the gift of
themselves and their spirit, which I carry with me every day of my life. I miss you.
To my father, who is there when I need him, and also has a really great name… thank you Gene!
To Helen and Jerry, you will never know how much you mean to me.
To Dan, I am glad we were able to share time together again after so many years.
This book is more than just words and code. It is time and people. The finished product you hold in your
hand is a snapshot of events that somehow managed to come together with the very hard work of a lot of
people; Carol, Ami, and Luann, I want to specifically thank you three for everything that you did.
Finally I would like to once again thank Jean-Marc Krikorian for really doing nothing as he did the last
time back in ’95.
I cannot forget you, the reader, for taking time out of your day to spend it with me. This book was written
for you, and I hope I have helped you in some way. If you have any questions or comments, please let me
know, thank you again!
Download from Wow! eBook <www.wowebook.com>
Download from Wow! eBook <www.wowebook.com>
CONTENTS
IntroductIon xxiii
NAVIGATION CHAPTER 1: 1

Navigation Stack 2
The Navigation Bar 2
UINavigationBarDelegate Protocol 2
Configuring Navigation Bars 2
Pushing and Popping Items 3
A Simple Navigation Bar 3
Development Steps: A Simple Navigation Bar 4
Test Your Application 17
The Toolbar 17
A Simple Toolbar 18
Development Steps: A Simple Toolbar 18
Test Your Application 26
The Tab Bar 26
UITabBarDelegate Protocol 26
Customizing Tab Bars 26
A Simple Tab Bar 26
Development Steps: A Simple Tab Bar 27
Designing the View Controllers 30
Test Your Application 51
Summary 51
ALERTS, ACTION SHEETS, AND MODAL VIEWS 5CHAPTER 2: 3
Alerts 53
Alert View Design 54
The UIAlertViewDelegate Protocol 54
A Simple AlertView for the iPhone-iPod Touch and iPad 54
Development Steps: Loading a File into a UITextView 55
Test Your Application 66
Action Sheets 66
Action Sheet Design 66
The UIActionSheetDelegate Protocol 67

Download from Wow! eBook <www.wowebook.com>
xvi
CONTENTS
An Action Sheet for the iPhone-iPod Touch 67
Development Steps: Creating an Action Sheet for the
iPhone or iPod Touch 68
Test Your Application 80
An Action Sheet for the iPad 80
Development Steps: Creating an Action Sheet for the iPad 81
Final Steps: Making the Connections 93
Test Your Application 95
Modal Views 95
Presenting and Dismissing the Modal View 95
Transition Styles 95
Modal Presentation Styles (iPad only) 95
A Modal View for the iPhone and iPod Touch 95
Development Steps: Creating a Modal View for the
iPhone and iPod Touch 96
Test Your Application 105
A Modal View for the iPad 106
Development Steps: Creating a Modal View for the iPad 108
Test Your Application 119
Summary 119
CUSTOM TABLE VIEWS 12CHAPTER 3: 1
Table Views 121
The Table View Cell 122
The UITableViewDataSource Protocol 122
The UITableViewDelegate Protocol 122
A Custom Table View Application 122
Development Steps: A Custom Table View Application 122

Creating the Contacts.plist Property List File 133
Test Your Application 135
Summary 135
THE SPLIT VIEW 13CHAPTER 4: 7
The UISplitViewController Class 137
UIPopoverControllerDelegate Protocol 138
UISplitViewControllerDelegate Protocol 138
A Simple Split View Application 138
Development Steps: A Simple Split View Application for the iPad 141
Creating the DataSource.plist Property List File 145
Test Your Application 158
Summary 158
Download from Wow! eBook <www.wowebook.com>
xvii
CONTENTS
TOUCH EVENTS 15CHAPTER 5: 9
Touch Event Handling 159
Single Touch 160
Multi-Touch 160
Taps 160
Swipes and Gestures 161
A Simple Touch Handler 162
Development Steps: A Simple Touch Handler 163
Test Your Application 174
A Simple Gesture Recognizer 174
Development Steps: A Simple Gesture Recognizer 175
Test Your Application 197
Summary 197
NOTIFICATION PROCESSING 19CHAPTER 6: 9
NSNotifications Concepts 200

The Notification Center 200
Registering for Local Notifications 200
Posting Local Notifications 200
Unregistering an Observer 201
A Local Named Notification 201
Development Steps: A Local Named Notification 202
Test Your Application 211
A Local Keyboard Notification 211
Development Steps: A Local Keyboard Notification 212
Test Your Application 225
Summary 225
NETWORKING CONCEPTS 22CHAPTER 7: 7
Communication over a Network 228
A Simple Network Browser 230
Development Steps: A Simple Network Browser 230
Peer-to-Peer Device Communications 244
Development Steps: Peer-to-Peer Device Communication 245
Test Your Application 262
Summary 262
Download from Wow! eBook <www.wowebook.com>
xviii
CONTENTS
MULTIMEDIA 26CHAPTER 8: 3
Frameworks for Audio 263
Media Player 263
AV Foundation 264
Audio Toolbox 264
Audio Unit 264
OpenAL 264
Frameworks for Video 265

MPMoviePlayerController 265
Supported Formats 265
Playing Audio from the iPod Library 265
Development Steps: Playing Audio from the iPod Library 266
Test Your Application 291
An Application That Plays Video from the iPod Library 291
Development Steps: An Application That Plays Video from the iPod Library 292
Test Your Application 312
Summary 312
APPLICATION PREFERENCES 31CHAPTER 9: 3
Application Configuration 313
Guidelines for Application Preferences 314
Preference Element Types 314
Implementing Preference Hierarchies 315
Accessing the Application’s Preferences 315
Setting Simple Preferences 315
Development Steps: Setting Simple Preferences 315
Source Code Listings for Setting Simple Preferences 320
Test Your Application 325
Creating a Child Pane Preference Hierarchy 325
Development Steps: Creating a Child Pane Preference Hierarchy 326
Test Your Application 338
Summary 338
DATA STORAGE 33CHAPTER 10: 9
Property Lists 339
Uses for Property Lists 340
Suggested Data Element Types 340
Saving and Restoring a Property List 340
Core Data 341
The Core Data Stack 341

Managed Objects 341
Managed Object Context 341
Download from Wow! eBook <www.wowebook.com>
xix
CONTENTS
The Managed Object Model 342
Persistent Store Coordinator 342
Xcode Modeling Tool 342
Fetching Managed Objects 343
Deleting Managed Objects 343
The Common Premise for Data Storage 343
Development Steps: A Simple Application Using Property Lists 344
Test Your Application 367
Development Steps: A Simple Application Using Core Data 367
Test Your Application 394
Summary 394
THE PASTEBOARD 39CHAPTER 11: 5
Pasteboard Concepts 396
Named Pasteboards 396
Persistence 396
The Editing Menu 397
Cutting the Selection 397
Pasting the Item 398
Dismissing the Editing Menu 398
Cutting and Pasting Text 398
Development Steps: Cutting and Pasting Text 398
Test Your Application 407
Cutting and Pasting Images 407
Development Steps: Cutting and Pasting Images 409
Test Your Application 416

Creating Custom Menus for the iPad 416
Development Steps: Creating Custom Menus for the iPad 418
Test Your Application 427
Summary 427
UNIT TESTING 42CHAPTER 12: 9
Setting Up the Environment 429
Using an Application-Testing Target 429
A Simple Unit Test 430
Development Steps: A Simple Unit Test 431
Creating the Data.plist Data Source 442
Test Your Application 445
Unit Test Creation Steps: A Simple Unit Test 446
Unit Testing Your Application 450
Registering your Device 451
Unit Testing Your Application 452
Summary 452
Download from Wow! eBook <www.wowebook.com>
xx
CONTENTS
PERFORMANCE TUNING AND OPTIMIZATION 45CHAPTER 13: 3
Profiling 454
Using the Simulator 454
Using the Device 454
Benefits of Profiling 454
A Simple Memory Leak Test 455
Development Steps: A Memory Leak Test 459
Development Steps Continued: Using the Instruments Application 467
Development Steps Continued: Using Build and Analyze
from the Main Menu 467
Summary 468

INTEGRATING IADS 46CHAPTER 14: 9
Joining the iAd Network 469
Setting Up Banking Information 470
Enabling Your Application for iAds 470
Configuring Your iAd Preferences 470
Preparing Your Application to Use the iAd Network 470
Implementing the ADBannerView 470
Integrating the iAd Framework 470
A Simple Application Using iAds 471
Development Steps: A Simple Application Using iAds 472
Test Your Application 479
Summary 479
MULTITASKING 48CHAPTER 15: 1
Multitasking Services 481
UIApplication Delegate Messages 482
Multitasking Responsibilities 482
Xcode Simulator Multitasking Limitations 483
Device Support of Multitasking 483
An Application That Multitasks Audio 483
Development Steps: An Application That Multitasks Audio 484
Test Your Application 503
Summary 503
APPENDIX A: YOUR INITIAL APP  FIRST STEPS 505
Xcode Project Builder 505
Available Application Types 506
The Project Window 508
Build and Run in the Simulator 512
Download from Wow! eBook <www.wowebook.com>
xxi
CONTENTS

Interface Builder 512
Creating an Interface Builder Document 513
The Document Window 513
The Library Window 513
The Inspector Window 514
The Connections Panel 514
Summary 514
APPENDIX B: IPHONE DEVELOPER CENTER 515
Resources for the iOS 4 SDK 515
Downloads 516
iOS 4 Reference Library 516
Sample Code 517
iPhone Developer Program 518
iPhone Provisioning Portal 518
Apple Developer Forums 518
Developer Support Center 519
App Store Resource Center 519
iTunes Connect 520
News & Announcements 520
RSS Feed Subscription 520
Summary 520
APPENDIX C: COCOA TOUCH STATIC LIBRARIES 521
Xcode Project Template 521
Choose Cocoa Static Library 522
Adding Classes to the Library 522
Source Code Listings for the DataSource Static Library 523
Building the Project 527
An Xcode Application Project 527
Creating Your New Application 528
Adding the Library to the New Project 528

Adding Library Headers to the App Project 530
Designing the App User Interface 530
Adding Source Code Using the Library Classes 535
Test Your Application 542
Summary 542
Download from Wow! eBook <www.wowebook.com>
xxii
CONTENTS
APPENDIX D: APPLE DEVELOPER RESOURCES 543
iPhone Developer Program 543
Developer Centers 543
iOS Resources 544
iOS Developer Resources 544
iOS Developer Guides 544
iTunes App Store 544
Application Distribution Procedures 545
iTunes Connect Developer Guide 545
IndEX 547
Download from Wow! eBook <www.wowebook.com>
INTRODUCTION
IN 1975 I PURCHASED a Texas Instruments SR-56 pocket calculator. What compelled me to purchase it was
the word programmable. I didn’t quite know what that meant, but I knew I had to get it. The SR-56 had 10
memories and 100 program steps. After about a week tinkering with it, I started to understand the power of
a program. I still have that calculator today, and while it no longer works, it is a reminder of how fascinat-
ing it is to write a program.
Over the decades, I worked with many technologies and languages, and over time, the development process
was becoming routine. The excitement that I originally felt with my SR-56 was beginning to fade. That is,
until 1989, and one word: NeXT. The excitement had returned. NeXT gave the developer a rich set of tools
to produce sophisticated applications within amazing time frames and with relative ease. Developing soft-
ware just made sense now.

When this environment was introduced to the Mac platform, it was the beginning of a great relationship.
Today, there is the iPhone, iPod touch, and iPad. For me, 35 years from the day I wrote my first program on
my SR-56, I am still like a kid in a candy store. I wrote this book to give you a deeper insight into how you
can use these tools to realize your ideas on these devices.
WHO THIS BOOK IS FOR
This book was written for the developer who is familiar with the Xcode environment and Objective-C lan-
guage in general.
For the reader who is familiar with developing applications but just not up to speed on Xcode and Objective C,
please visit the Apple iPhone Dev Center at
/>Each chapter discusses a specific topic or feature of the iPhone/iPad device. Following the discussion, you
will find the steps necessary to create a full working example of that feature. The source code that you will
develop is structured in a modular fashion, enabling you to extract it and implement the functionality in
your own applications.
WHAT THIS BOOK COVERS
The topics covered in this book use the current SDK 4.0 for iPhone and iPod Touch, and the SDK 3.2 for the
iPad. Xcode and Interface Builder are the main tools used to create all the applications presented. Finally,
with the Instruments application, performance issues are discussed.
HOW THIS BOOK IS STRUCTURED
Beginning with Chapter 1, the discussion will focus on the application frameworks included in the SDK,
and through a step-by-step process, you will design applications that help you understand these frameworks,
including when and where they are to be incorporated into your application. In this book, you will learn by
doing, and when you’ve completed all the Development Steps sections, you will have experience creating and
adding functionality to iPhone and iPad applications using iOS 4. Topics include table views, image views,
pickers, data storage, audio and video to name a few. Finally, the book discusses procedures that will ensure
your applications perform efficiently, allowing for a desirable user experience from those who purchase your
applications from the Apple iTunes App Store.
Download from Wow! eBook <www.wowebook.com>

×