iOS SDK Programming: A Beginner’s Guide - Pdf 12

www.it-ebooks.info
iOS SDK
Programming:
A Beginner’s Guide
www.it-ebooks.info
About the Authors
James A. Brannan is a senior developer with more than
15 years of experience. He has developed using everything
from AWK, to Visual Basic, to Java. His current interests
are iOS, Blackberry, Android, and Adobe Flex/Flash. He is
only $999,000 short of being the next app store overnight
millionaire. He lives in Gaithersburg, Maryland, with his wife,
two kids, two Macs, and bicycle.
Blake Ward has a PhD in Computer Science from Carnegie
Mellon University and has spent more than 30 years
programming and managing software development. He has
developed for a wide variety of mobile devices, ranging from the
Apple Newton and Palm Pilot to RIM’s Blackberry, the iPhone
and iPad, and Android phones. Blake has worked as a researcher
and in management at Apple, Xerox PARC, and numerous
startups. He is currently an independent iPhone and Android
developer, available through www.iphoneappquotes.com.
About the Technical Editor
Born to golf, forced to work, Steven Weber, a Java Web
Applications engineer, has ten years application development
under his belt. He’s dabbled in iOS application development
and released one corporate application since the launch of
Apple’s App Store. He’s currently living it up in the Colorado
Rockies.
www.it-ebooks.info
iOS SDK

terms.
THE WORK IS PROVIDED “AS IS.” McGRAW-HILL AND ITS LICENSORS MAKE NO GUARANTEES OR WARRANTIES AS TO THE
ACCURACY, ADEQUACY OR COMPLETENESS OF OR RESULTS TO BE OBTAINED FROM USING THE WORK, INCLUDING ANY
INFORMATION THAT CAN BE ACCESSED THROUGH THE WORK VIA HYPERLINK OR OTHERWISE, AND EXPRESSLY DISCLAIM
ANY WARRANTY, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO IMPLIED WARRANTIES OF MERCHANTABILITY
OR FITNESS FOR A PARTICULAR PURPOSE. McGraw-Hill and its licensors do not warrant or guarantee that the functions contained in the
work will meet your requirements or that its operation will be uninterrupted or error free. Neither McGraw-Hill nor its licensors shall be liable to
you or anyone else for any inaccuracy, error or omission, regardless of cause, in the work or for any damages resulting therefrom. McGraw-Hill
has no responsibility for the content of any information accessed through the work. Under no circumstances shall McGraw-Hill and/or its licensors
be liable for any indirect, incidental, special, punitive, consequential or similar damages that result from the use of or inability to use the work,
even if any of them has been advised of the possibility of such damages. This limitation of liability shall apply to any claim or cause whatsoever
whether such claim or cause arises in contract, tort or otherwise.
www.it-ebooks.info
For Timothy Hill and Doctor Ronald Holt. This book is not some lofty political or
anthropological treatise, but it was fun writing.
—James
For Bryce—the real author in our family.
—Blake
www.it-ebooks.info
This page intentionally left blank
www.it-ebooks.info
vii
Contents at a Glance
1 The iOS Software Development Kit (SDK) 1
2 A C Refresher 25
3 Just Enough Objective-C: Part One 41
4 Just Enough Objective-C: Part Two 61
5 Deploying to an iPhone, Debugging, and Testing 77
6 UIApplication and UIApplicationDelegate 109
7 UIView and UIViewController 127

Memory and Processor Speed 8
Small Screen 8
Security 9
Short-Lived Applications 9
Manual Memory Management 9
Relevant Documentation 9
Try This: Getting a Quick Start on iOS Development 10
Summary 23
2
A C Refresher 25
C Command-Line Programs 27
Try This: Creating a Simple C Program Using Xcode 27
www.it-ebooks.info
x iOS SDK Programming: A Beginner’s Guide
C Comments 28
Understanding Headers, Import, and Include 29
Try This: Creating a Header File 29
Preprocessor Statements 31
Try This: Using Preprocessor Statements 31
Data Types and Operators 32
Control, Functions, and Conditional Statements 33
Arrays and Structures 35
Functions 35
The printf Statement 36
Pointers 36
Try This: Using Pointers 37
Dereferencing a Pointer 38
Pointers and Arrays 39
Try This: Using an Array with Pointers 39
Summary 40

