Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 900 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
900
Dung lượng
24,65 MB
Nội dung
iOS 5Programming Cookbook
Vandad Nahavandipoor
Beijing
•
Cambridge
•
Farnham
•
Köln
•
Sebastopol
•
Tokyo
www.it-ebooks.info
iOS 5Programming Cookbook
by Vandad Nahavandipoor
Revision History for the :
See http://oreilly.com/catalog/errata.csp?isbn=9781449311438 for release details.
ISBN: 978-1-449-31143-8
1318287906
www.it-ebooks.info
To Agnieszka Marta Dybowska.
www.it-ebooks.info
www.it-ebooks.info
Table of Contents
Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ix
1. The Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.1
Creating a Simple iOS App in Xcode 2
1.2 Understanding Interface Builder 7
1.3
Compiling iOS Apps 13
1.4 Running iOS Apps on the Simulator 15
1.5
Running iOS Apps on iOS Devices 17
1.6
Packaging iOS Apps For Distribution 21
1.7
Declaring Variables in Objective-C 27
1.8 Comparing Values in Objective-C with an If Statement 29
1.9 Implementing Loops with For Statements 32
1.10 Implementing While Loops 35
1.11 Creating Custom Classes 37
1.12 Defining Functionality for Classes 42
1.13 Defining Two or More Methods with the Same Name 46
1.14
Allocating and Initializing Objects 49
1.15
Adding Properties to Classes 51
1.16
Moving from Manual Reference Counting to Automatic Reference
Counting 54
1.17
Typecasting with Automatic Reference Counting 59
1.18
Delegating Tasks with Protocols 63
1.19
Determining Whether Instance or Class Methods are Available 71
1.20
Determining Whether a Class is Available at Run Time 74
1.21
Allocating and Making Use of Strings 75
1.22
Allocating and Making Use of Numbers 79
1.23
Allocating and Making Use of Arrays 81
1.24
Allocating and Making Use of Dictionaries 86
1.25
Allocating and Making Use of Sets 89
1.26
Creating Bundles 92
1.27
Loading Data From the Main Bundle 93
1.28
Loading Data From Other Bundles 97
iii
www.it-ebooks.info
1.29 Sending Notifications with NSNotificationCenter 100
1.30 Listening for Notifications Sent From NSNotificationCenter 104
2. Implementing Controllers and Views . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109
2.1 Displaying Alerts with UIAlertView 110
2.2 Creating and Using Switches with UISwitch 121
2.3 Picking Values with UIPickerView 126
2.4 Picking Date and Time with UIDatePicker 136
2.5 Implementing Range Pickers with UISlider 144
2.6 Grouping Compact Options with UISegmentedControl 151
2.7 Presenting and Managing Views with UIViewController 159
2.8
Implementing Navigation with UINavigationController 163
2.9
Manipulating a Navigation Controller’s Array of
View Controllers 170
2.10
Displaying an Image on a Navigation Bar 171
2.11
Adding Buttons to Navigation Bars with UIBarButtonItem 172
2.12 Presenting Multiple View Controllers with UITabBarController 182
2.13
Displaying Static Text with UILabel 191
2.14 Accepting User Text Input with UITextField 196
2.15
Displaying Long Lines of Text with UITextView 208
2.16
Adding Buttons to the User Interface with UIButton 214
2.17 Displaying Images with UIImageView 220
2.18 Creating Scrollable Content with UIScrollView 226
2.19 Loading Web Pages with UIWebView 231
2.20 Presenting Master-Detail Views with UISplitViewController 237
2.21 Enabling Paging with UIPageViewController 245
2.22 Displaying Popovers with UIPopoverController 251
2.23 Displaying Progress with UIProgressView 262
2.24 Listening and Reacting to Keyboard Notifications 264
3.
Constructing and Using Table Views . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 281
3.1
Instantiating a Table View 281
3.2
Assigning a Delegate to a Table View 284
3.3
Populating a Table View with Data 286
3.4
Receiving and Handling Table View Events 290
3.5
Using Different Types of Accessories in a Table View Cell 291
3.6
Creating Custom Table View Cell Accessories 294
3.7
Displaying Hierarchical Data in Table Views 297
3.8
Enabling Swipe Deletion of Table View Cells 299
3.9
Constructing Headers and Footers in Table Views 302
3.10
Displaying Context Menus on Table Views Cells 314
3.11
Moving Cells and Sections in Table Views 319
3.12
Deleting Cells and Sections from Table Views 326
iv | Table of Contents
www.it-ebooks.info
4. Storyboards . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 329
4.1 Creating a Project with Storyboards 330
4.2 Adding a Navigation Controller to a Storyboard 333
4.3 Passing Data From One Screen to Another 346
4.4 Adding a Storyboard to an Existing Project 349
5. Concurrency . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 353
5.1 Constructing Block Objects 358
5.2 Accessing Variables in Block Objects 362
5.3 Invoking Block Objects 369
5.4 Dispatching Tasks to Grand Central Dispatch 370
5.5 Performing UI-Related Tasks with GCD 371
5.6
Performing Non-UI-Related Tasks Synchronously with GCD 376
5.7
Performing Non-UI Related Tasks Asynchronously with GCD 379
5.8
Performing Tasks After a Delay with GCD 385
5.9
Performing a Task at Most Once with GCD 388
5.10
Grouping Tasks Together with GCD 390
5.11
Constructing Your Own Dispatch Queues with GCD 394
5.12
Running Tasks Synchronously with Operations 397
5.13
Running Tasks Asynchronously with Operations 404
5.14
Creating Dependency Between Operations 411
5.15
Creating Concurrency with Timers 413
5.16
Creating Concurrency with Threads 418
5.17
Invoking Background Methods 424
5.18
Exiting Threads and Timers 425
6. Core Location and Maps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 429
6.1
Creating a Map View 431
6.2
Handling the Events of a Map View 435
6.3
Pinpointing the Location of a Device 436
6.4
Displaying Pins on a Map View 439
6.5
Displaying Pins with Different Colors on a Map View 443
6.6 Displaying Custom Pins on a Map View 449
6.7 Converting Longitude and Latitude to a Meaningful Address 452
6.8 Converting Meaningful Addresses to Longitude and Latitude 455
7. Implementing Gesture Recognizers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 457
7.1
Detecting Swipe Gestures 459
7.2
Detecting Rotation Gestures 461
7.3
Detecting Panning and Dragging Gestures 465
7.4
Detecting Long Press Gestures 467
7.5
Detecting Tap Gestures 470
7.6
Detecting Pinch Gestures 472
Table of Contents | v
www.it-ebooks.info
8. Networking, JSON, XML and Twitter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 475
8.1 Downloading Asynchronously with NSURLConnection 475
8.2 Handling Timeouts in Asynchronous Connections 478
8.3 Downloading Synchronously with NSURLConnection 479
8.4 Modifying a URL Request with NSMutableURLRequest 482
8.5
Sending HTTP GET Requests with NSURLConnection 483
8.6 Sending HTTP POST Requests with NSURLConnection 486
8.7 Sending HTTP DELETE Requests with NSURLConnection 489
8.8 Sending HTTP PUT Requests with NSURLConnection 491
8.9 Serializing Arrays and Dictionaries into JSON 494
8.10 Deserializing JSON into Arrays and Dictionaries 496
8.11
Integrating Twitter Functionality Into Your Apps 499
8.12
Parsing XML with NSXMLParser 508
9.
Audio and Video . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 515
9.1
Playing Audio Files 515
9.2
Handling Interruptions While Playing Audio 517
9.3
Recording Audio 518
9.4
Handling Interruptions While Recording Audio 525
9.5
Playing Audio over Other Active Sounds 526
9.6
Playing Video Files 530
9.7
Capturing Thumbnails from a Video File 534
9.8
Accessing the Music Library 537
10. Address Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 547
10.1
Getting a Reference to Address Book 550
10.2
Retrieving All the People in the Address Book 552
10.3
Retrieving Properties of Address Book Entries 553
10.4
Inserting a Person Entry into the Address Book 558
10.5
Inserting a Group Entry into the Address Book 562
10.6
Adding Persons to Groups 565
10.7
Searching the Address Book 569
10.8 Retrieving and Setting a Person’s Address Book Image 573
11. Camera and the Photo Library . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 581
11.1
Detecting and Probing the Camera 583
11.2
Taking Photos with the Camera 588
11.3
Taking Videos with the Camera 592
11.4
Storing Photos in the Photo Library 595
11.5
Storing Videos in the Photo Library 598
11.6
Retrieving Photos and Videos from the Photo Library 600
11.7
Retrieving Assets from the Assets Library 601
11.8
Editing Videos on an iOS Device 609
vi | Table of Contents
www.it-ebooks.info
12. Multitasking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 615
12.1 Detecting the Availability of Multitasking 616
12.2 Completing a Long-Running Task in the Background 617
12.3 Receiving Local Notifications in the Background 621
12.4 Playing Audio in the Background 630
12.5
Handling Location Changes in the Background 633
12.6 Saving and Loading the State of Multitasking iOS Apps 636
12.7 Handling Network Connections in the Background 639
12.8 Handling Notifications Delivered to a Waking App 642
12.9 Responding to Changes in App Settings 644
12.10 Opting Out of Multitasking 647
13.
Core Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 649
13.1
Creating a Core Data Model with Xcode 653
13.2
Generating Class Files for Core Data Entities 658
13.3
Creating and Saving Data Using Core Data 661
13.4
Reading Data from Core Data 663
13.5
Deleting Data From Core Data 666
13.6
Sorting Data in Core Data 669
13.7
Boosting Data Access in Table Views 671
13.8
Implementing Relationships in Core Data 683
14. Dates, Calendars and Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 691
14.1
Retrieving the List of Calendars 694
14.2
Adding Events to Calendars 696
14.3
Accessing the Contents of Calendars 700
14.4
Removing Events from Calendars 705
14.5
Adding Recurring Events to Calendars 709
14.6
Retrieving the Attendees of an Event 714
14.7
Adding Alarms to Calendars 719
14.8
Handling Event Changed Notifications 721
14.9
Presenting Event View Controllers 724
14.10 Presenting Event Edit View Controllers 729
15. Graphics and Animations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 733
15.1
Enumerating and Loading Fonts 745
15.2
Drawing Text 747
15.3
Constructing, Setting, and Using Colors 749
15.4
Drawing Images 754
15.5
Drawing Lines 759
15.6
Constructing Paths 766
15.7
Drawing Rectangles 771
15.8
Adding Shadows to Shapes 776
Table of Contents | vii
www.it-ebooks.info
15.9 Drawing Gradients 783
15.10 Displacing Shapes Drawn on Graphic Contexts 792
15.11 Scaling Shapes Drawn on Graphic Contexts 796
15.12 Rotating Shapes Drawn on Graphic Contexts 800
15.13 Animating and Moving Views 802
15.14 Animating and Scaling Views 812
15.15 Animating and Rotating Views 814
16. Core Motion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 817
16.1 Detecting the Availability of an Accelerometer 819
16.2
Detecting the Availability of a Gyroscope 821
16.3
Retrieving Accelerometer Data 822
16.4
Detecting Shakes on an iOS Device 825
16.5
Retrieving Gyroscope Data 830
17. iCloud . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 833
17.1
Setting Up Your App For iCloud 834
17.2
Storing and Synchronizing Dictionaries in iCloud 840
17.3
Creating and Managing Folders for Apps in iCloud 845
17.4
Searching for Files and Folders in iCloud 852
17.5
Storing User Documents in iCloud 862
Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 879
viii | Table of Contents
www.it-ebooks.info
[...]... Simple iOS App in Xcode Problem You've started to learn iOS Programming and you want to create a really simple iOS Project and app in Xcode Solution Create a new iOS Project in Xcode and then run it in the iOS Simulator using Command +Shift+R Discussion I'll assume you have a Mac and you have already installed the Xcode set of tools Now you want to create an iOS Project and run that App on the iOS Simulator... your iOS App on iPad Simulator 8 Now that everything is ready, press the Command+Shift+R keys on your keyboard or simply go to the Product menu and then press the Run button as shown in Figure 1 -5 1.1 Creating a Simple iOS App in Xcode | 5 www.it-ebooks.info Figure 1 -5 The Run menu item in Xcode Congratulations Now you have a simple app running in iOS Simulator As you saw, there are various different iOS. .. availability of front- and back-facing cameras on an iOS device Some of the recipes in this chapter are specific to iOS 4, with the rest working on both iOS 3 and iOS 4 You will also learn how to access the Photo Library using the Assets Library framework which is available in iOS 4 and later At the end of the chapter, you will learn about editing videos right on an iOS device using a built-in view controller... and editors at: O’Reilly Media, Inc 10 05 Gravenstein Highway North Sebastopol, CA 954 72 (800) 998-9938 (in the United States or Canada) (707) 829- 051 5 (international or local) (707) 829-0104 (fax) We have a web page for this book, where we list errata, examples, and any additional information You can access this page at: http://oreilly.com/catalog/97814493882 25 There is also a companion website to... the iOS development environment and know how to create an app for the iPhone or iPad This book does not get novice programmers started, but presents useful ways to get things done for iOS programmers ranging from novices to experts Organization of This Book In this book, we will discuss frameworks and classes that are available in iOS 3 and iOS 4 In some recipes, you will find code that runs only on iOS. .. that develops iOS applications iPhone Human Interface Guidelines http://developer.apple.com/library /ios/ #documentation/userexperience/conceptual/ mobilehig/Introduction/Introduction.html iPad Human Interface Guidelines https://developer.apple.com/library /ios/ #documentation/General/Conceptual/iPad HIG/Introduction/Introduction.html I also suggest that you skim through the iOS Application Programming. .. applications for different companies across the globe As you might have already guessed, iOS is my favorite platform and Objective-C is my favorite programming language I find that Objective-C helps programmers write clean code and iOS helps developers write user-friendly and useful applications I have coded in other programming languages such as Assembly (using NASM and TASM) and Delphi/Pascal for many... HIG/Introduction/Introduction.html I also suggest that you skim through the iOS Application Programming Guide” in the iOS Reference Library for some tips and advice on how to make great iOS applications: https://developer.apple.com/library /ios/ #documentation/iPhone/Conceptual/iPho neOSProgrammingGuide/Introduction/Introduction.html One of the things you will notice when reading Chapter 12 is the use... permission We appreciate, but do not require, attribution An attribution usually includes the title, author, publisher, and ISBN For example: iOS 4 Programming Cookbook, by Vandad Nahavandipoor (O’Reilly) Copyright 2011 Vandad Nahavandipoor, 978-1449-38822 -5. ” If you feel your use of code examples falls outside fair use or the permission given here, feel free to contact us at permissions@oreilly.com... how to use gesture recognizers, which enable your users to easily and intuitively manipulate the graphical interface of your iOS applications In this chapter, you will learn how to use all available gesture recognizers in the iOS SDK, with working examples tested on iOS 3 and iOS 4 on different devices such as the iPhone 3GS, iPhone 4, and iPad Chapter 8, Networking, JSON, XML and Twitter Demonstrates . Address Book 55 0 10.2 Retrieving All the People in the Address Book 55 2 10.3 Retrieving Properties of Address Book Entries 55 3 10.4 Inserting a Person Entry into the Address Book 55 8 10 .5 Inserting. 749 15. 4 Drawing Images 754 15. 5 Drawing Lines 759 15. 6 Constructing Paths 766 15. 7 Drawing Rectangles 771 15. 8 Adding Shadows to Shapes 776 Table of Contents | vii www.it-ebooks.info 15. 9 Drawing. iOS 5 Programming Cookbook Vandad Nahavandipoor Beijing • Cambridge • Farnham • Köln • Sebastopol • Tokyo www.it-ebooks.info iOS 5 Programming Cookbook by Vandad Nahavandipoor Revision