logo资料库

iOS.10.Swift.Programming.Cookbook.2016.12.pdf

第1页 / 共455页
第2页 / 共455页
第3页 / 共455页
第4页 / 共455页
第5页 / 共455页
第6页 / 共455页
第7页 / 共455页
第8页 / 共455页
资料共455页,剩余部分请下载后查看
Cover
Copyright
Table of Contents
Preface
Audience
Organization of This Book
Additional Resources
Using Code Examples
O’Reilly Safari
How to Contact Us
Acknowledgments
Chapter 1. iMessage Stickers and Apps
1.1 Setting Up a Sticker Pack Application
Problem
Solution
Discussion
See Also
1.2 Adjusting Sticker Sizes
Problem
Solution
Discussion
See Also
1.3 Building a Full-Fledged iMessage Application
Problem
Solution
Discussion
See Also
1.4 Adding an iMessage App Extension to an Existing App
Problem
Solution
Discussion
See Also
1.5 Utilizing an Expanded View in a Sticker Pack App
Problem
Solution
Discussion
See Also
1.6 Appending Rich Information to Stickers
Problem
Solution
Discussion
See Also
1.7 Creating Interactive Conversations with iMessage Apps
Problem
Solution
Discussion
See Also
Chapter 2. SiriKit
2.1 Setting Up Your Project for Siri
Problem
Solution
Discussion
See Also
2.2 Defining an Intent Handler
Problem
Solution
Discussion
See Also
2.3 Resolving Ambiguity in an Intent
Problem
Solution
Discussion
See Also
2.4 Reporting Progress for Resolving an Intent
Problem
Solution
Discussion
See Also
2.5 Handling an Intent
Problem
Solution
Discussion
See Also
Chapter 3. Measurements and Units
3.1 Converting Between and Working with Length Units
Problem
Solution
Discussion
See Also
3.2 Working with and Switching Between Angle Units
Problem
Solution
Discussion
See Also
3.3 Representing and Converting Between Durations of Time
Problem
Solution
Discussion
See Also
3.4 Using and Working with Frequency Units
Problem
Solution
Discussion
See Also
3.5 Working with and Using Power Units
Problem
Solution
Discussion
See Also
3.6 Representing and Comparing Temperature Units
Problem
Solution
Discussion
See Also
3.7 Working with and Converting Volume Units
Problem
Solution
Discussion
See Also
Chapter 4. Core Data
4.1 Designing Your Database Scheme
Problem
Solution
Discussion
See Also
4.2 Writing Data to the Database
Problem
Solution
Discussion
See Also
4.3 Reading Data from the Database
Problem
Solution
Discussion
See Also
4.4 Searching for Data in the Database
Problem
Solution
Discussion
See Also
4.5 Performing Background Tasks with Core Data
Problem
Solution
Discussion
See Also
Chapter 5. Swift 3.0, Xcode 8, and Interface Builder
5.1 Handling Errors in Swift
Problem
Solution
Discussion
See Also
5.2 Specifying Preconditions for Methods
Problem
Solution
Discussion
See Also
5.3 Ensuring the Execution of Code Blocks Before Exiting Methods
Problem
Solution
Discussion
5.4 Checking for API Availability
Problem
Solution
Discussion
See Also
5.5 Categorizing and Downloading Assets to Get Smaller Binaries
Problem
Solution
Discussion
5.6 Exporting Device-Specific Binaries
Problem
Solution
Discussion
5.7 Linking Separate Storyboards Together
Problem
Solution
Discussion
See Also
5.8 Adding Multiple Buttons to the Navigation Bar
Problem
Solution
Discussion
See Also
5.9 Optimizing Your Swift Code
Problem
Solution
Discussion
See Also
5.10 Showing the Header View of Your Swift Classes
Problem
Solution
Discussion
See Also
5.11 Creating Your Own Set Types
Problem
Solution
Discussion
See Also
5.12 Conditionally Extending a Type
Problem
Solution
Discussion
See Also
5.13 Building Equality Functionality into Your Own Types
Problem
Solution
Discussion
See Also
5.14 Looping Conditionally Through a Collection
Problem
Solution
Discussion
5.15 Designing Interactive Interface Objects in Playgrounds
Problem
Solution
Discussion
5.16 Grouping Switch Statement Cases Together
Problem
Solution
Discussion
5.17 Bundling and Reading Data in Your Apps
Problem
Solution
Discussion
Chapter 6. The User Interface
6.1 Animating Views
Problem
Solution
Discussion
6.2 Attaching Live Views to Playgrounds
Problem
Solution
Discussion
6.3 Running Playgrounds as Interactive and Continuous Apps
Problem
Solution
Discussion
6.4 Arranging Your Components Horizontally or Vertically
Problem
Solution
Discussion
6.5 Customizing Stack Views for Different Screen Sizes
Problem
Solution
Discussion
6.6 Creating Anchored Constraints in Code
Problem
Solution
Discussion
6.7 Allowing Users to Enter Text in Response to Local and Remote Notifications
Problem
Solution
Discussion
See Also
6.8 Dealing with Stacked Views in Code
Problem
Solution
Discussion
6.9 Showing Web Content in Safari View Controller
Problem
Solution
Discussion
6.10 Laying Out Text-Based Content on Your Views
Problem
Solution
Discussion
6.11 Improving Touch Rates for Smoother UI Interactions
Problem
Solution
Discussion
6.12 Supporting Right-to-Left Languages
Problem
Solution
Discussion
6.13 Associating Keyboard Shortcuts with View Controllers
Problem
Solution
Discussion
6.14 Recording the Screen and Sharing the Video
Problem
Solution
Discussion
Chapter 7. Apple Watch
7.1 Downloading Files onto the Apple Watch
Problem
Solution
Discussion
7.2 Noticing Changes in Pairing State Between the iOS and Watch Apps
Problem
Solution
Discussion
7.3 Transferring Small Pieces of Data to and from the Watch
Problem
Solution
Discussion
7.4 Transferring Dictionaries in Queues to and from the Watch
Problem
Solution
Discussion
7.5 Transferring Files to and from the Watch
Problem
Solution
Discussion
See Also
7.6 Communicating Interactively Between iOS and watchOS
Problem
Solution
Discussion
See Also
7.7 Setting Up Apple Watch for Custom Complications
Problem
Solution
Discussion
See Also
7.8 Constructing Small Complications with Text and Images
Problem
Solution
Discussion
7.9 Displaying Time Offsets in Complications
Problem
Solution
Discussion
See Also
7.10 Displaying Dates in Complications
Problem
Solution
Discussion
See Also
7.11 Displaying Times in Complications
Problem
Solution
Discussion
See Also
7.12 Displaying Time Intervals in Complications
Problem
Solution
Discussion
See Also
7.13 Recording Audio in Your Watch App
Problem
Solution
Discussion
See Also
7.14 Playing Local and Remote Audio and Video in Your Watch App
Problem
Solution
Discussion
See Also
Chapter 8. Contacts
8.1 Creating Contacts
Problem
Solution
Discussion
8.2 Searching for Contacts
Problem
Solution
Discussion
See Also
8.3 Updating Contacts
Problem
Solution
Discussion
8.4 Deleting Contacts
Problem
Solution
Discussion
8.5 Formatting Contact Data
Problem
Solution
Discussion
See Also
8.6 Picking Contacts with the Prebuilt System UI
Problem
Solution
Discussion
See Also
8.7 Creating Contacts with a Prebuilt System UI
Problem
Solution
Discussion
See Also
8.8 Displaying Contacts with a Prebuilt System UI
Problem
Solution
Discussion
See Also
Chapter 9. Extensions
9.1 Creating Safari Content Blockers
Problem
Solution
Discussion
9.2 Creating Shared Links for Safari
Problem
Solution
Discussion
9.3 Maintaining Your App’s Indexed Content
Problem
Solution
Discussion
Chapter 10. Web and Search
10.1 Making Your App’s Content Searchable
Problem
Solution
Discussion
10.2 Making User Activities Searchable
Problem
Solution
Discussion
See Also
10.3 Deleting Your App’s Searchable Content
Problem
Solution
Discussion
See Also
Chapter 11. Multitasking
11.1 Supporting Split Views
Problem
Solution
Discussion
11.2 Adding Picture in Picture Playback Functionality
Problem
Solution
Discussion
11.3 Handling Low Power Mode and Providing Alternatives
Problem
Solution
Discussion
Chapter 12. Maps and Location
12.1 Displaying a Specific Location on the Map
Problem
Solution
Discussion
12.2 Requesting the User’s Location a Single Time
Problem
Solution
Discussion
See Also
12.3 Requesting the User’s Location in the Background
Problem
Solution
Discussion
See Also
12.4 Customizing the Tint Color of Pins on the Map
Problem
Solution
Discussion
12.5 Providing Detailed Pin Information with Custom Views
Problem
Solution
Discussion
12.6 Displaying Traffic, Scale, and Compass Indicators on the Map
Problem
Solution
Discussion
12.7 Providing an ETA for Transit Transport Type
Problem
Solution
Discussion
See Also
12.8 Launching the iOS Maps App in Transit Mode
Problem
Solution
Discussion
See Also
12.9 Showing Maps in Flyover Mode
Problem
Solution
Discussion
Chapter 13. UI Testing
13.1 Preparing Your Project for UI Testing
Problem
Solution
Discussion
13.2 Automating UI Test Scripts
Problem
Solution
Discussion
See Also
13.3 Testing Text Fields, Buttons, and Labels
Problem
Solution
Discussion
See Also
13.4 Finding UI Components
Problem
Solution
Discussion
See Also
13.5 Long-Pressing on UI Elements
Problem
Solution
Discussion
See Also
13.6 Typing Inside Text Fields
Problem
Solution
Discussion
See Also
13.7 Swiping on UI Elements
Problem
Solution
Discussion
See Also
13.8 Tapping UI Elements
Problem
Solution
Discussion
See Also
Chapter 14. Core Motion
14.1 Querying Pace and Cadence Information
Problem
Solution
Discussion
14.2 Recording and Reading Accelerometer Data
Problem
Solution
Discussion
Chapter 15. Security
15.1 Protecting Your Network Connections with ATS
Problem
Solution
Discussion
15.2 Binding Keychain Items to Passcode and Touch ID
Problem
Solution
Discussion
See Also
15.3 Opening URLs Safely
Problem
Solution
Discussion
15.4 Authenticating the User with Touch ID and Timeout
Problem
Solution
Discussion
See Also
Chapter 16. Multimedia
16.1 Reading Out Text with the Default Siri Alex Voice
Problem
Solution
Discussion
16.2 Downloading and Preparing Remote Media for Playback
Problem
Solution
Discussion
16.3 Enabling Spoken Audio Sessions
Problem
Solution
Discussion
Chapter 17. UI Dynamics
17.1 Adding a Radial Gravity Field to Your UI
Problem
Solution
Discussion
17.2 Creating a Linear Gravity Field on Your UI
Problem
Solution
Discussion
17.3 Creating Turbulence Effects with Animations
Problem
Solution
Discussion
17.4 Adding Animated Noise Effects to Your UI
Problem
Solution
Discussion
17.5 Creating a Magnetic Effect Between UI Components
Problem
Solution
Discussion
17.6 Designing a Velocity Field on Your UI
Problem
Solution
Discussion
17.7 Handling Collisions Between Nonrectangular Views
Problem
Solution
Discussion
Index
About the Author
C o a v n d X ers S co d e 8 wift 3 iOS 10 Swift Programming Cookbook SOLUTIONS & EXAMPLES FOR IOS APPS Vandad Nahavandipoor
iOS 10 Swift Programming Cookbook Solutions and Examples for iOS Apps Vandad Nahavandipoor
iOS 10 Swift Programming Cookbook by Vandad Nahavandipoor Copyright © 2017 Vandad Nahavandipoor. 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://www.oreilly.com/safari). For more information, contact our corporate/ institutional sales department: 800-998-9938 or corporate@oreilly.com. Editor: Rachel Roumeliotis Production Editor: Shiny Kalapurakkel Copyeditor: Jasmine Kwityn Proofreader: Rachel Monaghan Indexer: Judy McConville Interior Designer: David Futato Cover Designer: Karen Montgomery Illustrator: Rebecca Panzer December 2016: First Edition Revision History for the First Edition 2016-12-01: First Release See http://oreilly.com/catalog/errata.csp?isbn=9781491966433 for release details. The O’Reilly logo is a registered trademark of O’Reilly Media, Inc. iOS 10 Swift Programming Cookbook, the cover image, and related trade dress are trademarks of O’Reilly Media, Inc. While the publisher and the author have used good faith efforts to ensure that the information and instructions contained in this work are accurate, the publisher and the author disclaim all responsibility for errors or omissions, including without limitation responsibility for damages resulting from the use of or reliance on this work. Use of the information and instructions contained in this work is at your own risk. If any code samples or other technology this work contains or describes is subject to open source licenses or the intellectual property rights of others, it is your responsibility to ensure that your use thereof complies with such licenses and/or rights. 978-1-491-96643-3 [LSI]
Table of Contents Preface. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ix 1. iMessage Stickers and Apps. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 1.1 Setting Up a Sticker Pack Application 2 1.2 Adjusting Sticker Sizes 4 1.3 Building a Full-Fledged iMessage Application 6 1.4 Adding an iMessage App Extension to an Existing App 15 1.5 Utilizing an Expanded View in a Sticker Pack App 16 1.6 Appending Rich Information to Stickers 24 1.7 Creating Interactive Conversations with iMessage Apps 27 2. SiriKit. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37 2.1 Setting Up Your Project for Siri 37 2.2 Defining an Intent Handler 44 2.3 Resolving Ambiguity in an Intent 52 2.4 Reporting Progress for Resolving an Intent 60 2.5 Handling an Intent 62 3. Measurements and Units. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65 3.1 Converting Between and Working with Length Units 65 3.2 Working with and Switching Between Angle Units 67 3.3 Representing and Converting Between Durations of Time 68 3.4 Using and Working with Frequency Units 70 3.5 Working with and Using Power Units 72 3.6 Representing and Comparing Temperature Units 73 3.7 Working with and Converting Volume Units 74 iii
4. Core Data. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77 4.1 Designing Your Database Scheme 78 4.2 Writing Data to the Database 83 4.3 Reading Data from the Database 85 4.4 Searching for Data in the Database 88 4.5 Performing Background Tasks with Core Data 91 5. Swift 3.0, Xcode 8, and Interface Builder. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95 5.1 Handling Errors in Swift 95 5.2 Specifying Preconditions for Methods 97 5.3 Ensuring the Execution of Code Blocks Before Exiting Methods 98 5.4 Checking for API Availability 100 5.5 Categorizing and Downloading Assets to Get Smaller Binaries 101 5.6 Exporting Device-Specific Binaries 105 5.7 Linking Separate Storyboards Together 106 5.8 Adding Multiple Buttons to the Navigation Bar 107 5.9 Optimizing Your Swift Code 108 5.10 Showing the Header View of Your Swift Classes 112 5.11 Creating Your Own Set Types 113 5.12 Conditionally Extending a Type 115 5.13 Building Equality Functionality into Your Own Types 117 5.14 Looping Conditionally Through a Collection 118 5.15 Designing Interactive Interface Objects in Playgrounds 120 5.16 Grouping Switch Statement Cases Together 122 5.17 Bundling and Reading Data in Your Apps 123 6. The User Interface. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129 6.1 Animating Views 129 6.2 Attaching Live Views to Playgrounds 133 6.3 Running Playgrounds as Interactive and Continuous Apps 136 6.4 Arranging Your Components Horizontally or Vertically 137 6.5 Customizing Stack Views for Different Screen Sizes 139 6.6 Creating Anchored Constraints in Code 143 6.7 Allowing Users to Enter Text in Response to Local and Remote Notifications 148 6.8 Dealing with Stacked Views in Code 152 6.9 Showing Web Content in Safari View Controller 154 6.10 Laying Out Text-Based Content on Your Views 155 6.11 Improving Touch Rates for Smoother UI Interactions 156 6.12 Supporting Right-to-Left Languages 159 6.13 Associating Keyboard Shortcuts with View Controllers 164 6.14 Recording the Screen and Sharing the Video 165 iv | Table of Contents
7. Apple Watch. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173 7.1 Downloading Files onto the Apple Watch 175 7.2 Noticing Changes in Pairing State Between the iOS and Watch Apps 180 7.3 Transferring Small Pieces of Data to and from the Watch 184 7.4 Transferring Dictionaries in Queues to and from the Watch 193 7.5 Transferring Files to and from the Watch 198 7.6 Communicating Interactively Between iOS and watchOS 203 7.7 Setting Up Apple Watch for Custom Complications 213 7.8 Constructing Small Complications with Text and Images 220 7.9 Displaying Time Offsets in Complications 231 7.10 Displaying Dates in Complications 239 7.11 Displaying Times in Complications 245 7.12 Displaying Time Intervals in Complications 251 7.13 Recording Audio in Your Watch App 258 7.14 Playing Local and Remote Audio and Video in Your Watch App 261 8. Contacts. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 265 8.1 Creating Contacts 266 8.2 Searching for Contacts 272 8.3 Updating Contacts 277 8.4 Deleting Contacts 282 8.5 Formatting Contact Data 283 8.6 Picking Contacts with the Prebuilt System UI 288 8.7 Creating Contacts with a Prebuilt System UI 295 8.8 Displaying Contacts with a Prebuilt System UI 297 9. Extensions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 301 9.1 Creating Safari Content Blockers 301 9.2 Creating Shared Links for Safari 306 9.3 Maintaining Your App’s Indexed Content 309 10. Web and Search. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 315 10.1 Making Your App’s Content Searchable 315 10.2 Making User Activities Searchable 319 10.3 Deleting Your App’s Searchable Content 323 11. Multitasking. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 325 11.1 Supporting Split Views 325 11.2 Adding Picture in Picture Playback Functionality 328 11.3 Handling Low Power Mode and Providing Alternatives 335 Table of Contents | v
12. Maps and Location. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 339 12.1 Displaying a Specific Location on the Map 339 12.2 Requesting the User’s Location a Single Time 342 12.3 Requesting the User’s Location in the Background 344 12.4 Customizing the Tint Color of Pins on the Map 346 12.5 Providing Detailed Pin Information with Custom Views 349 12.6 Displaying Traffic, Scale, and Compass Indicators on the Map 350 12.7 Providing an ETA for Transit Transport Type 352 12.8 Launching the iOS Maps App in Transit Mode 356 12.9 Showing Maps in Flyover Mode 357 13. UI Testing. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 359 13.1 Preparing Your Project for UI Testing 359 13.2 Automating UI Test Scripts 362 13.3 Testing Text Fields, Buttons, and Labels 365 13.4 Finding UI Components 367 13.5 Long-Pressing on UI Elements 370 13.6 Typing Inside Text Fields 372 13.7 Swiping on UI Elements 374 13.8 Tapping UI Elements 376 14. Core Motion. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 379 14.1 Querying Pace and Cadence Information 380 14.2 Recording and Reading Accelerometer Data 381 15. Security. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 383 15.1 Protecting Your Network Connections with ATS 383 15.2 Binding Keychain Items to Passcode and Touch ID 385 15.3 Opening URLs Safely 387 15.4 Authenticating the User with Touch ID and Timeout 389 16. Multimedia. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 393 16.1 Reading Out Text with the Default Siri Alex Voice 393 16.2 Downloading and Preparing Remote Media for Playback 395 16.3 Enabling Spoken Audio Sessions 398 17. UI Dynamics. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 401 17.1 Adding a Radial Gravity Field to Your UI 401 17.2 Creating a Linear Gravity Field on Your UI 407 17.3 Creating Turbulence Effects with Animations 411 17.4 Adding Animated Noise Effects to Your UI 412 17.5 Creating a Magnetic Effect Between UI Components 415 vi | Table of Contents
分享到:
收藏