ebook img

iOS 8 Swift Programming Cookbook Solutions & Examples for iOS Apps PDF

901 Pages·2014·23.51 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview iOS 8 Swift Programming Cookbook Solutions & Examples for iOS Apps

C o v e r s X iOS 8 Swift Programming Cookbook c o d e Entirely rewritten for Apple’s Swift programming language, this updated “ From coding with new CPiO 6 cookbook helps you overcome the vexing issues you’re likely to face when iOS features to tackling or creating apps for iOS devices. oS complex requirements, o You’ll find hundreds of new and revised recipes for using the iOS 8 SDK, g Vandad's iOS k 8 including techniques for working with Health app data and HomeKit accessories, enhancing and animating graphics, storing and protecting Programming Cookbook br aS data, sending and receiving notifications, and managing files and folders. always provides me with o Each recipe includes sample code on GitHub that you can use right away. the resources I need and mw o good, clear, practical ■ Use CloudKit APIs to store information in the cloud with ease kmi examples. Now that I'm f ■ Create custom keyboards and extensions t starting out using Swift, i ■ Access users’ health-related information with HealthKit n this edition is my Bible—I'd ■ Interact with accessories inside the user’s home with HomeKit have been lost without it.” g ■ Create vibrant and lifelike user interfaces with UIKit Dynamics —João Duarte ■ Use the Keychain to protect your app’s data Senior iOS Software Engineer ■ Develop location-aware and multitasking-aware apps iOS 8 Swift ■ Work with iOS 8’s audio and video APIs ■ Use Event Kit UI to manage calendars, dates, and events ■ Take advantage of the accelerometer and the gyroscope ■ Get working examples for implementing gesture recognizers P rogramming ■ Retrieve and manipulate contacts and groups from the Address Book ■ Determine a camera’s availability and access the Photo Library C ookbook Vandad Nahavandipoor is an iOS and OS X programmer for an international N media group with over 7,000 employees in more than 29 countries. Previously a he worked for Lloyds Banking Group in England to deliver their iOS apps to millions h a of users in the UK. v a n d ip o SOLUTIONS & EXAMPLES FOR IOS APPS o r MOBILE DEVELOPMENT/IOS Twitter: @oreillymedia facebook.com/oreilly US $59.99 CAN $62.99 ISBN: 978-1-491-90869-3 Vandad Nahavandipoor C o v e r s X iOS 8 Swift Programming Cookbook c o d e Entirely rewritten for Apple’s Swift programming language, this updated “ From coding with new CPiO 6 cookbook helps you overcome the vexing issues you’re likely to face when iOS features to tackling or creating apps for iOS devices. oS complex requirements, o You’ll find hundreds of new and revised recipes for using the iOS 8 SDK, g Vandad's iOS k 8 including techniques for working with Health app data and HomeKit accessories, enhancing and animating graphics, storing and protecting Programming Cookbook br aS data, sending and receiving notifications, and managing files and folders. always provides me with o Each recipe includes sample code on GitHub that you can use right away. the resources I need and mw o good, clear, practical ■ Use CloudKit APIs to store information in the cloud with ease kmi examples. Now that I'm f ■ Create custom keyboards and extensions t starting out using Swift, i ■ Access users’ health-related information with HealthKit n this edition is my Bible—I'd ■ Interact with accessories inside the user’s home with HomeKit have been lost without it.” g ■ Create vibrant and lifelike user interfaces with UIKit Dynamics —João Duarte ■ Use the Keychain to protect your app’s data Senior iOS Software Engineer ■ Develop location-aware and multitasking-aware apps iOS 8 Swift ■ Work with iOS 8’s audio and video APIs ■ Use Event Kit UI to manage calendars, dates, and events ■ Take advantage of the accelerometer and the gyroscope ■ Get working examples for implementing gesture recognizers P rogramming ■ Retrieve and manipulate contacts and groups from the Address Book ■ Determine a camera’s availability and access the Photo Library C ookbook Vandad Nahavandipoor is an iOS and OS X programmer for an international N media group with over 7,000 employees in more than 29 countries. Previously a he worked for Lloyds Banking Group in England to deliver their iOS apps to millions h a of users in the UK. v a n d ip o SOLUTIONS & EXAMPLES FOR IOS APPS o r MOBILE DEVELOPMENT/IOS Twitter: @oreillymedia facebook.com/oreilly US $59.99 CAN $62.99 ISBN: 978-1-491-90869-3 Vandad Nahavandipoor iOS 8 Swift Programming Cookbook Vandad Nahavandipoor iOS 8 Swift Programming Cookbook by Vandad Nahavandipoor Copyright © 2015 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://safaribooksonline.com). For more information, contact our corporate/ institutional sales department: 800-998-9938 or [email protected]. Editors: Andy Oram and Rachel Roumeliotis Cover Designer: Ellie Volckhausen Production Editor: Nicole Shelby Interior Designer: David Futato Proofreader: Gillian McGarvey Illustrator: Rebecca Demarest Indexer: Lucie Haskins November 2014: First Edition Revision History for the First Edition: 2014-11-06: First release See http://oreilly.com/catalog/errata.csp?isbn=9781491908693 for release details. The O’Reilly logo is a registered trademark of O’Reilly Media, Inc. iOS 8 Swfit Programming Cookbook, the cover image, 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 the publisher and the author have used good faith efforts to ensure that the information and instruc‐ tions 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 intel‐ lectual property rights of others, it is your responsibility to ensure that your use thereof complies with such licenses and/or rights. ISBN: 978-1-491-90869-3 [Malloy] Table of Contents Preface. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ix 1. The Basics. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 1.1. Adding Blur Effects to Your Views 9 1.2. Presenting Temporary Information on the Screen with Popovers 12 1.3. Displaying Images with UIImageView 17 1.4. Displaying Static Text with UILabel 21 1.5. Adding Buttons to the User Interface with UIButton 27 1.6. Displaying Alerts and Action Sheets 31 1.7. Creating, Using, and Customizing Switches with UISwitch 37 1.8. Picking Values with the UIPickerView 41 1.9. Picking the Date and Time with UIDatePicker 46 1.10. Implementing Range Pickers with UISlider 49 1.11. Grouping Compact Options with UISegmentedControl 53 1.12. Presenting Sharing Options with UIActivityViewController 57 1.13. Presenting Custom Sharing Options with UIActivityViewController 63 1.14. Displaying an Image on a Navigation Bar 68 1.15. Adding Buttons to Navigation Bars Using UIBarButtonItem 70 1.16. Accepting User Text Input with UITextField 75 1.17. Displaying Long Lines of Text with UITextView 83 1.18. Creating Scrollable Content with UIScrollView 88 1.19. Loading Web Pages with WebKit 91 1.20. Loading Web Pages with UIWebView 94 1.21. Displaying Progress with UIProgressView 97 1.22. Creating a Provision Profile 99 2. Extensions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107 2.1. Adding New Photo Editing Capabilities to the Photos App 110 2.2. Providing a Custom Sharing Extension to iOS 118 iii 2.3. Building Custom Keyboards 129 2.4. Creating a Service Within Your App with Action Extensions 137 2.5. Adding Widgets to the Notification Center 144 3. Managing Health Data with HealthKit. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155 3.1. Setting Up Your App for HealthKit 155 3.2. Retrieving and Modifying the User’s Weight Information 160 3.3. Accessing and Modifying the User’s Height Information 165 3.4. Retrieving User Characteristics 172 3.5. Observing Changes to the User’s Health Information 177 3.6. Reading and Modifying the User’s Total Calories Burned 183 3.7. Converting Between Units 196 4. Managing Home Appliances with HomeKit. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201 4.1. Simulating HomeKit Accessories 203 4.2. Managing the User’s Home in HomeKit 212 4.3. Adding Rooms to the User’s Home 219 4.4. Specifying Zones in the User’s Home 224 4.5. Discovering and Managing HomeKit Enabled Accessories 229 4.6. Interacting with HomeKit Accessories 234 4.7. Grouping Services of HomeKit Accessories 242 5. Creating Dynamic and Interactive User Interfaces. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 247 5.1. Adding Gravity to Your UI Components 249 5.2. Detecting and Reacting to Collisions Between UI Components 250 5.3. Animating Your UI Components with a Push 257 5.4. Attaching Multiple Dynamic Items to Each Other 262 5.5. Adding a Dynamic Snap Effect to Your UI Components 266 5.6. Assigning Characteristics to Your Dynamic Effects 269 6. Table and Collection Views. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 275 6.1. Populating a Table View with Data 277 6.2. Enabling Swipe Deletion of Table View Cells 281 6.3. Constructing Headers and Footers in Table Views 282 6.4. Displaying a Refresh Control for Table Views 288 6.5. Providing Basic Content to a Collection View 292 6.6. Feeding Custom Cells to Collection Views Using .xib Files 297 6.7. Handling Events in Collection Views 303 6.8. Providing Header and Footer in a Collection View 307 6.9. Adding Custom Interactions to Collection Views 312 iv | Table of Contents 7. Concurrency and Multitasking. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 315 7.1. Performing UI-Related Tasks 319 7.2. Performing Non-UI Related Tasks 321 7.3. Performing Tasks After a Delay 329 7.4. Performing a Task Only Once 331 7.5. Grouping Tasks Together 332 7.6. Creating Simple Concurrency with Operations 335 7.7. Creating Dependency Between Operations 340 7.8. Firing Periodic Tasks 342 7.9. Completing a Long-Running Task in the Background 345 7.10. Adding Background Fetch Capabilities to Your Apps 348 7.11. Playing Audio in the Background 357 7.12. Handling Location Changes in the Background 360 7.13. Handling Network Connections in the Background 363 8. Security. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 367 8.1. Authenticating the User with Touch ID 373 8.2. Enabling Security and Protection for Your Apps 376 8.3. Storing Values in the Keychain 381 8.4. Finding Values in the Keychain 383 8.5. Updating Existing Values in the Keychain 386 8.6. Deleting Existing Values in the Keychain 390 8.7. Sharing Keychain Data Between Multiple Apps 392 8.8. Writing to and Reading Keychain Data from iCloud 397 8.9. Storing Files Securely in the App Sandbox 399 8.10. Securing Your User Interface 403 9. Core Location, iBeacon, and Maps. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 407 9.1. Detecting Which Floor the User Is on in a Building 407 9.2. Defining and Processing iBeacons 409 9.3. Pinpointing the Location of a Device 415 9.4. Displaying Pins on a Map View 420 9.5. Displaying Custom Pins on a Map View 423 9.6. Searching on a Map View 429 9.7. Displaying Directions on the Map 434 9.8. Customizing the View of the Map with a Camera 440 10. Gesture Recognizers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 445 10.1. Detecting Swipe Gestures 447 10.2. Detecting Rotation Gestures 449 10.3. Detecting Panning and Dragging Gestures 452 10.4. Detecting Long Press Gestures 455 Table of Contents | v 10.5. Detecting Tap Gestures 458 10.6. Detecting Pinch Gestures 460 10.7. Detecting Screen Edge Pan Gestures 462 11. Networking and Sharing. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 465 11.1. Downloading Data Using NSURLSession 465 11.2. Downloading Data in the Background Using NSURLSession 473 11.3. Uploading Data Using NSURLSession 477 11.4. Downloading Asynchronously with NSURLConnection 479 11.5. Handling Timeouts in Asynchronous Connections 483 11.6. Downloading Synchronously with NSURLConnection 484 11.7. Customizing URL Requests 487 11.8. Sending HTTP Requests with NSURLConnection 488 11.9. Serializing and Deserializing JSON Objects 491 11.10. Integrating Social Sharing into Your Apps 495 12. Multimedia. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 499 12.1. Playing Audio Files 499 12.2. Recording Audio 501 12.3. Playing Video Files 509 12.4. Capturing Thumbnails from Video Files 513 12.5. Accessing the Music Library 516 13. Address Book. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 525 13.1. Retrieving a Person Entity with System UI 527 13.2. Retrieving a Property of a Person Entity with System UI 531 13.3. Requesting Access to the Address Book 534 13.4. Retrieving All the People in the Address Book 537 13.5. Retrieving Properties of Address Book Entries 538 13.6. Inserting a Person Entry into the Address Book 541 13.7. Inserting a Group Entry into the Address Book 544 13.8. Adding Persons to Groups 547 13.9. Searching the Address Book 549 13.10. Retrieving and Setting a Person’s Address Book Image 552 14. Files and Folder Management. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 555 14.1. Finding the Paths of the Most Useful Folders on Disk 557 14.2. Writing to and Reading from Files 559 14.3. Creating Folders on Disk 564 14.4. Enumerating Files and Folders 565 14.5. Deleting Files and Folders 571 14.6. Saving Objects to Files 574 vi | Table of Contents 15. Camera and the Photo Library. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 577 15.1. Detecting and Probing the Camera 579 15.2. Taking Photos with the Camera 583 15.3. Taking Videos with the Camera 587 15.4. Storing Photos in the Photo Library 590 15.5. Storing Videos in the Photo Library 595 15.6. Searching for and Retrieving Images and Videos 597 15.7. Reacting to Changes in Images and Videos 602 15.8. Editing Images and Videos Right on the Device 608 16. Notifications. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 615 16.1. Sending Notifications 616 16.2. Listening for and Reacting to Notifications 618 16.3. Listening and Reacting to Keyboard Notifications 621 16.4. Scheduling Local Notifications 630 16.5. Listening for and Reacting to Local Notifications 636 16.6. Handling Local System Notifications 639 16.7. Setting Up Your App for Push Notifications 642 16.8. Delivering Push Notifications to Your App 648 16.9. Reacting to Push Notifications 656 17. Core Data. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 659 17.1. Performing Batch Updates on Core Data 661 17.2. Writing to Core Data 664 17.3. Reading Data from Core Data 666 17.4. Deleting Data from Core Data 668 17.5. Sorting Data in Core Data 670 17.6. Boosting Data Access in Table Views 672 17.7. Implementing Relationships in Core Data 680 17.8. Fetching Data in the Background 686 17.9. Using Custom Data Types in Your Core Data Model 690 18. Dates, Calendars, and Events. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 697 18.1. Constructing Date Objects 698 18.2. Retrieving Date Components 699 18.3. Requesting Permission to Access Calendars 700 18.4. Retrieving Calendar Groups on an iOS Device 705 18.5. Adding Events to Calendars 707 18.6. Accessing the Contents of Calendars 712 18.7. Removing Events from Calendars 714 18.8. Adding Recurring Events to Calendars 718 18.9. Retrieving the Attendees of an Event 722 Table of Contents | vii 18.10. Adding Alarms to Calendars 725 19. Graphics and Animations. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 729 19.1. Drawing Text 734 19.2. Drawing Images 736 19.3. Constructing Resizable Images 739 19.4. Drawing Lines 745 19.5. Constructing Paths 751 19.6. Drawing Rectangles 755 19.7. Adding Shadows to Shapes 759 19.8. Drawing Gradients 764 19.9. Transforming Views 770 19.10. Animating Views 776 20. Core Motion. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 787 20.1. Retrieving Altitude Data 788 20.2. Retrieving Pedometer Data 790 20.3. Detecting the Availability of an Accelerometer 794 20.4. Detecting the Availability of a Gyroscope 796 20.5. Retrieving Accelerometer Data 797 20.6. Detecting Shakes on an iOS Device 801 20.7. Retrieving Gyroscope Data 802 21. Cloud. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 805 21.1. Setting Up Your App for CloudKit 807 21.2. Storing Data with CloudKit 812 21.3. Retrieving Data with CloudKit 820 21.4. Querying the Cloud with CloudKit 826 21.5. Observing Changes to Records in CloudKit 831 21.6. Retrieving User Information from CloudKit 839 21.7. Storing and Synchronizing Dictionaries in iCloud 846 21.8. Creating and Managing Files and Folders in iCloud 851 21.9. Searching for Files and Folders in iCloud 855 Index. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 863 viii | Table of Contents

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.