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

IPhone in Action pps
Nội dung xem thử
Mô tả chi tiết
iPhone in Action
iPhone in Action
INTRODUCTION TO WEB AND SDK DEVELOPMENT
CHRISTOPHER ALLEN
SHANNON APPELCLINE
MANNING
Greenwich
(74° w. long.)
For online information and ordering of this and other Manning books, please visit
www.manning.com. The publisher offers discounts on this book when ordered in quantity.
For more information, please contact
Special Sales Department
Manning Publications Co.
Sound View Court 3B fax: (609) 877-8256
Greenwich, CT 06830 email: [email protected]
©2009 by Manning Publications Co. All rights reserved.
No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in
any form or by means electronic, mechanical, photocopying, or otherwise, without prior written
permission of the publisher.
Many of the designations used by manufacturers and sellers to distinguish their products are
claimed as trademarks. Where those designations appear in the book, and Manning
Publications was aware of a trademark claim, the designations have been printed in initial caps
or all caps.
Recognizing the importance of preserving what has been written, it is Manning’s policy to have
the books we publish printed on acid-free paper, and we exert our best efforts to that end.
Recognizing also our responsibility to conserve the resources of our planet, Manning books are
printed on paper that is at least 15% recycled and processed without the use of elemental chlorine.
Development Editor Tom Cirtin
Manning Publications Co. Copyeditors: Liz Welch, Andy Carroll
Sound View Court 3B Typesetter: Gordan Salinovic
Greenwich, CT 06830 Cover designer: Leslie Haimes
ISBN 193398886X
Printed in the United States of America
1 2 3 4 5 6 7 8 9 10 – MAL – 13 12 11 10 09 08
v
contents
preface xv
acknowledgments xvii
about this book xix
PART 1INTRODUCING IPHONE PROGRAMMING.................... 1
1 Introducing the iPhone 3
1.1 iPhone core specifications 4
iPhone input and output specifications 5 ■ iPhone network
specifications 6 ■ iPhone browser specifications 7 ■ Other iPhone
hardware features 8
1.2 How the iPhone compares to the industry 8
The physical comparison 9 ■ Competitive internet viewing 9
Mobile web standards 10 ■ The rest of the story 10
1.3 How the iPhone is unique 10
1.4 Understanding iPhone input and output 12
Output and iPhone viewport 12 ■ Output and orientations 12
Input and iPhone mousing 13
1.5 Summary 15
vi CONTENTS
2 Web development or the SDK? 16
2.1 Comparing the two programming styles 16
2.2 A central philosophy: the continuum of programming 18
2.3 Advantages and disadvantages 19
Web development 19 ■ SDK development 21 ■ To each program its
platform 22
2.4 Stand-alone iPhone development 22
Web development models 23 ■ SDK development models 24
2.5 Integrated iPhone development 25
Mirrored development 26 ■ Mixed development 26 ■ Client-server
development 27 ■ Last thoughts on integration 27
2.6 Summary 27
PART 2DESIGNING WEB PAGES FOR THE IPHONE ............... 29
3 Redeveloping web pages for the iPhone 31
3.1 The iPhone viewport 32
Making sitewide viewport changes 34 ■ Making local viewport
changes 34 ■ Viewport properties and constants 35
3.2 Making your web pages iPhone friendly 36
Avoiding missing iPhone functionality 36 ■ Creating good links 39
Practicing good web work 39 ■ Fixing common problems 41
3.3 Making your web pages iPhone optimized 43
Detecting the iPhone through USER_AGENT 43 ■ Detecting the
iPhone through CSS 44 ■ Optimizing with CSS 44
3.4 Manipulating iPhone chrome 45
The three bars 45 ■ Web clips 46
3.5 Capturing iPhone events 47
One-fingered touches 47 ■ Two-fingered gestures 48
3.6 Redisplaying web pages 49
The Gmail iPhone pages 50 ■ The Facebook iPhone pages 51
3.7 Supporting non-iPhone users 52
3.8 Summary 54
CONTENTS vii
4 Advanced WebKit and textual web apps 55
4.1 Introducing the WebKit 56
New HTML elements 57 ■ New CSS elements 57
4.2 CSS transforms, transitions, and animations 59
The transform function 59 ■ The transition function 61 ■ The
animation function 64
4.3 The WebKit database 65
Loading a database 65 ■ Running a transaction 65 ■ A sample
database 66
4.4 Adjusting the chrome 69
4.5 Recognizing touches and gestures 69
Accessing events 70 ■ Converting events 71 ■ Accessing touches 72
Accessing gestures 74
4.6 Recognizing orientation 75
4.7 Upcoming features: CSS gradients and masks 76
CSS gradients 76 ■ CSS masks 77 ■ The Canvas alternative 77
4.8 Summary 78
5 Using iUI for web apps 80
5.1 Creating your own iPhone UI 81
The graphical interface 81 ■ The iPhone data paradigm 83 ■ Other
iPhone design elements 83
5.2 Getting ready for iUI 84
5.3 Developing with iUI 85
The iUI toolbar 86 ■ iUI lists 87 ■ iUI dialogs 89 ■ iUI searches
done right with Ajax 90 ■ iUI panels and rows 91 ■ iUI buttons 92
iUI attributes 93
5.4 Creating an iUI back end 94
5.5 Other iUI tips and tricks 95
Organizing your code 95 ■ Improving data listings 97
Compressing iUI 98 ■ Selecting a different look 98
5.6 Integrating iUI with other libraries 99
Using jQuery with iUI 99 ■ Using iUI with WebKit 100
5.7 Summary 101
viii CONTENTS
6 Using Canvas for web apps 102
6.1 Getting ready for Canvas 103
Enabling Canvas 103 ■ Ensuring compatibility 103 ■ Putting it
together 104
6.2 Drawing paths 105
Basic path commands 106 ■ Curve commands 107
6.3 Drawing shapes 110
Drawing rectangles 110 ■ Writing shape functions 110
6.4 Creating styles: colors, gradients, and lines 112
Color styles 112 ■ Gradient styles 112 ■ Line styles 114
6.5 Modifying composition and clipping 114
Global variables 115 ■ Clipping paths 116
6.6 Transforming and restoring 116
Transformations 116 ■ State stacking 117
6.7 Incorporating images, patterns, and text 117
Image commands 118 ■ Pattern commands 119 ■ Text
commands 119
6.8 Putting it together 120
6.9 Applying animation 121
6.10 Summary 123
7 Building web apps with Dashcode 124
7.1 An introduction to Dashcode 125
Starting a Dashcode project 126 ■ The anatomy of Dashcode 126
Running Dashcode projects 130 ■ Deploying Dashcode projects 130
7.2 Writing Dashcode programs 131
Using library parts 132 ■ Adding action buttons 134 ■ Using
the list-based Browser template 135 ■ Working with the stackLayout
part 136 ■ Exploring the rest of Dashcode 139
7.3 Integrating Dashcode with existing libraries 140
Integrating Dashcode with WebKit 140 ■ Integrating Dashcode
with iUI 141 ■ Integrating Dashcode with Canvas 141 ■ Deeper
integration 142
7.4 Summary 142
CONTENTS ix
8 Debugging iPhone web pages 143
8.1 Using Apache locally 143
8.2 Debugging with your desktop browser 144
Using Safari 144 ■ Using Firefox 146 ■ Using the iPhone
Simulator 148
8.3 Debugging with your iPhone 149
Using iPhone Debug 150 ■ Using bookmarklets 150
8.4 Profiling for the iPhone 151
8.5 Summary 152
9 SDK programming for web developers 154
9.1 An introduction to C’s concepts 155
Declarations and typing 156 ■ Memory management and pointers 157
File structure and directives 158 ■ Compiling 158 ■ Other
elements 159
9.2 An introduction to object-oriented programming 159
Objects and classes 160 ■ Messaging 160
9.3 The Model-View-Controller (MVC) pattern 161
9.4 Summary 162
PART 3LEARNING SDK FUNDAMENTALS ........................ 165
10 Learning Objective-C and the iPhone OS 167
10.1 Getting ready for the SDK 168
Installing the SDK 168 ■ The anatomy of the SDK 169
10.2 Introducing Objective-C 171
The big picture 171 ■ The message 172 ■ Class definition 174
Properties 176 ■ Other compiler directives 178 ■ Categories and
protocols 179 ■ Wrapping up Objective-C 179
10.3 Introducing the iPhone OS 180
The anatomy of the iPhone OS 180 ■ Windows and views 183
10.4 The iPhone OS’s methods 184
Object creation 184 ■ Memory management 186 ■ Event response 187
Life-cycle management 188
10.5 Summary 189
x CONTENTS
11 Using Xcode 190
11.1 Introducing Xcode 190
The anatomy of Xcode 191 ■ Compiling and executing in Xcode 192
11.2 Creating a first project in Xcode: Hello, World! 192
Understanding main.m 193 ■ Understanding the application
delegate 194 ■ Writing Hello, World! 196
11.3 Creating a new class in Xcode 198
The new class how-to 199 ■ The header file 199 ■ The source code
file 200 ■ Linking it in 202
11.4 Other Xcode functionality 202
Adding frameworks with Xcode 202 ■ Using alternate templates with
Xcode 203 ■ Xcode tips and tricks 204
11.5 Summary 205
12 Using Interface Builder 206
12.1 An introduction to Interface Builder 207
The anatomy of Interface Builder 207 ■ Simulating in Interface
Builder 210
12.2 Creating a first project in Interface Builder:
pictures and the web 210
Creating new objects 210 ■ Manipulating objects graphically 211
Using the inspector window 211 ■ Working with pictures 213
12.3 Building connections in Interface Builder 214
Declaring an IBOutlet 215 ■ Connecting an object 215 ■ Coding
with IBOutlets 216
12.4 Other Interface Builder functionality 217
Building other connections 217 ■ Creating external objects 218
Initializing Interface Builder objects 218 ■ Accessing .xib files 219
Creating new .xib files 219
12.5 Summary 220
13 Creating basic view controllers 221
13.1 The view controller family 222
13.2 The bare view controller 223
The anatomy of a view controller 223 ■ Creating a view
controller 224 ■ Building up a view controller interface 225
Using your view controller 226
CONTENTS xi
13.3 The table view controller 231
The anatomy of a table view controller 231 ■ Creating a table view
controller 231 ■ Building up a table interface 233 ■ Using your
table view controller 238
13.4 Summary 239
14 Monitoring events and actions 240
14.1 An introduction to events 240
The responder chain 241 ■ Touches and events 242
14.2 A touching example: the event reporter 244
Setting things up in Interface Builder 245 ■ Preparing a view for
touches 246 ■ Controlling your events 248
14.3 Other event functionality 250
Regulating events 250 ■ Other event methods and properties 251
14.4 An introduction to actions 252
The UIControl object 252 ■ Control events and actions 252 ■ The
addTarget:action:forControlEvents: method 254
14.5 Adding a button to an application 255
Using addTarget:action:forControlEvents: 255 ■ Using an IBAction 256
14.6 Other action functionality 257
The UITextField 257 ■ The UISlider 259 ■ A TextField/Slider
mashup 260 ■ Actions made easy 261 ■ Actions in use 261
14.7 Introducing notifications 262
14.8 Summary 262
15 Creating advanced view controllers 264
15.1 The tab bar view controller 265
The anatomy of a tab bar controller 265 ■ Creating a tab bar
controller 266 ■ Building a tab bar interface 267 ■ Using your tab
bar controller 271
15.2 The navigation controller 271
The anatomy of a navigation controller 272 ■ Creating a
navigation controller 273 ■ Building a navigation controller 274
Using your navigation controller 276
15.3 Using the flipside controller 279
15.4 Modal view controllers 281
15.5 Summary 281
xii CONTENTS
PART 4PROGRAMMING WITH THE SDK TOOLKIT............ 283
16 Data: actions, preferences, files, SQLite, and addresses 285
16.1 Accepting user actions 286
16.2 Maintaining user preferences 288
Creating your own preferences 288 ■ Using the system settings 293
16.3 Opening files 297
Accessing your bundle 298 ■ Accessing other directories 299
Manipulating files 300 ■ Filesaver: a UITextView example 301
16.4 Using SQLite 303
Setting up an SQLite database 304 ■ Accessing SQLite 305
Accessing your SQLite database 305 ■ Building a navigation menu
from a database 306 ■ Expanding this example 313
16.5 Accessing the Address Book 313
An overview of the frameworks 313 ■ Accessing Address Book
properties 314 ■ Querying the Address Book 316 ■ Using the
Address Book UI 318
16.6 Summary 322
17 Positioning: accelerometers and location 324
17.1 The accelerometer and orientation 325
The orientation property 325 ■ The orientation notification 325
17.2 The accelerometer and movement 326
Accessing the UIAccelerometer 326 ■ Parsing the UIAcceleration 327
Checking for gravity 328 ■ Checking for movement 330
Recognizing simple accelerometer movement 330
17.3 The accelerometer and gestures 333
17.4 All about Core Location 335
The location classes 336 ■ An example using location and distance 337
An example using altitude 340 ■ Core Location and the Internet 342
17.5 Summary 343
18 Media: images and sounds 344
18.1 An introduction to images 345
Loading a UIImage 345 ■ Drawing a UIImageView 346
Modifying an image in the UIKit 347
18.2 Drawing simple images with Core Graphics 347
CONTENTS xiii
18.3 Accessing photos 349
Using the image picker 349 ■ Taking photos 349 ■ Saving to the
photo album 350
18.4 Collage: an image example 351
The collage view controller 351 ■ The collage temporary image
view 354 ■ The collage view 355 ■ Expanding on this example 356
18.5 Using the Media Player framework 357
The media player class 357 ■ The volume view 359 ■ Better
integrating the media player 359
18.6 Playing sounds manually 360
Playing simple sounds 361 ■ Vibrating the iPhone 362 ■ Playing
complex sounds 362 ■ Other audio frameworks 364
18.7 Summary 365
19 Graphics: Quartz, Core Animation, and OpenGL 366
19.1 An introduction to Quartz 2D 367
19.2 The Quartz context 367
Drawing to a UIView 369 ■ Drawing to a bitmap 370
19.3 Drawing paths 371
Finishing a path 372 ■ Creating reusable paths 373 ■ Drawing
rectangles 374
19.4 Setting the graphic state 375
Setting colors 375 ■ Making transformations 376 ■ Setting
clipping paths 379 ■ Other settings 379 ■ Managing the
state 380
19.5 Advanced drawing in Quartz 381
Drawing gradients 381 ■ Drawing images 383 ■ Drawing
words 384 ■ What we didn’t cover 385
19.6 Drawing on a picture: an example 386
The photodraw view controller 386 ■ The photodraw view 388
Expanding on the example 390
19.7 An introduction to Core Animation 391
The fundamentals of Core Animation 391 ■ Getting started with
Core Animation 392 ■ Drawing a simple implicit animation 392
Drawing a simple explicit animation 393
19.8 An introduction to OpenGL 394
19.9 Summary 394
xiv CONTENTS
20 The web: web views and internet protocols 396
20.1 The hierarchy of the internet 397
20.2 Low-level networking 397
The CFHost class 398
20.3 Working with URLs 399
Creating an NSURL 399 ■ Building an NSURLRequest 400
Manipulating HTML data by hand 400
20.4 Using UIWebView 401
Calling up the web view 402 ■ Managing the web view delegate 403
Thumbnails: a web view example 403 ■ Google Maps: a Core
Location example 405
20.5 Parsing XML 407
Starting up NSXMLParser 408 ■ Acting as a delegate 408
Building a sample RSS reader: an XML example 409 ■ Altitude
redux: a Core Location example 414
20.6 POSTing to the web 416
POSTing by hand 416 ■ Submitting forms 417
20.7 Accessing the social web 418
Using web protocols 419 ■ Using TouchJSON 420
20.8 Summary 421
appendix A iPhone OS class reference 423
appendix B External sources and references 427
appendix C Publishing your SDK program 429
index 433