Contents xi
Multiple-Argument Messages 67
Try This: Creating a Simple Multiple-Argument Message 67
Understanding the id Variable Type, Dynamic Typing, and Dynamic Binding 69
The id Type 69
Dynamic Binding and Dynamic Typing 70
Understanding Inheritance 70
Overriding Methods 70
Overloading Methods 71
Using Categories 72
Using Protocols 72
Handling Exceptions 74
Summary 75
5
Deploying to an iPhone, Debugging, and Testing 77
Installing Applications on an iPhone 79
Membership 79
Certificates, Devices, Application IDs, and Provisioning 80
Try This: Deploying an Application to iPhone 83
Debugging 88
Using the Debugger 89
Try This: Debugging an Application 93
NSZombieEnabled 97
Try This: Enabling Zombies 98
Instruments—Leaks 101
Try This: Find a Memory Leak 102
Distributing Your Application 104
Ad Hoc Deployment and Testing 104
Distributing Your App via the App Store 107
Summary 108

Try This: Allowing Users to Customize a Tab Bar 151
Summary 156
9
UINavigationBar and UINavigationController 159
UINavigationBar, UINavigationController, and UINavigationItem 162
Try This: Building a Three-View Application Using a Navigation Bar 163
Adding Another View 172
Try This: See How a Utility Application Uses NavigationBar 176
More on the UINavigationController 177
Popping View Controllers 177
Configuring the Navigation Bar 177
Try This: Using a Navigation Controller in a Tab 178
Summary 183
10
Tables Using UITableView and UITableViewController 185
UITableView 189
UITableViewDelegate and UITableViewDataSource 189
UITableViewDelegate 189
UITableViewDataSource 189
Try This: Adopting the UITableViewDelegate and UITableViewDataSource 191
Try This: Adding a Delegate and Data Source 194
UITableViewController 198
Try This: Using a UITableViewController 198
Grouping and Indexing 201
Grouped Table Style 202
Try This: Grouping 202
Indexing 205
Try This: Indexing 205
Images in Tables 207
Try This: Adding an Image 207

Try This: Adding an Application Badge 260
Summary 261
12
Controls—Part One: Using Buttons, Sliders, Switches,
and Text Fields 263
Buttons 265
UIButton with a Background Image and Image 265
Try This: Using a Custom Button Background Image and Image 266
Button Types 270
UIToolBar 271
Try This: Creating a UIToolbar 273
UISwitch 276
UISlider 276
Appearance 277
Values 277
Continuous Property 277
Try This: Using a Switch and a Slider 277
UITextField 280
Try This: Using UITextField (with a Number Pad) 282
UITextView 286
UISegmentedControl 287
www.it-ebooks.info
xiv iOS SDK Programming: A Beginner’s Guide
Try This: Using a UISegmentedControl 287
The Web View 289
UIWebView 290
UIWebViewDelegate 290
Try This: Creating a Simple Web Browser 291
Summary 294
13

Reading Settings Using NSUserDefaults 346
Try This: Reading the Settings Bundle 347
Changed Settings While Suspended 348
Summary 348
15
Property Lists and Archiving 349
An iOS Application’s Directory Structure 350
Directories 350
www.it-ebooks.info
Contents xv
Property Lists 351
Simple Serialization 352
Try This: Preserving an NSArray 352
NSPropertyListSerialization 354
Try This: Preserving to an XML Property List 355
Archiving 358
Protocols to Adopt 358
NSKeyedArchiver and NSKeyedUnarchiver 359
Try This: Archiving and Unarchiving an Object 360
Try This: Archiving and Unarchiving an Object Hierarchy 364
Multitasking and Saving Application State 368
Summary 369
16
Data Persistence Using SQLite 371
Adding a SQLite Database 372
Try This: Creating a Simple Database Using FireFox SQLite Manager 372
Basic SQLite Database Manipulation 376
Opening the Database 376
Statements, Preparing Statements, and Executing Statements 377
Select 378

