Thông tin tài liệu
www.it-ebooks.info
www.it-ebooks.info
Learning iPhone Programming
www.it-ebooks.info
www.it-ebooks.info
Learning iPhone Programming
Alasdair Allan
Beijing
•
Cambridge
•
Farnham
•
Köln
•
Sebastopol
•
Taipei
•
Tokyo
www.it-ebooks.info
Learning iPhone Programming
by Alasdair Allan
Copyright © 2010 Alasdair Allan. All rights reserved.
Printed in the United States of America.
Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472.
O’Reilly
books may be purchased for educational, business, or sales promotional use. Online editions
are also available for most titles (http://my.safaribooksonline.com). For more information, contact our
corporate/institutional sales department: 800-998-9938 or corporate@oreilly.com.
Editor: Brian Jepson
Production Editor: Sarah Schneider
Copyeditor: Audrey Doyle
Proofreader: Kiel Van Horn
Indexer: Seth Maislin
Cover Designer: Karen Montgomery
Interior Designer: David Futato
Illustrator: Robert Romano
Printing History:
March 2010:
First Edition.
O’Reilly and the O’Reilly logo are registered trademarks of O’Reilly Media, Inc. Learning
iPhone
Programming, the image of a lapwing, and related trade dress are trademarks of O’Reilly Media, Inc.
Many of the designations used by manufacturers and sellers to distinguish their products are claimed as
trademarks. Where those designations appear in this book, and O’Reilly Media, Inc. was aware of a
trademark claim, the designations have been printed in caps or initial caps.
While every precaution has been taken in the preparation of this book, the publisher and author assume
no responsibility for errors or omissions, or for damages resulting from the use of the information con-
tained herein.
TM
This book uses RepKover™, a durable and flexible lay-flat binding.
ISBN: 978-0-596-80643-9
[M]
1267461377
www.it-ebooks.info
Table of Contents
Preface .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xi
1. Why Go Native? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
The Pros and Cons 1
Why Write Native Applications? 2
The Release Cycle 3
Build It and They Will Come 4
2. Becoming a Developer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
Registering As an iPhone Developer 5
Enrolling in the iPhone Developer Program 7
The Apple Developer Connection 8
Installing the iPhone SDK 8
Preparing Your iPhone or iPod touch 11
Creating a Development Certificate 12
Getting the UDID of Your Development Device 14
Creating an App ID 15
Creating a Mobile Provisioning Profile 16
Making Your Device Available for Development 17
3. Your First iPhone App .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
Objective-C Basics 19
Object-Oriented Programming 19
The Objective-C Object Model 21
The Basics of Objective-C Syntax 23
Creating a Project 23
Exploring the Project in Xcode 25
Our Project in Interface Builder 32
Adding Code 34
Connecting the Outlets in Interface Builder 36
Putting the Application on Your iPhone 37
v
www.it-ebooks.info
4. Coding in Objective-C . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
Declaring and Defining Classes 41
Declaring a Class with the Interface 41
Defining a Class with the Implementation 42
Object Typing 43
Properties 44
Synthesizing Properties 45
The Dot Syntax 45
Declaring Methods 45
Calling Methods 46
Calling Methods on nil 47
Memory Management 47
Creating Objects 47
The Autorelease Pool 48
The alloc, retain, copy, and release Cycle 48
The dealloc Method 50
Responding to Memory Warnings 50
Fundamental iPhone Design Patterns 50
The Model-View-Controller Pattern 51
Views and View Controllers 51
The Delegates and DataSource Pattern 52
Conclusion 53
5. Table-View-Based Applications .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
Simplifying the Template Classes 55
Creating a Table View 58
Organizing and Navigating Your Source Code 61
Connecting the Outlets 62
Building a Model 65
Adding Images to Your Projects 71
Connecting the Controller to the Model 73
Mocking Up Functionality with Alert Windows 74
Adding Navigation Controls to the Application 75
Adding a City View 79
Edit Mode 85
Deleting a City Entry 89
Adding a City Entry 90
The “Add New City ” Interface 93
Capturing the City Data 100
6. Other View Controllers .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107
Utility Applications 107
Making the Battery Monitoring Application 108
vi | Table of Contents
www.it-ebooks.info
Tab Bar Applications 119
Refactoring the Template 120
Adding Another Tab Bar Item 122
Finishing Up 124
Modal View Controllers 125
Modifying the City Guide Application 126
The Image Picker View Controller 133
Adding the Image Picker to the City Guide Application 133
7. Connecting to the Network . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145
Detecting Network Status 145
Apple’s Reachability Class 145
Embedding a Web Browser in Your App 150
A Simple Web View Controller 150
Displaying Static HTML Files 159
Getting Data Out of a UIWebView 160
Sending Email 161
Getting Data from the Internet 166
Synchronous Requests 166
Asynchronous Requests 167
Using Web Services 168
8. Handling Data .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 191
Data Entry 191
UITextField and Its Delegate 191
UITextView and Its Delegate 193
Parsing XML 195
Parsing XML with libxml2 196
Parsing XML with NSXMLParser 197
Parsing JSON 199
The Twitter Search Service 201
The Twitter Trends Application 202
Regular Expressions 213
Introduction to Regular Expressions 213
Storing Data 217
Using Flat Files 217
Storing Information in an SQL Database 218
Core Data 224
9. Distributing Your Application .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 225
Adding Missing Features 225
Adding an Icon 225
Adding a Launch Image 227
Table of Contents | vii
www.it-ebooks.info
Changing the Display Name 231
Enabling Rotation 232
Building and Signing 233
Ad Hoc Distribution 233
Developer-to-Developer Distribution 240
App Store Distribution 240
Submitting to the App Store 241
The App Store Resource Center 244
Reasons for Rejection 244
10. Using Sensors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 249
Hardware Support 249
Determining Available Hardware Support 249
Setting Required Hardware Capabilities 251
Using the Camera 253
The Core Location Framework 254
Location-Dependent Weather 256
Using the Accelerometer 266
Writing an Accelerometer Application 268
Using the Digital Compass 272
Accessing the Proximity Sensor 274
Using Vibration 275
11. Geolocation and Mapping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 277
User Location 277
Annotating Maps 285
12. Integrating Your Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 295
Application Preferences 295
Accessing Global Preferences 305
Custom URL Schemes 305
Using Custom Schemes 305
Registering Custom Schemes 306
Media Playback 310
Using the Address Book 314
Interactive People Picking 315
Programmatic People Picking 319
13. Other Native Platforms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 321
PhoneGap 321
Download and Installation 322
Building a PhoneGap Project 323
MonoTouch 325
viii | Table of Contents
www.it-ebooks.info
[...]... iPhone requires an Intel Mac running Mac OS X 10.5 (Leopard) or later Registering As an iPhone Developer Before you can develop for the iPhone, you need to become a registered iPhone developer so that you can download and install the iPhone SDK This will give you access to the SDK and allow you to build and test your applications in iPhone Simulator If you take it a step further and enroll in the iPhone. .. registering as an iPhone developer will be to verify your email address Apple Websites You’ll use four main websites as part of the iPhone development process: The iPhone Dev Center This site is where you can get access to the latest versions of the iPhone SDK, along with background technical information, API documentation, sample code, and instructional videos You need to be a registered iPhone developer... Worldwide Developers Conference) Installing the iPhone SDK Once you have registered as an iPhone developer, you can log in to the iPhone Dev Center and download the iPhone SDK At the time of this writing, Apple combined the iPhone SDK and Xcode into a single download It is possible that in future releases you may need to install Xcode first, and then install the iPhone SDK from a separate installer Newer... the iPhone SDK | 9 www.it-ebooks.info Figure 2-2 The Xcode New Project window worse yet, days later) that you’re using classes or frameworks that aren’t actually present on the device Both NSPredicate and NSXMLDocument are commonly used classes For instance, NSXMLDocument is the class most people programming in Objective-C on the Mac (rather than the iPhone) would use to perform an XQuery on an XML document... Who Should Read This Book? This book gives a rapid introduction to programming for the iPhone and iPod touch for those with some programming experience If you are developing on the Mac for the first time, drawn to the platform because of the iPhone, or alternatively you are an experienced Mac programmer making the transition to the iPhone, this book is for you What Should You Already Know? The book... Developer www.it-ebooks.info Enrolling in the iPhone Developer Program If you intend to sell your applications on the App Store, or you just want to be able to deploy them onto your own iPhone or iPod touch, you’ll also need to enroll in the iPhone Developer Program If you’ve not already registered as an iPhone developer, you can do that during this process Your iPhone Developer Program membership lasts... the iPhone and iPod touch and prepare them for distribution You’ll need to be both a registered iPhone developer and enrolled in one of the iPhone Developer Programs to access this site The App Store Resource Center This site provides help and advice on how to distribute your application on the App Store, including preparing your app for submission, understanding the App Store approval process, and learning. .. on the App Store You’ll need to be both a registered iPhone developer and enrolled in the iPhone Developer Standard Program to access this site iTunes Connect This site provides you with the tools to manage your applications on the iTunes App Store and your contracts with Apple You’ll need to be both a registered iPhone developer and enrolled in the iPhone Developer Standard Program to access this site... a Developer This chapter walks you through the process of registering as an iPhone developer and setting up your work environment, from installing Xcode and the iPhone SDK to generating the developer certificates you’ll need to build your applications and deploy them onto your own iPhone or iPod touch Chapter 3, Your First iPhone App This chapter allows you to get hands-on as quickly as possible and... free account, you also won’t have access to prerelease versions of the iPhone SDK or the iPhone OS You can sign up at http://developer.apple.com /iphone/ 5 www.it-ebooks.info If you are an existing Apple Developer Connection (ADC) member, or if you have an iTunes or MobileMe account, you can use your existing Apple ID to register as an iPhone developer However, if you intend to sell software commercially, . www.it-ebooks.info
www.it-ebooks.info
Learning iPhone Programming
www.it-ebooks.info
www.it-ebooks.info
Learning iPhone Programming
Alasdair Allan
Beijing
•
Cambridge
•
Farnham
•
Köln
•
Sebastopol
•
Taipei
•
Tokyo
www.it-ebooks.info
Learning. As an iPhone Developer 5
Enrolling in the iPhone Developer Program 7
The Apple Developer Connection 8
Installing the iPhone SDK 8
Preparing Your iPhone
Ngày đăng: 17/02/2014, 23:20
Xem thêm: Tài liệu Learning iPhone Programming doc