ios for c# developers - devconnections talk
DESCRIPTION
Crash-course for C# developers on getting started with C#/iOS development.TRANSCRIPT
![Page 1: iOS for C# Developers - DevConnections Talk](https://reader035.vdocuments.site/reader035/viewer/2022062307/5555bfc8d8b42afe5d8b524d/html5/thumbnails/1.jpg)
iOS for C# Developers
Miguel de Icaza – Xamarin Inc@migueldeicaza
![Page 2: iOS for C# Developers - DevConnections Talk](https://reader035.vdocuments.site/reader035/viewer/2022062307/5555bfc8d8b42afe5d8b524d/html5/thumbnails/2.jpg)
iOS Adoption
![Page 3: iOS for C# Developers - DevConnections Talk](https://reader035.vdocuments.site/reader035/viewer/2022062307/5555bfc8d8b42afe5d8b524d/html5/thumbnails/3.jpg)
.NET Event Idiom
Objects raising a number of events
F.Clicked += EventHandler
Storage for properties/events:
• either in object• Or uses bags, like
DependencyProperties
MyControl
OnEnter
OnLeave
Clicked
Background
Font
![Page 4: iOS for C# Developers - DevConnections Talk](https://reader035.vdocuments.site/reader035/viewer/2022062307/5555bfc8d8b42afe5d8b524d/html5/thumbnails/4.jpg)
MyControlDelegate
Objective-C Delegate Idiom
Objects use a peer object to post notifications
f.Delegate = new myDelegate ();
Must implement methods in MyDelegate
MyControl
OnEnter
OnLeave
Clicked
Background
Font
Delegate
![Page 5: iOS for C# Developers - DevConnections Talk](https://reader035.vdocuments.site/reader035/viewer/2022062307/5555bfc8d8b42afe5d8b524d/html5/thumbnails/5.jpg)
Objective-C Action/Target
• Poor man’s C# delegate.
• In .NET this is mapped to a C# delegate– Use methods– Anonymous methods– Lambdas
![Page 6: iOS for C# Developers - DevConnections Talk](https://reader035.vdocuments.site/reader035/viewer/2022062307/5555bfc8d8b42afe5d8b524d/html5/thumbnails/6.jpg)
C# on iOS
• All the features you come to expect from C#
• IDEs:– Visual Studio on Windows– Xamarin Studio on Mac
• Think of iOS as another platform to target– Just like you ASP.NET or WPF are– Same level of code sharing
![Page 7: iOS for C# Developers - DevConnections Talk](https://reader035.vdocuments.site/reader035/viewer/2022062307/5555bfc8d8b42afe5d8b524d/html5/thumbnails/7.jpg)
APIs for C# Developers on iOS
• .NET’s Base Class Libraries– mscorlib, System, System.Core, System.Data– System.Web.Services– etc
• Native iOS APIs surfaced as C# classes– Mapped with some artistic liberties:– Follow .NET’s Framework Design Guidelines
![Page 8: iOS for C# Developers - DevConnections Talk](https://reader035.vdocuments.site/reader035/viewer/2022062307/5555bfc8d8b42afe5d8b524d/html5/thumbnails/8.jpg)
iOS APIs Surfaced to .NET
• Objective-C to C# bridge– Special runtime support for these– Integrates the Object Systems
• Object Oriented C Code– Manually mapped to C# classes
• Mostly CoreFoundation derived types
• Regular C code– Mapped to C# classes as well
![Page 9: iOS for C# Developers - DevConnections Talk](https://reader035.vdocuments.site/reader035/viewer/2022062307/5555bfc8d8b42afe5d8b524d/html5/thumbnails/9.jpg)
iOS APIs for .NET
• iOS APIs are weakly typed– Similar to .NET 1.0 code
• C# bindings are strongly typed– Helps explore the API– Let the IDE help you write your code
• Async-ified (same rules as .NET async)
![Page 10: iOS for C# Developers - DevConnections Talk](https://reader035.vdocuments.site/reader035/viewer/2022062307/5555bfc8d8b42afe5d8b524d/html5/thumbnails/10.jpg)
THE BASICS
![Page 11: iOS for C# Developers - DevConnections Talk](https://reader035.vdocuments.site/reader035/viewer/2022062307/5555bfc8d8b42afe5d8b524d/html5/thumbnails/11.jpg)
Starting Up
• C# Main () method– Call UIApplication.Main – Pass the name of your application delegate class
• System creates UIApplication class– Instantiates your UIApplicationDelegate
• UIApplicationDelegate methods invoked– FinishedLaunching performs UI setup
![Page 12: iOS for C# Developers - DevConnections Talk](https://reader035.vdocuments.site/reader035/viewer/2022062307/5555bfc8d8b42afe5d8b524d/html5/thumbnails/12.jpg)
Complete app
![Page 13: iOS for C# Developers - DevConnections Talk](https://reader035.vdocuments.site/reader035/viewer/2022062307/5555bfc8d8b42afe5d8b524d/html5/thumbnails/13.jpg)
UIApplicationDelegate
• How the operating system talks to your app
• Mostly deals with state:– Starting up (fresh, openUrl request)– Suspending– Resuming– Respond to notifications– Background downloads
• Also: data security, UI orientation
![Page 14: iOS for C# Developers - DevConnections Talk](https://reader035.vdocuments.site/reader035/viewer/2022062307/5555bfc8d8b42afe5d8b524d/html5/thumbnails/14.jpg)
Hierarchy
Screens
Windows
ViewControllers
Views or ViewControllers
![Page 15: iOS for C# Developers - DevConnections Talk](https://reader035.vdocuments.site/reader035/viewer/2022062307/5555bfc8d8b42afe5d8b524d/html5/thumbnails/15.jpg)
UIScreen
• Represents a screen available in your device
• UIScreen.MainScreen is the main screen– Same as UIScreen.Screens [0]
• Other screens used for external connectors
![Page 16: iOS for C# Developers - DevConnections Talk](https://reader035.vdocuments.site/reader035/viewer/2022062307/5555bfc8d8b42afe5d8b524d/html5/thumbnails/16.jpg)
UIWindows
• Developers use one (system does others)– Routes events– Sets the Root View Controller
• UIWindow.RootViewController– Must be set by the end of running your FinishedLaunching method.
![Page 17: iOS for C# Developers - DevConnections Talk](https://reader035.vdocuments.site/reader035/viewer/2022062307/5555bfc8d8b42afe5d8b524d/html5/thumbnails/17.jpg)
UIView
• Base class for all UI Elements
BackgroundColor = UIColor.Red
![Page 18: iOS for C# Developers - DevConnections Talk](https://reader035.vdocuments.site/reader035/viewer/2022062307/5555bfc8d8b42afe5d8b524d/html5/thumbnails/18.jpg)
UIView Subclasses
UIView
UIControl
UIButton
UIPicker
UISlider
UISwitch
UIStepper
UITextField
UIImageView
UILabel
UIProgressView
UIScrollView
UITableView
UITextView
UICollectionView10,000 foot view, not comprehensive
Simple UIViews
Controls
ScrollViews
![Page 19: iOS for C# Developers - DevConnections Talk](https://reader035.vdocuments.site/reader035/viewer/2022062307/5555bfc8d8b42afe5d8b524d/html5/thumbnails/19.jpg)
UIViews are General Containers
Can be arbitrarily nested
UIView methods:AddSubview (UISubview)RemoveFromSuperview ()
Unlike Gtk/Winforms: Everything is a container
![Page 20: iOS for C# Developers - DevConnections Talk](https://reader035.vdocuments.site/reader035/viewer/2022062307/5555bfc8d8b42afe5d8b524d/html5/thumbnails/20.jpg)
Can be Arbitrarily Nested
![Page 21: iOS for C# Developers - DevConnections Talk](https://reader035.vdocuments.site/reader035/viewer/2022062307/5555bfc8d8b42afe5d8b524d/html5/thumbnails/21.jpg)
UIView’s
• Frame– Superview coordinates– RectangleF
• Bounds– Size in UIView’s
coordinates
• Transform– 2D Affine transformation
• Center– Superview coordinates
X, Y = (20,10)
W,H=(50,30)
Center X, Y = (35,20)
![Page 22: iOS for C# Developers - DevConnections Talk](https://reader035.vdocuments.site/reader035/viewer/2022062307/5555bfc8d8b42afe5d8b524d/html5/thumbnails/22.jpg)
2D Affine Transforms
Rotation Translation Shearing Scaling
![Page 23: iOS for C# Developers - DevConnections Talk](https://reader035.vdocuments.site/reader035/viewer/2022062307/5555bfc8d8b42afe5d8b524d/html5/thumbnails/23.jpg)
UIView’s
• Frame– Superview coordinates– RectangleF
• Bounds– Size in UIView’s
coordinates
• Transform– 2D Affine transformation
• Center– Superview coordinates
45’ rotation over the center
![Page 24: iOS for C# Developers - DevConnections Talk](https://reader035.vdocuments.site/reader035/viewer/2022062307/5555bfc8d8b42afe5d8b524d/html5/thumbnails/24.jpg)
UIView Center+Affine Transforms
• Frame– Superview coordinates– RectangleF
• Bounds– Size in UIView’s
coordinates
• Transform– 2D Affine transformation
• Center– Superview coordinates
![Page 25: iOS for C# Developers - DevConnections Talk](https://reader035.vdocuments.site/reader035/viewer/2022062307/5555bfc8d8b42afe5d8b524d/html5/thumbnails/25.jpg)
Custom UIView - Rendering
• Override Draw (RectangleF region) method– Must paint the entire requested region
• Obtain the UIGraphics’ current draw context
• CGContext: Immediate graphics API– Similar to System.Drawing on Windows.Forms– Not retained, like Silverlight
![Page 26: iOS for C# Developers - DevConnections Talk](https://reader035.vdocuments.site/reader035/viewer/2022062307/5555bfc8d8b42afe5d8b524d/html5/thumbnails/26.jpg)
Samplepublic override void Draw (RectangleF rect){ using (CGContext context = UIGraphics.GetCurrentContext ()) { // turn on anti-aliasing context.SetAllowsAntialiasing (true); // loop through each spot and draw it foreach (Spot s in touchSpots) { context.SetFillColor (s.Red, s.Green, s.Blue, s.Alpha); context.FillEllipseInRect (new RectangleF (s.Point,size)); } }}
![Page 27: iOS for C# Developers - DevConnections Talk](https://reader035.vdocuments.site/reader035/viewer/2022062307/5555bfc8d8b42afe5d8b524d/html5/thumbnails/27.jpg)
Custom UIViews – Touch Handling• Configure properties:
– UserInteractionEnabled, MultipleTouchEnabled
• Override:TouchesBegan:
– User touched the UI
TouchesMoved– Updated locations
TouchesEnded– User lifted fingers
TouchesCancelled– System cancelled (for example, incoming call)
![Page 28: iOS for C# Developers - DevConnections Talk](https://reader035.vdocuments.site/reader035/viewer/2022062307/5555bfc8d8b42afe5d8b524d/html5/thumbnails/28.jpg)
Animation
• UIKit is powered by an animation framework– Details, beyond the scope of today’s talk
• Certain properties can be animated:– Frame, Bounds, Center, Transform– Alpha, BackgroundColor, ContentStretch
• Very little setup needed
![Page 29: iOS for C# Developers - DevConnections Talk](https://reader035.vdocuments.site/reader035/viewer/2022062307/5555bfc8d8b42afe5d8b524d/html5/thumbnails/29.jpg)
Animation + Async
• UIView.Animate methods
• Use AnimateNotifyAsync family of methods– Duration– Lambda to update visual properties– Options controlling animation
• Await on the call– Will resume execution after animation completes
![Page 30: iOS for C# Developers - DevConnections Talk](https://reader035.vdocuments.site/reader035/viewer/2022062307/5555bfc8d8b42afe5d8b524d/html5/thumbnails/30.jpg)
Sample
![Page 31: iOS for C# Developers - DevConnections Talk](https://reader035.vdocuments.site/reader035/viewer/2022062307/5555bfc8d8b42afe5d8b524d/html5/thumbnails/31.jpg)
Dynamics – new in iOS 7
• Introduces a physics engine into the UI – Gravity– Collision– Attachment– Snap– Forces/Pushing
![Page 32: iOS for C# Developers - DevConnections Talk](https://reader035.vdocuments.site/reader035/viewer/2022062307/5555bfc8d8b42afe5d8b524d/html5/thumbnails/32.jpg)
Gravity + Collision Detection
![Page 33: iOS for C# Developers - DevConnections Talk](https://reader035.vdocuments.site/reader035/viewer/2022062307/5555bfc8d8b42afe5d8b524d/html5/thumbnails/33.jpg)
UIViewController
• Typically a full screen of content
• Host for your views– In charge of layout – Orientation changes– Provides event routing for your views
• Some can host other UIViewControllers– “UIViewController Containment”
• UIViewController.View property is the root UIView
![Page 34: iOS for C# Developers - DevConnections Talk](https://reader035.vdocuments.site/reader035/viewer/2022062307/5555bfc8d8b42afe5d8b524d/html5/thumbnails/34.jpg)
Manually Creating your UI
![Page 35: iOS for C# Developers - DevConnections Talk](https://reader035.vdocuments.site/reader035/viewer/2022062307/5555bfc8d8b42afe5d8b524d/html5/thumbnails/35.jpg)
Using a UI Designer
![Page 36: iOS for C# Developers - DevConnections Talk](https://reader035.vdocuments.site/reader035/viewer/2022062307/5555bfc8d8b42afe5d8b524d/html5/thumbnails/36.jpg)
UIViewControllers in UIKit
UIViewControllers in UIKit.
UIViewController
UIActivityViewController
UICollectionViewController
UINavigationViewController
UIPageViewController
UIReferenceLibraryViewController
UITabBarViewController
UITableViewController
UISplitVuewController
Built-in UI
Building Blocks
![Page 37: iOS for C# Developers - DevConnections Talk](https://reader035.vdocuments.site/reader035/viewer/2022062307/5555bfc8d8b42afe5d8b524d/html5/thumbnails/37.jpg)
Built-in UI
UIActivityViewController UIReferenceLibraryViewController
![Page 38: iOS for C# Developers - DevConnections Talk](https://reader035.vdocuments.site/reader035/viewer/2022062307/5555bfc8d8b42afe5d8b524d/html5/thumbnails/38.jpg)
Other UIViewControllers• AddressBookUI
– ABNewPersonViewController– ABPersonViewController– ABUnknownPersonViewController
• EventKitUI– EKCalendarChooser– EKEventViewController
• GLKit– GLKViewController
• GameKit– GKMatchmakerViewController
• MediaPlayer– MPMediaPickerController– MPMoviePlayerViewController.
• MultipeerConnectivity– MCBrowserViewController– MCPeerPickerViewController
• PassKit– PKAddPassesViewController
• QuickLook– QLPreviewController
• Social/Twitter– SLComposeViewController– TWTweetComposeViewController
• StoreKit– SKStoreProductViewController
![Page 39: iOS for C# Developers - DevConnections Talk](https://reader035.vdocuments.site/reader035/viewer/2022062307/5555bfc8d8b42afe5d8b524d/html5/thumbnails/39.jpg)
Some Examples
ABNewPersonViewController EKEventViewController
![Page 40: iOS for C# Developers - DevConnections Talk](https://reader035.vdocuments.site/reader035/viewer/2022062307/5555bfc8d8b42afe5d8b524d/html5/thumbnails/40.jpg)
Some Examples
TWTweetComposeViewController MPMoviewPlayerViewController
![Page 41: iOS for C# Developers - DevConnections Talk](https://reader035.vdocuments.site/reader035/viewer/2022062307/5555bfc8d8b42afe5d8b524d/html5/thumbnails/41.jpg)
UIViewControllers and Storyboards
• Name your class in the designer– Will be reflected in your code
• Only after ViewDidLoad () are objects created– Any references to other views or controllers– Wont be valid until after this method is called
![Page 42: iOS for C# Developers - DevConnections Talk](https://reader035.vdocuments.site/reader035/viewer/2022062307/5555bfc8d8b42afe5d8b524d/html5/thumbnails/42.jpg)
Presenting View Controllers
• Given a current UIViewController, call:– PresentViewController– PresentViewControllerAsync
• Modality of controller:– bool ModalViewController {get;set}
• You can build your own visual transition– And control every step of it
![Page 43: iOS for C# Developers - DevConnections Talk](https://reader035.vdocuments.site/reader035/viewer/2022062307/5555bfc8d8b42afe5d8b524d/html5/thumbnails/43.jpg)
UIControl
![Page 44: iOS for C# Developers - DevConnections Talk](https://reader035.vdocuments.site/reader035/viewer/2022062307/5555bfc8d8b42afe5d8b524d/html5/thumbnails/44.jpg)
Common Controls
![Page 45: iOS for C# Developers - DevConnections Talk](https://reader035.vdocuments.site/reader035/viewer/2022062307/5555bfc8d8b42afe5d8b524d/html5/thumbnails/45.jpg)
Strongly Typed Notifications
• NSNotificationCenter– Application Message Bus– Hub for posting messages• “Keyboard will appear”• “Font size changed”
• Strongly typed in C#– Class.Notification.ObserveXXXX ()
![Page 46: iOS for C# Developers - DevConnections Talk](https://reader035.vdocuments.site/reader035/viewer/2022062307/5555bfc8d8b42afe5d8b524d/html5/thumbnails/46.jpg)
Strongly Typed
![Page 47: iOS for C# Developers - DevConnections Talk](https://reader035.vdocuments.site/reader035/viewer/2022062307/5555bfc8d8b42afe5d8b524d/html5/thumbnails/47.jpg)
THREE VIEWS TO MASTER
![Page 48: iOS for C# Developers - DevConnections Talk](https://reader035.vdocuments.site/reader035/viewer/2022062307/5555bfc8d8b42afe5d8b524d/html5/thumbnails/48.jpg)
UIScrollView
• Where the magic originates• Powerful control that handles scrolling– Pagination, scrolling, smooth motion
• Must see: Series of WWDC talks on it– Every possible trick and hack
![Page 49: iOS for C# Developers - DevConnections Talk](https://reader035.vdocuments.site/reader035/viewer/2022062307/5555bfc8d8b42afe5d8b524d/html5/thumbnails/49.jpg)
UITableView
• Most UI in iOS is a table• Variable height• External Data Source– Request Section/Row– Return UITableViewCell
• External Delegate
• Powerful• And Cumbersome
![Page 50: iOS for C# Developers - DevConnections Talk](https://reader035.vdocuments.site/reader035/viewer/2022062307/5555bfc8d8b42afe5d8b524d/html5/thumbnails/50.jpg)
MonoTouch.Dialog – UITableView made easy
• Switches the model– From callback to fetch data (very scalable)– To dump all data into view (easy, not scalable)
• Elements:– Provide cell-specific style renderers
![Page 51: iOS for C# Developers - DevConnections Talk](https://reader035.vdocuments.site/reader035/viewer/2022062307/5555bfc8d8b42afe5d8b524d/html5/thumbnails/51.jpg)
MonoTouch.Dialog sample
• Sample program• Mimics Sounds Settings
![Page 52: iOS for C# Developers - DevConnections Talk](https://reader035.vdocuments.site/reader035/viewer/2022062307/5555bfc8d8b42afe5d8b524d/html5/thumbnails/52.jpg)
![Page 53: iOS for C# Developers - DevConnections Talk](https://reader035.vdocuments.site/reader035/viewer/2022062307/5555bfc8d8b42afe5d8b524d/html5/thumbnails/53.jpg)
UICollectionView
• Arbitrary collections• In any form you want– Table, Grid– Baseball diamond– Circle– Or anything you can
provide rules for
![Page 54: iOS for C# Developers - DevConnections Talk](https://reader035.vdocuments.site/reader035/viewer/2022062307/5555bfc8d8b42afe5d8b524d/html5/thumbnails/54.jpg)
More Resources
Wallace McClure’s talkMechanics of it - Right after this oneIslander IE
• http://developer.apple.com/ios• Apple’s site
• http://docs.xamarin.com/ios– Docs, tutorials on C# and iOS
• http://planet.xamarin.com– Technical Blogs from the community