Updating Entities 416
Try This: Adding Navigation and AKCGroup Editing 417
Navigation 424
Try This: Adding Navigation and Editing for a List of Breeds 424
Try This: Adding a Breed Detail View 428
Distributing Core Data with Your App 430
What Next? 432
Summary 433
18
Multimedia 435
Playing Sounds 436
AudioServicesPlaySystemSound 437
AVAudioPlayer and AVAudioPlayerDelegate 438
Try This: Playing a Sound and an MP3 439
Media Player Framework 442
Media Data Classes 442
Selecting Multimedia 443
Playing Multimedia: MPMusicPlayerController 444
Try This: Using the Media Picker and Media Player 445
MPMoviePlayerController 451
Try This: Play a Video 452
Summary 455
19
Universal Applications for the iPad 457
Creating a Universal Application 460
Try This: Building an App for iPad and iPhone 460
Handling Orientation Changes 463
Try This: Reacting to Orientation Changes 463
Icons and Default Screens 464
Split Views 465

their creations sold on the App Store. And we aren’t necessarily talking about high-minded
technical innovations. The media has reported that apps that make your iPhone pass gas have
made folks hundreds of thousands of dollars. Rival farting App developers have even gone so
far as to sue one another over the App Store’s precious revenue. The iOS family of devices
and the App Store are here to stay—and there’s still plenty of opportunity for you to create
the next great app.
As proof of this popularity, after posting a few tutorial videos on Vimeo, James heard
from people from Asia, Europe, and South America about those videos. So, when we decided
to write this book, we kept in mind that iOS devices have significant international appeal.
We have tried to make this book as accommodating as possible for as wide an audience as
possible. We have kept colloquialisms to a minimum, for instance. But more important than
avoiding colloquialisms, this book relies upon discrete, numbered steps that illustrate each
major concept. Rather than a lot of prose describing the iOS SDK, we show you the SDK in
action.
The Book’s Focus
This book has three goals. The first of these is to get you comfortable with using the iOS’s user
interface controls in Interface Builder. Interface Builder is a useful tool that removes much
of the complexity of creating and laying out the user interface for iOS apps. Once you master
www.it-ebooks.info
xx iOS SDK Programming: A Beginner’s Guide
this tool, building a graphical user interface (GUI) using Interface Builder is quicker and more
intuitive than using code.
The second goal of this book is to brush up your C language programming skills and
introduce you to Objective-C. Most likely you haven’t used C since college, and chances are
good you have never used Objective-C. However, because Objective-C is the language used
for Cocoa and Cocoa Touch programming, you must understand Objective-C if you wish
to create iPhone and iPad apps. After refreshing your memory on C, this book moves on to
Objective-C with a two-chapter tutorial that will give you a foundation for getting started with
the iOS SDK.
The third goal of this book is to cover all of the most useful functionality of the iOS SDK

Introduction xxi
Finally, Chapter 19 discusses the new SDK functionality available for the iPad and shows
you how to create a universal application that will run on the iPhone or iPod touch but also
take full advantage of the larger display on the iPad when available. All of the framework
functionality described in the earlier chapters applies to the iPad and the new iPhone 4, so
this chapter focuses on how to layer new iOS 4 functionality on top of the skills that you’ve
already learned.
This book doesn’t require any prior knowledge of C, Objective-C, or Cocoa Touch and
the iOS frameworks, so provided you have some prior programming experience and you
work through all of the exercises in the book, you should be ready to start working on your
own iPhone applications when you’ve finished the book. However, even if you eventually
decide to hire an independent developer to help build your application (via a web site like
www.iphoneappquotes.com), everything you’ve learned working through the exercises will
be invaluable when it comes time to turn your ideas into a design and work with others to
implement them.
www.it-ebooks.info
This page intentionally left blank
www.it-ebooks.info
1
Chapter 1
The iOS Software
Development Kit (SDK)
www.it-ebooks.info
2 iOS SDK Programming: A Beginner’s Guide
Key Skills & Concepts
L Understanding the App Store
L Understanding how to obtain Xcode and the iOS SDK
L Deciding if this book is right for you
L Understanding Xcode’s help and Apple’s online documentation
S


Nhờ tải bản gốc

Tài liệu, ebook tham khảo khác

Music ♫

Copyright: Tài liệu đại học © DMCA.com Protection Status