learning ios programming 2nd edition

428 769 0
learning ios programming 2nd edition

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

www.it-ebooks.info www.it-ebooks.info SECOND EDITION Learning iOS Programming Alasdair Allan Beijing • Cambridge • Farnham • Köln • Sebastopol • Tokyo www.it-ebooks.info Learning iOS Programming, Second Edition by Alasdair Allan Copyright © 2012 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. Editors: Shawn Wallace and Brian Jepson Production Editor: Holly Bauer Proofreader: Linley Dolby Indexer: Ellen Troutman Zaig Cover Designer: Karen Montgomery Interior Designer: David Futato Illustrator: Rebecca Demarest March 2010: First Edition (Learning iPhone Programming). March 2012: Second Edition. Revision History for the Second Edition: 2012-03-09 First release See http://oreilly.com/catalog/errata.csp?isbn=9781449303778 for release details. Nutshell Handbook, the Nutshell Handbook logo, and the O’Reilly logo are registered trademarks of O’Reilly Media, Inc. Learning iOS 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 authors assume no responsibility for errors or omissions, or for damages resulting from the use of the information con- tained herein. ISBN: 978-1-449-30377-8 [LSI] 1331580575 www.it-ebooks.info Table of Contents Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ix 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 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 Registering as an iOS Developer 7 Enrolling in the iOS Developer Program 9 The Mac Developer Program 10 Installing the iOS SDK 11 What Happens When There Is a Beta? 13 Preparing Your iOS Device 14 Creating a Development Certificate 15 Getting the UDID of Your Development Device 17 Creating an App ID 18 Creating a Mobile Provisioning Profile 19 Making Your Device Available for Development 20 3. Your First iOS App . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 Objective-C Basics 23 Object-Oriented Programming 23 The Objective-C Object Model 25 The Basics of Objective-C Syntax 27 Creating a Project 28 Exploring the Project in Xcode 32 Our Project in Interface Builder 39 Building the User Interface 41 Connecting the User Interface to the Code 42 iii www.it-ebooks.info Running the Application in the Simulator 46 Putting the Application on Your iPhone 48 4. Coding in Objective-C . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49 Declaring and Defining Classes 49 Declaring a Class with the Interface 49 Defining a Class with the Implementation 51 Object Typing 52 Properties 52 Synthesizing Properties 54 The Dot Syntax 54 Declaring Methods 54 Calling Methods 55 Calling Methods on nil 56 Memory Management 56 Creating Objects 57 The Autorelease Pool 57 The alloc, retain, copy, and release Cycle 58 Automatic Reference Counting 59 The dealloc Method 60 Responding to Memory Warnings 60 Fundamental iOS Design Patterns 60 The Model-View-Controller Pattern 61 Views and View Controllers 61 The Delegates and DataSource Pattern 62 Conclusion 63 5. Table View–Based Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65 Creating the Project 65 Creating a Table View 66 Running the Code 71 Populating the Table View 71 Building a Model 73 Adding Cities to the Guide 78 Adding Images to Your Projects 80 Connecting the Controller to the Model 81 Mocking Up Functionality with Alert Windows 84 Adding Navigation Controls to the Application 85 Adding a City View 87 Edit Mode 94 Deleting a City Entry 98 Adding a City Entry 99 The “Add New City” Interface 102 iv | Table of Contents www.it-ebooks.info Capturing the City Data 109 6. Other View Controllers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115 Utility Applications 115 Making the Battery Monitoring Application 116 Tab Bar Applications 127 Adding Another Tab Bar Item 128 Combining View Controllers 132 Modal View Controllers 137 Modifying the City Guide Application 139 The Image Picker View Controller 148 Adding the Image Picker to the City Guide Application 148 Master-Detail Applications 157 Creating a Universal Application 158 Popover Controllers 167 7. Connecting to the Network . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 169 Detecting Network Status 169 Apple’s Reachability Class 169 Embedding a Web Browser in Your App 181 A Simple Web View Controller 182 Displaying Static HTML Files 191 Getting Data Out of a UIWebView 192 Sending Email 192 Getting Data from the Internet 198 Synchronous Requests 198 Asynchronous Requests 198 Using Web Services 199 8. Handling Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219 Data Entry 219 UITextField and Its Delegate 219 UITextView and Its Delegate 221 Parsing XML 223 Parsing XML with libxml2 224 Parsing XML with NSXMLParser 225 Parsing JSON 227 NSJSONSerialization 227 The JSON Framework 229 Retrieving Twitter Trends 230 Using the Twitter Framework 232 The Twitter Trends Application 232 Regular Expressions 240 Table of Contents | v www.it-ebooks.info Introduction to Regular Expressions 241 Storing Data 245 Using Flat Files 246 Storing Information in a SQL Database 247 Core Data 254 9. Using Sensors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 255 Hardware Support 255 Network Availability 256 Camera Availability 256 Audio Input Availability 256 GPS Availability 257 Magnetometer Availability 257 Setting Required Hardware Capabilities 258 Persistent WiFi 258 Background Modes 259 Differences Between iPhone and iPad 260 Using the Camera 261 The Core Motion Framework 262 Pulling Motion Data 262 Pushing Motion Data 263 The Accelerometer 264 The Gyroscope 268 The Magnetometer 270 Accessing the Proximity Sensor 275 Using Vibration 277 10. Geolocation and Mapping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 279 The Core Location Framework 279 Device Heading 281 Location-Dependent Weather 283 Reverse Geocoding 284 Forward Geocoding 284 CLPlacemark Objects 285 Modifying the Weather Application 285 User Location and MapKit 293 Annotating Maps 301 11. Introduction to iCloud . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 309 How Can I Use iCloud? 309 iCloud Backup 310 Provisioning Your Application for iCloud 310 Using Key-Value Storage 313 vi | Table of Contents www.it-ebooks.info Wrapping Up 320 12. Integrating Your Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 323 Application Preferences 323 The Accounts Framework 333 The Twitter Framework 335 Sending Tweets 336 Custom URL Schemes 337 Using Custom Schemes 337 Registering Custom Schemes 339 Media Playback 344 Using the Address Book 349 Interactive People Picking 349 Programmatic People Picking 353 Sending Text Messages 353 13. Distributing Your Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 355 Adding Missing Features 355 Adding an Icon 355 Adding a Launch Image 359 Changing the Display Name 364 Enabling Rotation 365 Building and Signing 366 Ad Hoc Distribution 367 Developer-to-Developer Distribution 372 App Store Distribution 373 Submitting to the App Store 374 Building Your Application for App Store Distribution 380 The App Store Resource Center 383 Reasons for Rejection 384 14. Going Further . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 389 Cocoa and Objective-C 389 The iOS SDK 389 Web Applications 390 PhoneGap 390 Core Data 391 In-App Purchase 391 MKStoreKit 392 Core Animation 392 Game Kit 392 Writing Games 393 Look and Feel 393 Table of Contents | vii www.it-ebooks.info Hardware Accessories 394 Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 395 viii | Table of Contents www.it-ebooks.info [...]... pocket Second Edition Despite the title change to Learning iOS Programming, the book you hold in your hands is the second edition of Learning iPhone Programming, although there have been sweeping changes along the way While the iPhone is just four years old, to me that sometimes seems like a lifetime The changes made in this second edition reflect the fact that a lot has happened since the first edition. .. websites as part of the iOS development process: iOS Dev Center (http://developer.apple.com /ios/ ) This site is where you can get access to the latest versions of the iOS SDK, along with background technical information, API documentation, sample code, and instructional videos You need to be a registered iOS developer to access the site iOS Provisioning Portal (https://developer.apple.com /ios/ manage/overview/index.action)... to obtain the iOS SDK was to become a registered iOS developer However, you can now download the current release of Xcode and the iOS SDK directly from the Mac App Store If you are still running OS X Snow Leopard, then you need to register as an iOS developer and become a member of the developer program, and download Xcode 4 and the iOS SDK from the iOS Dev Center The version of Xcode available on... process, and learning how to manage your apps on the App Store You’ll need to be both a registered iOS developer and enrolled in the iOS Developer Standard Program to access this site Enrolling in the iOS 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, iPod touch, or iPad, you’ll also need to enroll in the iOS Developer... ground up for Xcode 4 and iOS 5 using ARC Who Should Read This Book? This book gives a rapid introduction to programming for the iPhone, iPod touch, and iPad 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 are an experienced Mac programmer making the transition to the iOS, this book is for you... distribution You’ll need to be both a registered iOS developer and enrolled in one of the iOS Developer Programs to access this site iTunes Connect (https://itunesconnect.apple.com/) 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 iOS developer and enrolled in the iOS Developer Standard Program to access... (Enterprise Program) If you stick with a free account, you also won’t have access to prerelease versions of the iOS SDK or iOS releases You can sign up at http://developer.apple.com /ios/ If you have an existing Apple ID, such as for an iTunes or iCloud account, you can use this identity to register as an iOS developer However, if you intend to sell software commercially, you may want to create a new identity... release Installing the iOS SDK | 13 www.it-ebooks.info Preparing Your iOS Device Before you can install applications onto your iOS device, you must follow a number of steps, and you’ll need to do so in the order shown in Figure 2-3 Figure 2-3 The workflow for creating certificates and mobile provisioning profiles So, if you have enrolled in either the Standard or Enterprise iOS Developer Program, now... to choose a filename (the default is usually your desktop) Next, log in to the iOS Dev Center (http://developer.apple.com /ios/ ) and click on the link to the iOS Provisioning Portal This will take you to the main portal used to manage certificates and devices associated with your developer program account Preparing Your iOS Device | 15 www.it-ebooks.info Figure 2-5 The Keychain Access.app Certificate... a Developer This chapter walks you through the process of registering as an iOS developer and setting up your work environment, from installing Xcode and the iOS SDK to generating the developer certificates you’ll need to build your applications and deploy them onto your own iPhone, iPod touch, or iPad Chapter 3, Your First iOS App This chapter allows you to get hands-on as quickly as possible and walks . www.it-ebooks.info www.it-ebooks.info SECOND EDITION Learning iOS Programming Alasdair Allan Beijing • Cambridge • Farnham • Köln • Sebastopol • Tokyo www.it-ebooks.info Learning iOS Programming, Second Edition by Alasdair. fits in your pocket. Second Edition Despite the title change to Learning iOS Programming, the book you hold in your hands is the second edition of Learning iPhone Programming, although there. Futato Illustrator: Rebecca Demarest March 2010: First Edition (Learning iPhone Programming) . March 2012: Second Edition. Revision History for the Second Edition: 2012-03-09 First release See http://oreilly.com/catalog/errata.csp?isbn=9781449303778

