Developing C# Apps for iPhone and iPad Using MonoTouch iOS Apps Development for .NET Developers ■ ■ ■ Bryan Costanich i Developing C# Apps for iPhone and iPad Using MonoTouch: iOS Apps Development for .NET Developers Copyright © 2011 by Bryan Costanich All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or by any information storage or retrieval system, without the prior written permission of the copyright owner and the publisher. ISBN-13 (pbk): 978-1-4302-3174-5 ISBN-13 (electronic): 978-1-4302-3175-2 Trademarked names, logos, and images may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, logo, or image we use the names, logos, and images only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. The use in this publication of trade names, trademarks, service marks, and similar terms, even if they are not identified as such, is not to be taken as an expression of opinion as to whether or not they are subject to proprietary rights. President and Publisher: Paul Manning Lead Editor: Steve Anglin Development Editor: Matthew Moodie Technical Reviewer: Geoff Norton Editorial Board: Steve Anglin, Mark Beckner, Ewan Buckingham, Gary Cornell, Jonathan Gennick, Jonathan Hassell, Michelle Lowman, James Markham, Matthew Moodie, Jeff Olson, Jeffrey Pepper, Frank Pohlmann, Douglas Pundick, Ben Renow-Clarke, Dominic Shakeshaft, Matt Wade, Tom Welsh Coordinating Editor: Adam Heath Copy Editor: Tracy Brown Compositor: MacPS, LLC Indexer: BIM Indexing & Proofreading Services Artist: April Milne Cover Designer: Anna Ishchenko Distributed to the book trade worldwide by Springer Science+Business Media, LLC., 233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail [email protected], or visit www.springeronline.com. For information on translations, please e-mail [email protected], or visit www.apress.com. Apress and friends of ED books may be purchased in bulk for academic, corporate, or promotional use. eBook versions and licenses are also available for most titles. For more information, reference our Special Bulk Sales–eBook Licensing web page at www.apress.com/bulk-sales. The information in this book is distributed on an “as is” basis, without warranty. Although every precaution has been taken in the preparation of this work, neither the author(s) nor Apress shall have any liability to any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly by the information contained in this work. The source code for this book is available to readers at www.apress.com. You will need to answer questions pertaining to this book in order to successfully download the code. Life is a beautiful mess. Laugh or cry; choose one. Contents at a Glance Contents .............................................................................................................. v About the Author ............................................................................................... xiv About the Technical Reviewer ........................................................................... xv Acknowledgments ............................................................................................ xvi Preface ............................................................................................................ xvii(cid:2) ■Chapter 1: Getting Started with MonoTouch ................................................... 1(cid:2) ■Chapter 2: Our First Application .................................................................... 11(cid:2) ■Chapter 3: Creating Multi-Screen Applications Using the MVC Pattern ........ 43(cid:2) ■Chapter 4: iPad and Universal (iPhone/iPad) Applications ........................... 59(cid:2) ■Chapter 5: More on Views and Controllers .................................................... 73(cid:2) ■Chapter 6: Introduction to Controls ............................................................... 93(cid:2) ■Chapter 7: Standard Controls ...................................................................... 107(cid:2) ■Chapter 8: Content Controls ........................................................................ 155(cid:2) ■Chapter 9: Working with Tables .................................................................. 193(cid:2) ■Chapter 10: Working with Keyboards .......................................................... 223(cid:2) ■Chapter 11: Multitasking ............................................................................. 233(cid:2) ■Chapter 12: Working with Touch ................................................................. 247(cid:2) ■Chapter 13: Working with Shared Resources .............................................. 271(cid:2) ■Chapter 14: User and Application Settings .................................................. 303(cid:2) ■Chapter 15: Working with CoreLocation ...................................................... 321(cid:2) ■Chapter 16: Drawing with CoreGraphics ..................................................... 331(cid:2) ■Chapter 17: Core Animation ........................................................................ 361(cid:2) ■Chapter 18: Notifications ............................................................................. 377(cid:2) ■Chapter 19: Working with Data ................................................................... 393(cid:2) ■Chapter 20: Publishing to the App Store ..................................................... 407(cid:2) ■Chapter 21: Third-Party Libraries ................................................................ 419(cid:2) ■Chapter 22: Using Objective-C Libraries and Code ...................................... 433(cid:2) Index ............................................................................................................... 463 iv Contents Contents at a Glance .......................................................................................... iv(cid:2) About the Author ............................................................................................... xiv(cid:2) About the Technical Reviewer ........................................................................... xv(cid:2) Acknowledgments ............................................................................................ xvi(cid:2) Preface ............................................................................................................ xvii(cid:2) ■Chapter 1: Getting Started with MonoTouch ................................................... 1(cid:2) (cid:2) Developing for the iPhone and iPad ........................................................................................................................ 2 (cid:2) Limited Multitasking .......................................................................................................................................... 2 (cid:2) Limited System Resources ................................................................................................................................ 2 (cid:2) Small Screen Size .............................................................................................................................................. 3 (cid:2) Device-specific Technology ............................................................................................................................... 3 (cid:2) Constrained Response Time .............................................................................................................................. 3 (cid:2) Constrained Access ........................................................................................................................................... 3 (cid:2) MonoTouch Background ......................................................................................................................................... 4 (cid:2) iPhone vs. iPod Touch vs iPad? ......................................................................................................................... 4 (cid:2) How Does It Work? ............................................................................................................................................. 4 (cid:2) How Do I Build a User-Interface (UI); Can I Use Silverlight? .............................................................................. 4 (cid:2) How Do I Distribute My Apps? ........................................................................................................................... 5 (cid:2) What Is the Licensing Model? ............................................................................................................................ 5 (cid:2) Are There Any Limitations of MonoTouch? ........................................................................................................ 6 (cid:2) Getting Started ........................................................................................................................................................ 7 (cid:2) Mac Computer Running Snow Leopard ............................................................................................................. 7 (cid:2) Apple’s iOS SDK ................................................................................................................................................. 7 (cid:2) Mono for OSX ..................................................................................................................................................... 7 (cid:2) MonoTouch SDK ................................................................................................................................................. 8 (cid:2) Text Editor or Integrated Development Environment ......................................................................................... 8 (cid:2) Documentation ................................................................................................................................................... 8 (cid:2) Resources .......................................................................................................................................................... 9 (cid:2) 3rd Party Libraries ............................................................................................................................................... 9 (cid:2) Summary ................................................................................................................................................................ 9 v ■ CONTENTS ■Chapter 2: Our First Application .................................................................... 11(cid:2) (cid:2) Starting the Example ............................................................................................................................................ 11 (cid:2) Create a New Solution ..................................................................................................................................... 13 (cid:2) Examining the Code ......................................................................................................................................... 15 (cid:2) Interface Builder ................................................................................................................................................... 16 (cid:2) Exploring Interface Builder .............................................................................................................................. 17 (cid:2) Building the Interface ....................................................................................................................................... 22 (cid:2) Outlets ................................................................................................................................................................... 23 (cid:2) Outlets Exposed to Code .................................................................................................................................. 27 (cid:2) Running the Application ................................................................................................................................... 30 (cid:2) Actions .................................................................................................................................................................. 32 (cid:2) Adding an Action .............................................................................................................................................. 32 (cid:2) Actions in Code ................................................................................................................................................ 34 (cid:2) Choosing Which Device to Simulate ..................................................................................................................... 35 (cid:2) Application Name and Icons ................................................................................................................................. 37 (cid:2) Application Name ............................................................................................................................................. 38 (cid:2) Application Icons .............................................................................................................................................. 39 (cid:2) Summary .............................................................................................................................................................. 42 ■Chapter 3: Creating Multi-Screen Applications Using the MVC Pattern ........ 43(cid:2) (cid:2) Model-View-Controller (MVC) Pattern ................................................................................................................... 43 (cid:2) Model ............................................................................................................................................................... 44 (cid:2) View ................................................................................................................................................................. 44 (cid:2) Controller ......................................................................................................................................................... 45 (cid:2) Benefits of the MVC Pattern .................................................................................................................................. 45 (cid:2) Views and View Controllers in MonoTouch ........................................................................................................... 45 (cid:2) Sample Application ............................................................................................................................................... 47 (cid:2) Adding the Navigation Controller to the Main Screen ...................................................................................... 48 (cid:2) HelloWorld and HelloUniverse Screens ............................................................................................................ 51 (cid:2) Showing Different Screens .............................................................................................................................. 53 (cid:2) Summary .............................................................................................................................................................. 57 ■Chapter 4: iPad and Universal (iPhone/iPad) Applications ........................... 59(cid:2) (cid:2) A Note About Resolution on the iPhone ................................................................................................................ 60 (cid:2) Creating an iPad-Only Application ........................................................................................................................ 60 (cid:2) Changing Common Settings in MonoDevelop .................................................................................................. 61 (cid:2) iPad Screens in Interface Builder .................................................................................................................... 62 (cid:2) Creating a Universal iPhone/iPad Application ....................................................................................................... 64 (cid:2) Method 1: Universal Project Template ............................................................................................................. 65 (cid:2) Method 2: Programmatic Device Detection ..................................................................................................... 67 (cid:2) Summary .............................................................................................................................................................. 72 ■Chapter 5: More on Views and Controllers .................................................... 73(cid:2) (cid:2) Custom UIViewController and UIView Implementations ........................................................................................ 73 (cid:2) Default UIView Constructor for a View Defined in a xib file ............................................................................. 74 (cid:2) Manually Loading a UIView Defined in a .xib File ............................................................................................ 75 (cid:2) UIViewController Event Lifecycle ..................................................................................................................... 75 (cid:2) UIView Event Lifecycle ..................................................................................................................................... 77 (cid:2) Managing More than One View in a Controller ..................................................................................................... 78 (cid:2) Switching Views .............................................................................................................................................. 78 vi ■ CONTENTS (cid:2) Animating the Transition .................................................................................................................................. 80 (cid:2) Handling Rotation ................................................................................................................................................. 80 (cid:2) Rotation Lifecycle Events in UIViewController ................................................................................................. 83 (cid:2) General Approaches to Rotation ...................................................................................................................... 84 (cid:2) Rotation Review ............................................................................................................................................... 91 (cid:2) Summary .............................................................................................................................................................. 91 ■Chapter 6: Introduction to Controls ............................................................... 93(cid:2) (cid:2) Coordinate System ................................................................................................................................................ 93 (cid:2) Frame ............................................................................................................................................................... 95 (cid:2) Autosizing ............................................................................................................................................................. 95 (cid:2) Working with Fonts ............................................................................................................................................... 96 (cid:2) Font Sizes ........................................................................................................................................................ 97 (cid:2) Enumerating Fonts ........................................................................................................................................... 97 (cid:2) Tags ...................................................................................................................................................................... 98 (cid:2) Control States ....................................................................................................................................................... 98 (cid:2) Working with Images ............................................................................................................................................ 99 (cid:2) FromFile vs. FromBundle ............................................................................................................................... 100 (cid:2) Which One? .................................................................................................................................................... 102 (cid:2) Subclassing (Creating Custom Controls) ............................................................................................................. 102 (cid:2) Necessary Constructors ................................................................................................................................. 102 (cid:2) Registering Your Controls .............................................................................................................................. 102 (cid:2) Using Your Controls in Interface Builder ........................................................................................................ 103 (cid:2) Prototypes vs. Delegates vs. Events, What? ....................................................................................................... 103 (cid:2) Protocols ........................................................................................................................................................ 104 (cid:2) Delegates ....................................................................................................................................................... 104 (cid:2) Weak-Delegates ............................................................................................................................................. 105 (cid:2) Updating Your Controls from the UI Thread ........................................................................................................ 105 (cid:2) Summary ............................................................................................................................................................ 106 ■Chapter 7: Standard Controls ...................................................................... 107(cid:2) (cid:2) UILabel ................................................................................................................................................................ 108 (cid:2) Text Wrapping Options ................................................................................................................................... 109 (cid:2) UITextField ..................................................................................................................................................... 109 (cid:2) Borders .......................................................................................................................................................... 110 (cid:2) Text Value ...................................................................................................................................................... 111 (cid:2) Default Placeholder Text ................................................................................................................................ 111 (cid:2) Keyboards ...................................................................................................................................................... 111 (cid:2) UIButton .............................................................................................................................................................. 111 (cid:2) Handling ‘Clicks’ ............................................................................................................................................ 111 (cid:2) Different Types of Buttons ............................................................................................................................. 112 (cid:2) Button Text ..................................................................................................................................................... 113 (cid:2) UIImage ............................................................................................................................................................... 113 (cid:2) Animating an Image View .............................................................................................................................. 115 (cid:2) UIScrollView ........................................................................................................................................................ 115 (cid:2) Zooming ......................................................................................................................................................... 116 (cid:2) Implementing Tap-to-Zoom ........................................................................................................................... 117 (cid:2) Scroll Paging .................................................................................................................................................. 117 (cid:2) UISegmentedControl ........................................................................................................................................... 118 vii ■ CONTENTS (cid:2) Configuring the Segmented Control ............................................................................................................... 119 (cid:2) Specifying Segment Sizes ............................................................................................................................. 119 (cid:2) Handling Button Presses ................................................................................................................................ 120 (cid:2) UISwitch .............................................................................................................................................................. 120 (cid:2) UISlider ............................................................................................................................................................... 121 (cid:2) Images ........................................................................................................................................................... 122 (cid:2) Accessing the Value ....................................................................................................................................... 122 (cid:2) UIActivityIndicatorView ....................................................................................................................................... 122 (cid:2) UIProgressView ................................................................................................................................................... 124 (cid:2) Setting the Progress Value ............................................................................................................................ 125 (cid:2) UIPagerControl .................................................................................................................................................... 125 (cid:2) Page Changes via the Pager Control .............................................................................................................. 126 (cid:2) Updating the Pager Control When the Page is Scrolled to Via a Scroll View ................................................. 127 (cid:2) UIAlertView ......................................................................................................................................................... 128 (cid:2) Alerts and Garbage Collection ........................................................................................................................ 129 (cid:2) Working with Buttons .................................................................................................................................... 129 (cid:2) Alert Delegate ................................................................................................................................................ 131 (cid:2) Customizing the Alert View Even Further ....................................................................................................... 132 (cid:2) UIActionSheet ..................................................................................................................................................... 134 (cid:2) Creating a Simple Action Sheet ..................................................................................................................... 136 (cid:2) Button Types .................................................................................................................................................. 137 (cid:2) Adding Custom Buttons ................................................................................................................................. 137 (cid:2) Displaying an Action Sheet ............................................................................................................................ 138 (cid:2) Subclassing ................................................................................................................................................... 138 (cid:2) UIDatePicker ....................................................................................................................................................... 138 (cid:2) Configuration ................................................................................................................................................. 140 (cid:2) Showing Dynamically ..................................................................................................................................... 140 (cid:2) UIPickerView ....................................................................................................................................................... 144 (cid:2) Populating the Picker ..................................................................................................................................... 144 (cid:2) UIPickerViewModel ........................................................................................................................................ 145 (cid:2) UIToolbar ............................................................................................................................................................. 147 (cid:2) Item Types ..................................................................................................................................................... 148 (cid:2) Toolbars in Interface Builder .......................................................................................................................... 149 (cid:2) Programmatic Creation .................................................................................................................................. 153 (cid:2) Sizing ............................................................................................................................................................. 153 (cid:2) Handling Clicks .............................................................................................................................................. 154 (cid:2) Summary ............................................................................................................................................................ 154 ■Chapter 8: Content Controls ........................................................................ 155(cid:2) (cid:2) Navigation Controller .......................................................................................................................................... 155 (cid:2) Parts of the Navigation Controller .................................................................................................................. 156 (cid:2) Using the Navigation Controller ..................................................................................................................... 158 (cid:2) Modifying the Navigation Bar ......................................................................................................................... 159 (cid:2) Navigation Toolbar ......................................................................................................................................... 162 (cid:2) Tab Bar Controller ............................................................................................................................................... 163 (cid:2) Creating a Tab Bar Controller ......................................................................................................................... 165 (cid:2) Split View Controller ........................................................................................................................................... 168 (cid:2) Using the Split View ....................................................................................................................................... 170 (cid:2) Web View ............................................................................................................................................................ 172 viii ■ CONTENTS (cid:2) Using the Web View ....................................................................................................................................... 173 (cid:2) Loading Local Content ................................................................................................................................... 176 (cid:2) Interacting with Page Content ....................................................................................................................... 176 (cid:2) Loading Non-Web Documents ....................................................................................................................... 178 (cid:2) Map View ............................................................................................................................................................ 178 (cid:2) Using the Map View ....................................................................................................................................... 179 (cid:2) Using Device Location .................................................................................................................................... 181 (cid:2) Annotating the Map ........................................................................................................................................ 182 (cid:2) User Overlays ................................................................................................................................................. 188 (cid:2) Search Bar .......................................................................................................................................................... 191 (cid:2) Summary ............................................................................................................................................................ 192 ■Chapter 9: Working with Tables .................................................................. 193(cid:2) (cid:2) Parts of the UITableView ..................................................................................................................................... 193 (cid:2) Populating a Table .............................................................................................................................................. 194 (cid:2) UITableViewSource ........................................................................................................................................ 195 (cid:2) Responding to User Interaction ........................................................................................................................... 198 (cid:2) UITableViewController ......................................................................................................................................... 199 (cid:2) Refreshing the Table When Data Changes .......................................................................................................... 200 (cid:2) Customizing the Appearance of a Table View ..................................................................................................... 200 (cid:2) Table Styles ................................................................................................................................................... 200 (cid:2) Providing a Table Index .................................................................................................................................. 202 (cid:2) Cell Styles ...................................................................................................................................................... 204 (cid:2) Custom Cells .................................................................................................................................................. 206 (cid:2) Editable Tables ................................................................................................................................................... 212 (cid:2) Editing Methods ............................................................................................................................................. 213 (cid:2) Deleting Items ................................................................................................................................................ 215 (cid:2) Advanced Table Editing ................................................................................................................................. 216 (cid:2) Table Performance Considerations ..................................................................................................................... 218 (cid:2) Cell Reuse ...................................................................................................................................................... 219 (cid:2) Cache the Row Height .................................................................................................................................... 219 (cid:2) Cache Images ................................................................................................................................................ 219 (cid:2) Avoid Transparency ....................................................................................................................................... 220 (cid:2) Manually Draw the Cell .................................................................................................................................. 220 (cid:2) Avoid Complex Graphical Computations ........................................................................................................ 220 (cid:2) Create Your Cell in Code ................................................................................................................................ 220 (cid:2) Further Optimizations .................................................................................................................................... 220 (cid:2) Summary ............................................................................................................................................................ 221 ■Chapter 10: Working with Keyboards .......................................................... 223(cid:2) (cid:2) Keyboard/Input Properties .................................................................................................................................. 223 (cid:2) Capitalization ................................................................................................................................................. 224 (cid:2) Correction ...................................................................................................................................................... 224 (cid:2) Keyboard ........................................................................................................................................................ 225 (cid:2) Return Key ..................................................................................................................................................... 226 (cid:2) Languages ..................................................................................................................................................... 227 (cid:2) Hiding the Keyboard ............................................................................................................................................ 227 (cid:2) Making Input Fields Visible When the Keyboard Covers Them ........................................................................... 228 (cid:2) Summary ............................................................................................................................................................ 231 ix ■ CONTENTS ■Chapter 11: Multitasking ............................................................................. 233(cid:2) (cid:2) Application States ............................................................................................................................................... 234 (cid:2) Understanding Background Tasks ...................................................................................................................... 235 (cid:2) Checking for Multitasking Capability ............................................................................................................. 236 (cid:2) Application Delegate Methods ....................................................................................................................... 236 (cid:2) Multitasking Guidelines and Requirements ................................................................................................... 237 (cid:2) Asking the iOS for Time to Complete a Task ....................................................................................................... 239 (cid:2) Task Execution Expiration Time ..................................................................................................................... 239 (cid:2) Task Completion Patterns .............................................................................................................................. 240 (cid:2) Registering Your Application to be Allowed to Perform a Particular Background Task Category ....................... 241 (cid:2) Audio Applications .............................................................................................................................................. 243 (cid:2) Location Applications .......................................................................................................................................... 243 (cid:2) VoIP Applications ................................................................................................................................................ 244 (cid:2) VoIP Socket Handling ..................................................................................................................................... 244 (cid:2) VoIP Keep-alive .............................................................................................................................................. 244 (cid:2) Summary ............................................................................................................................................................ 245 ■Chapter 12: Working with Touch ................................................................. 247(cid:2) (cid:2) When to Use Which? ........................................................................................................................................... 247 (cid:2) Enabling Touch ................................................................................................................................................... 248 (cid:2) Touch Events ...................................................................................................................................................... 249 (cid:2) The UITouch Class .......................................................................................................................................... 249 (cid:2) Using Touch Events ........................................................................................................................................ 249 (cid:2) Gesture Recognizers ........................................................................................................................................... 255 (cid:2) Using Gesture Recognizers ............................................................................................................................ 255 (cid:2) Retrieving Gesture Information ...................................................................................................................... 259 (cid:2) Example Using the Tap Gesture ..................................................................................................................... 262 (cid:2) Example Using the Pan Gesture to Drag an Object ........................................................................................ 262 (cid:2) Allowing Gestures and Touch Events Simultaneously ................................................................................... 265 (cid:2) Creating a Custom Gesture ............................................................................................................................ 265 (cid:2) Summary ............................................................................................................................................................ 269 ■Chapter 13: Working with Shared Resources .............................................. 271(cid:2) (cid:2) File System ......................................................................................................................................................... 271 (cid:2) Case-Sensitivity ............................................................................................................................................. 271 (cid:2) Application Sandbox ...................................................................................................................................... 272 (cid:2) Application Directories ................................................................................................................................... 272 (cid:2) Backup/Restore ............................................................................................................................................. 273 (cid:2) Application Updates ....................................................................................................................................... 274 (cid:2) Device Battery ..................................................................................................................................................... 274 (cid:2) Battery Level .................................................................................................................................................. 274 (cid:2) Battery State .................................................................................................................................................. 275 (cid:2) Getting Battery Change Notifications ............................................................................................................. 276 (cid:2) Address Book/Contacts ....................................................................................................................................... 276 (cid:2) Address Book Controllers .............................................................................................................................. 277 (cid:2) Working Directly with the Address Book ....................................................................................................... 285 (cid:2) Photos and Camera ............................................................................................................................................. 289 (cid:2) UIImagePickerController ................................................................................................................................ 289 (cid:2) AV Foundation Framework ............................................................................................................................. 295 x
Description: