iOS 5 Programming Cookbook phần 1 ppsx

90 462 0
iOS 5 Programming Cookbook phần 1 ppsx

Đ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

iOS 5 Programming Cookbook Vandad Nahavandipoor Beijing • Cambridge • Farnham • Köln • Sebastopol • Tokyo iOS 5 Programming 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 To Agnieszka Marta Dybowska. 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 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 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 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 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 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 [...]... instructions: 1 Select the Window menu 2 Under the Window menu, select Organizer 3 On top of Organizer, make sure the Devices item is selected, as shown in Figure 1- 14 1 .5 Running iOS Apps on iOS Devices | 17 Figure 1- 14 Selecting the Devices button in Organizer 4 On the left side of Organizer's Devices screen, make sure you've selected your device by clicking on it (Figure 1- 15 ) Figure 1- 15 A device... in Figure 1- 4 4 | Chapter 1:  The Basics Figure 1- 4 Running 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 Figure 1 -5 The Run menu item in Xcode Congratulations Now you have a simple app running in iOS Simulator... interface As simple as that (Figure 1- 9) 10 | Chapter 1:  The Basics Figure 1- 9 A button on a nib Right after this, from the Xcode menus, select File and then Save to make sure your Understanding_Interface_BuilderViewController_iPhone.xib is saved Then go ahead and run your app on iOS Simulator (Figure 1- 10) 1. 2 Understanding Interface Builder | 11 Figure 1- 10 A button on the UI of your app You might... take advantage of Objective-C’s distinctive memory management model 1 Figure 1- 1 The New Project dialog in Xcode 1. 1 Creating a 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... Interface Builder See Also XXX 12 | Chapter 1:  The Basics 1. 3 Compiling iOS Apps Problem You have learned how to create an iOS app and wonder how it behaves Solution Compile and run your iOS apps using Apple's latest compiler Then test your app on iOS Simulator and also, preferrably, on a device Discussion Creating an iOS App can be categorized under the following tasks: 1 2 3 4 5 Planning Prototyping Designing... currently selected So if I click on the left side of the Scheme button, I'll see something similar to Figure 1- 12 So go ahead and click on the left side of the Scheme button and select the project that you wish to run on the iOS Simulator 1. 4 Running iOS Apps on the Simulator | 15 Figure 1- 12 The currently-selected project 3 The right side of the Scheme button lets you choose which device/simulator... author 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- 0 51 5 (international or local) (707) 829- 010 4 (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/97 814 493882 25 There is also a companion website to... Figure 1- 2 Leave the rest of the values in this screen just the way I've left them in Figure 1- 2 and press the Next button Figure 1- 2 Setting the new project's settings 5 You will now be asked to save your project on a disk Select your desired location and press the Create button, as shown in Figure 1- 3 Xcode will now create your project files and the structure of your project 1. 1 Creating a Simple iOS. .. tools Now you want to create an iOS Project and run that App on the iOS Simulator This process is really straightforward: 1 Open Xcode if you don't have it open yet 2 Select File on the menu bar, select New, and then select New Project You will be greeted with a screen similar to that shown in Figure 1- 1 2 | Chapter 1:  The Basics 3 In the New Project dialog (Figure 1- 1), on the left, make sure the Application... mistakes that make your apps unstable See Also XXX 14 | Chapter 1:  The Basics 1. 4 Running iOS Apps on the Simulator Problem You've prepared an iOS project in Xcode and would like to run it in iOS Simulator to make sure it works Solution You need to use the Scheme breadcrumb button on top-left corner of Xcode to first select the project that you wish to run on the iOS Simulator and then select the simulator . . . . . . 58 1 11. 1 Detecting and Probing the Camera 58 3 11 .2 Taking Photos with the Camera 58 8 11 .3 Taking Videos with the Camera 59 2 11 .4 Storing Photos in the Photo Library 59 5 11 .5 Storing. 749 15 . 4 Drawing Images 754 15 . 5 Drawing Lines 759 15 . 6 Constructing Paths 766 15 . 7 Drawing Rectangles 7 71 15 . 8 Adding Shadows to Shapes 776 Table of Contents | vii 15 . 9 Drawing Gradients 783 15 . 10 . 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

Ngày đăng: 13/08/2014, 18:20

Từ khóa liên quan

Mục lục

  • Table of Contents

  • Preface

    • Audience

    • Organization of This Book

    • Additional Resources

    • Conventions Used in This Book

    • Using Code Examples

    • We’d Like to Hear from You

    • Safari® Books Online

    • Acknowledgments

    • Chapter€1.€The Basics

      • 1.0€ Introduction

      • 1.1€ Creating a Simple iOS App in Xcode

        • Problem

        • Solution

        • Discussion

        • See Also

        • 1.2€ Understanding Interface Builder

          • Problem

          • Solution

          • Discussion

          • See Also

          • 1.3€ Compiling iOS Apps

            • Problem

            • Solution

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

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

Tài liệu liên quan