Ngày đăng: 05/05/2014, 14:54

Từ khóa liên quan

Mục lục

  • Table of Contents

  • Preface

    • Second Edition

    • Who Should Read This Book?

    • What Should You Already Know?

    • What Will You Learn?

    • What’s in This Book?

    • Conventions Used in This Book

    • Using Code Examples

    • How to Contact Us

    • Safari® Books Online

    • Acknowledgments

  • Chapter 1. Why Go Native?

    • The Pros and Cons

      • Why Write Native Applications?

    • The Release Cycle

    • Build It and They Will Come

  • Chapter 2. Becoming a Developer

    • Registering as an iOS Developer

    • Enrolling in the iOS Developer Program

    • The Mac Developer Program

    • Installing the iOS SDK

      • What Happens When There Is a Beta?

    • Preparing Your iOS Device

      • Creating a Development Certificate

      • Getting the UDID of Your Development Device

      • Creating an App ID

      • Creating a Mobile Provisioning Profile

      • Making Your Device Available for Development

  • Chapter 3. Your First iOS App

    • Objective-C Basics

      • Object-Oriented Programming

      • The Objective-C Object Model

      • The Basics of Objective-C Syntax

    • Creating a Project

      • Exploring the Project in Xcode

        • Overview of an iPhone application

        • The application delegate

        • The view controller

      • Our Project in Interface Builder

      • Building the User Interface

      • Connecting the User Interface to the Code

      • Running the Application in the Simulator

      • Putting the Application on Your iPhone

  • Chapter 4. Coding in Objective-C

    • Declaring and Defining Classes

      • Declaring a Class with the Interface

      • Defining a Class with the Implementation

      • Object Typing

      • Properties

      • Synthesizing Properties

      • The Dot Syntax

      • Declaring Methods

      • Calling Methods

      • Calling Methods on nil

    • Memory Management

      • Creating Objects

      • The Autorelease Pool

      • The alloc, retain, copy, and release Cycle

      • Automatic Reference Counting

      • The dealloc Method

      • Responding to Memory Warnings

    • Fundamental iOS Design Patterns

      • The Model-View-Controller Pattern

      • Views and View Controllers

      • The Delegates and DataSource Pattern

    • Conclusion

  • Chapter 5. Table View–Based Applications

    • Creating the Project

    • Creating a Table View

      • Running the Code

    • Populating the Table View

    • Building a Model

      • Adding Cities to the Guide

      • Adding Images to Your Projects

    • Connecting the Controller to the Model

      • Mocking Up Functionality with Alert Windows

    • Adding Navigation Controls to the Application

    • Adding a City View

    • Edit Mode

      • Deleting a City Entry

      • Adding a City Entry

      • The “Add New City” Interface

      • Capturing the City Data

  • Chapter 6. Other View Controllers

    • Utility Applications

      • Making the Battery Monitoring Application

        • Building the interface

        • Writing the code

    • Tab Bar Applications

      • Adding Another Tab Bar Item

    • Combining View Controllers

    • Modal View Controllers

      • Modifying the City Guide Application

    • The Image Picker View Controller

      • Adding the Image Picker to the City Guide Application

    • Master-Detail Applications

      • Creating a Universal Application

    • Popover Controllers

  • Chapter 7. Connecting to the Network

    • Detecting Network Status

      • Apple’s Reachability Class

        • Reusing the Reachability class

        • Synchronous reachability

        • Asynchronous reachability

        • Using Reachability directly

        • Updating the Reachability project

    • Embedding a Web Browser in Your App

      • A Simple Web View Controller

      • Displaying Static HTML Files

      • Getting Data Out of a UIWebView

    • Sending Email

    • Getting Data from the Internet

      • Synchronous Requests

      • Asynchronous Requests

      • Using Web Services

        • The Google Weather Service

        • Building an application

        • Parsing the XML document

        • Populating the UI

        • Tidying up

  • Chapter 8. Handling Data

    • Data Entry

      • UITextField and Its Delegate

      • UITextView and Its Delegate

        • Dismissing the UITextView

    • Parsing XML

      • Parsing XML with libxml2

      • Parsing XML with NSXMLParser

    • Parsing JSON

      • NSJSONSerialization

        • Parsing JSON documents

        • Creating JSON documents

      • The JSON Framework

      • Retrieving Twitter Trends

      • Using the Twitter Framework

      • The Twitter Trends Application

        • Retrieving the trends

        • Building a UI

        • Parsing the JSON document

        • Tidying up

    • Regular Expressions

      • Introduction to Regular Expressions

        • NSRegularExpression

        • RegexKitLite

        • Faking regex support with the built-in NSPredicate

    • Storing Data

      • Using Flat Files

        • Reading and writing text content

        • Creating temporary files

        • Other file manipulation

      • Storing Information in a SQL Database

        • Adding a database to your project

        • Data persistence for the City Guide application

        • Refactoring and rethinking

      • Core Data

  • Chapter 9. Using Sensors

    • Hardware Support

      • Network Availability

      • Camera Availability

      • Audio Input Availability

      • GPS Availability

      • Magnetometer Availability

    • Setting Required Hardware Capabilities

      • Persistent WiFi

      • Background Modes

    • Differences Between iPhone and iPad

    • Using the Camera

    • The Core Motion Framework

      • Pulling Motion Data

      • Pushing Motion Data

      • The Accelerometer

        • Using the accelerometer directly

      • The Gyroscope

        • Using the gyroscope directly

      • The Magnetometer

        • Using the magnetometer directly

    • Accessing the Proximity Sensor

    • Using Vibration

  • Chapter 10. Geolocation and Mapping

    • The Core Location Framework

      • Device Heading

    • Location-Dependent Weather

      • Reverse Geocoding

      • Forward Geocoding

      • CLPlacemark Objects

      • Modifying the Weather Application

    • User Location and MapKit

    • Annotating Maps

  • Chapter 11. Introduction to iCloud

    • How Can I Use iCloud?

      • iCloud Backup

      • Provisioning Your Application for iCloud

    • Using Key-Value Storage

    • Wrapping Up

  • Chapter 12. Integrating Your Application

    • Application Preferences

    • The Accounts Framework

    • The Twitter Framework

      • Sending Tweets

    • Custom URL Schemes

      • Using Custom Schemes

        • Making a telephone call

        • Opening the Settings application

      • Registering Custom Schemes

    • Media Playback

    • Using the Address Book

      • Interactive People Picking

      • Programmatic People Picking

    • Sending Text Messages

  • Chapter 13. Distributing Your Application

    • Adding Missing Features

      • Adding an Icon

      • Adding a Launch Image

      • Changing the Display Name

      • Enabling Rotation

    • Building and Signing

      • Ad Hoc Distribution

        • Obtaining a distribution certificate

        • Registering devices

        • Creating a provisioning profile

        • Building your application for ad hoc distribution

        • Distributing an ad hoc build

      • Developer-to-Developer Distribution

      • App Store Distribution

    • Submitting to the App Store

      • Building Your Application for App Store Distribution

      • The App Store Resource Center

    • Reasons for Rejection

  • Chapter 14. Going Further

    • Cocoa and Objective-C

      • The iOS SDK

    • Web Applications

      • PhoneGap

    • Core Data

    • In-App Purchase

      • MKStoreKit

    • Core Animation

    • Game Kit

    • Writing Games

    • Look and Feel

    • Hardware Accessories

  • Index

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan