Siêu thị PDFTải ngay đi em, trời tối mất

Thư viện tri thức trực tuyến

Kho tài liệu với 50,000+ tài liệu học thuật

© 2023 Siêu thị PDF - Kho tài liệu học thuật hàng đầu Việt Nam

Tài liệu Learning iPhone Programming doc
PREMIUM
Số trang
376
Kích thước
8.9 MB
Định dạng
PDF
Lượt xem
721

Tài liệu Learning iPhone Programming doc

Nội dung xem thử

Mô tả chi tiết

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 [email protected].

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

Download and Installation 325

Building a MonoTouch Project 327

14. Going Further . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 335

Cocoa and Objective-C 335

The iPhone SDK 335

Web Applications 336

Core Data 336

Push Notifications 337

In-App Purchase 338

Core Animation 339

Game Kit 339

Writing Games 339

Look and Feel 340

Hardware Accessories 340

Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 343

Table of Contents | ix

www.it-ebooks.info

www.it-ebooks.info

Preface

The arrival of the iPhone changed everything. Or, at the very least, it changed the

direction of software development for mobile platforms, which is a pretty big thing. It

spawned an entire generation of copycat devices and shook an entire multibillion-dollar

industry to its knees. Despite this, it still fits in your pocket.

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 assumes some knowledge of C, or at least passing knowledge of a C-derived

language. Additionally, while I do give a crash course, some familiarity with object￾oriented programming concepts would be helpful.

What Will You Learn?

This book will guide you through developing your first application for the iPhone, from

opening Xcode for the first time to submitting your application to the App Store. You’ll

learn about Objective-C and the core frameworks needed to develop for the iPhone by

writing applications that use them, giving you a basic framework for building your own

applications independently.

xi

www.it-ebooks.info

What’s in This Book?

Here’s a short summary of the chapters in this book and what you’ll find inside:

Chapter 1, Why Go Native?

This chapter discusses the need for native applications and compares building

native applications to building web applications.

Chapter 2, Becoming 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 walks you

through building your first Hello World application, including how to deploy and

run the application on your iPhone or iPod touch.

Chapter 4, Coding in Objective-C

This chapter provides a crash course in the basics of the Objective-C language, and

if you’re familiar with another C-derived language (and perhaps with object￾oriented programming), it should be enough to get you up and running with

Objective-C and the Cocoa Touch frameworks.

Chapter 5, Table-View-Based Applications

The UITableView and associated classes are perhaps the most commonly used

classes when building user interfaces for iPhone or iPod touch applications. Due

to the nature of the applications, these classes can be used to solve a large cross

section of problems, and as a result they appear almost everywhere. In this chapter,

we dive fairly deeply into the table view classes.

Chapter 6, Other View Controllers

After discussing the table view controller in detail, we discuss some of the other

view controllers and classes that will become useful when building your applica￾tions: simple two-screen views, single-screen tabbed views, modal view controllers,

and a view controller for selecting video and images.

Chapter 7, Connecting to the Network

This chapter discusses connecting to the Internet, browsing the Web, sending

email, and retrieving information.

Chapter 8, Handling Data

This chapter discusses how to handle data input, both from the application user

and programmatically, and how to parse XML and JSON documents. The chapter

also covers storing data in flat files and storing data with the SQLite database

engine.

xii | Preface

www.it-ebooks.info

Chapter 9, Distributing Your Application

This chapter talks about how to add some final polish to your application and

walks you through the process of building your application for distribution, either

via ad hoc distribution or for the App Store.

Chapter 10, Using Sensors

This chapter discusses how to determine what hardware is available and illustrates

how to deal with the major sensors on the iPhone and iPod touch: the

accelerometer, magnetometer, camera, and GPS.

Chapter 11, Geolocation and Mapping

This chapter walks you through the process of building applications that make use

of the Core Location and MapKit frameworks.

Chapter 12, Integrating Your Application

This chapter shows you some of the tricks to integrate your application with the

iPhone’s software ecosystem, how to present user preferences with Settings Bun￾dles, and how to use custom URL schemes to launch your application. It also

discusses how to make use of the Media Player and Address Book.

Chapter 13, Other Native Platforms

This chapter deals with the PhoneGap and MonoTouch platforms for building

native applications for the iPhone and iPod touch that can be sold on the App Store.

The chapter then walks you through the installation process and building your first

Hello World application for both platforms.

Chapter 14, Going Further

This chapter provides a collection of pointers to more advanced material on the

topics we covered in the book, and material covering some of those topics that we

didn’t manage to talk about in the book.

Conventions Used in This Book

The following typographical conventions are used in this book:

Italic

Indicates new terms, URLs, email addresses, filenames, and file extensions

Constant width

Used for program listings, as well as within paragraphs to refer to program elements

such as variable or function names, databases, data types, environment variables,

statements, and keywords

Constant width bold

Shows commands or other text that should be typed literally by the user

Constant width italic

Shows text that should be replaced with user-supplied values or by values deter￾mined by context

Preface | xiii

www.it-ebooks.info

Tải ngay đi em, còn do dự, trời tối mất!