YOU ARE DOWNLOADING DOCUMENT

Please tick the box to continue:

Transcript
Page 1: Programming iOS in C#

Programming iOS in C#

Frank A. KruegerSeattle .NET Mobile

March 4, 2014

Page 2: Programming iOS in C#

AGENDA

• Intro to iOS APIs

• Intro to UIKit

• Demo programming with Xamarin Studio

Page 3: Programming iOS in C#

Frank

• .NET developer since .NET 1.1

• First iOS app accepted to the App Store on Dec 11, 2008

• First apps written in Objective-C++

@praeclarumhttp://praeclarum.org

http://github.com/praeclarum

Page 4: Programming iOS in C#

Frank

Page 5: Programming iOS in C#

Frank

• Started using MonoTouch (Xamarin.iOS) summer 2009

• First app released with iPad in May 2010

Page 6: Programming iOS in C#

Frank

Page 7: Programming iOS in C#

Frank

https://github.com/praeclarum/lcars

Page 8: Programming iOS in C#

Frank

• iCircuit released July, 2010

Page 9: Programming iOS in C#

iOS APIs

Page 10: Programming iOS in C#

iOS APIsCocoa Touch

UIKit, GameKit, MapKit, …

Media

CoreGraphics, CoreAudio, CoreImage, CoreText, OpenGL, SpriteKit, …

Services

Foundation, CoreLocation,

CoreMotion, PassKit, JavaScriptCore,

Multipeer Connectivity, …

Core OS

Accelerate, CoreBluetooth,

ExternalAccessory, …

Page 11: Programming iOS in C#

Cocoa Touch• UIKit to create the UI

• iAd to get rich

• GameKit to interface with Game Center

• MapKit for interactive 2D and 3D maps

• AddressBookUI to access Contacts

• EventKitUI to access the Calendar

• MessageUI to send email or messages

Page 12: Programming iOS in C#

Media• CoreGraphics to render vector graphics onto bitmaps

• ImageIO even supports raw images

• CoreImage hardware accelerated image processing

• CoreAnimation for high performance realtime rendering (basis for UIKit)

• GLKit for all your 3D needs

• SpriteKit is a 2D sprite game engine

• AVFoundation, AssetsLibrary, AudioToolbox, AudioUnit, CoreAudio, CoreMIDI, CoreVideo, OpenAL, Media Player. Get the picture?

Page 13: Programming iOS in C#

Services• Foundation provides a serializable data, collections, networking, data

streams, strings (like the BCL)

• CoreLocation provides location updates

• CoreMotion provides orientation and activity tracking

• Multipeer Connectivity easily create peer-to-peer and mesh networks

• JavaScriptCore full JavaScript engine that you can embed in your app and even bridge to your object model

• CoreData full ORM and data store that even works over iCloud

• Social accesses OS-level social accounts to post messages

• PassKit to add passes to the Passbook app

Page 14: Programming iOS in C#

Core OS

• Accelerate super fast and efficient image and matrix math library

• Security provides safe places to put data

• CoreBluetooth gives low level access to the Bluetooth hardware

• ExternalAccessory gives low level access to devices plugged into the device

Page 15: Programming iOS in C#

iOS APICocoa Touch

UIKit, GameKit, MapKit, …

Media

CoreGraphics, CoreAudio, CoreImage, CoreText, OpenGL, SpriteKit, …

Services

Foundation, CoreLocation,

CoreMotion, PassKit, JavaScriptCore,

Multipeer Connectivity, …

Core OS

Accelerate, CoreBluetooth,

ExternalAccessory, …

https://developer.apple.com/library/ios

Page 16: Programming iOS in C#
Page 17: Programming iOS in C#
Page 18: Programming iOS in C#
Page 19: Programming iOS in C#

Objective-C Declaration

- (void)presentViewController:(UIViewController *)viewControllerToPresent animated:(BOOL)flag completion:(void (^)(void))completion

[self presentViewController:vc animated:YES completion:^{ // Do stuff after it’s been presented}];

Page 20: Programming iOS in C#

Objective-C Declaration in C#

- (void)presentViewController:(UIViewController *)viewControllerToPresent animated:(BOOL)flag completion:(void (^)(void))completion

Task PresentViewControllerAsync ( UIViewController viewControllerToPresent, bool animated)

await PresentViewController (vc, true);// Do stuff after it’s been presented

Page 21: Programming iOS in C#
Page 22: Programming iOS in C#
Page 23: Programming iOS in C#
Page 24: Programming iOS in C#

UIKit

Page 25: Programming iOS in C#

Single Screen App

UIWindow

UIApplication

UIApplicationDelegate

Delegate

Window

Page 26: Programming iOS in C#

Single Screen App

UIWindow

UIApplication

UIApplicationDelegate

Delegate

Window

Created by OS

Subclassed

by your app

Created by you

Page 27: Programming iOS in C#

Single Screen App

UIWindowUIViewControlle

r

RootViewController

Page 28: Programming iOS in C#

UIViewController

• The Controller in MVC responsible for

- monitoring and coordinating user interactions

- to view and edit model data

Page 29: Programming iOS in C#

UIViewControlleris an MVC controller

Cloud DataUser DataServices

Device Sensors

VIEW

LabelTable

Edit Box

CONTROLLER

Model

Page 30: Programming iOS in C#

UIViewController• A Screen of UI

- on iPhone, takes up the majority of the screen

- on iPad, can take up the whole screen or have children view controllers in a layout

- designed to work with parent UINavigationController with navigation actions and toolbar actions

- present or otherwise transitions to other view controllers

Page 31: Programming iOS in C#

Single Screen App

UIWindowUIViewControlle

r

RootViewController UIView

View

Model

Page 32: Programming iOS in C#

Single Screen App

UIWindowUIViewControlle

r

RootViewController UIView

View

ModelUILabel

UIButton…

Subviews

Page 33: Programming iOS in C#

Multi Screen AppUIWindow

RootViewController

UIViewController

Model

UIViewController

UIViewView

UILabelUIButton

Subviews

UIViewController

UIViewView

UILabelUIButton

Subviews

ChildViewControllers

Page 34: Programming iOS in C#

UIView• “Dumb” - should not interact with the Model

directly

• Responsible for drawing itself

• Contains subviews to layout

• Receives touch events, can be assigned gesture recognizers

• Participates in the responder chain for user input (keyboard, pop-up menus)

Page 35: Programming iOS in C#

UIViews• Output

• Input

• Big Scrolling Complicated Things

• Custom

Page 36: Programming iOS in C#

UILabelDisplays rich-

text

Output Views

UIImageViewDisplays images

UIBezierPathDisplays vector graphics

UIActivityIndicatorView

Animates a circle toindicate activity

UIProgressViewA linear progress

bar

Page 37: Programming iOS in C#

Input Views

UISliderDiscrete or continuous

number selection

UIStepperTwo-button

action

UISegmentedControl

Modern radio buttons

UITextFieldSingle-line text

input

UIButtonYou know what it

does

UISwitchOn/Off toggle

Page 38: Programming iOS in C#

UIScrollView• Scroll and zoom subviews

• Responsible for velocity scroll and bounce effects

• Can also page through views

• Scroll views within scroll views are supported

• Basis for many full screen views

Page 39: Programming iOS in C#

UITableView

• Scroll view designed to display a long vertical list of cells

• Used everywhere in iOS from login forms, status feeds, episode lists…

• MonoTouch.Dialog simplifies the interface

Page 40: Programming iOS in C#

UICollectionView• Versatile and efficient view

to display large amount numbers of views arranged and sized in any fashion

• Introduced in iOS 6, these are meant to replace UITableViews to create richer UIs

• Pluggable layout engine with built-in flow layout

• Advanced transition and physical animations built into iOS 7

Page 41: Programming iOS in C#

UIWebView

• Practically an entire web browser in a UIView

• You can control its cookies and cache

• You can execute JavaScript code against the DOM

• You can feed it raw HTML or point it to a URL

Page 42: Programming iOS in C#

UITextView

• Multiline rich text editor

Page 43: Programming iOS in C#

UIPickerView

• UIDatePicker is a ready-to-use UIPickerView

Page 44: Programming iOS in C#

Custom UIViews

Composition through Subviews

• Need to layout using auto layout constraints or old-fashioned rectangle setting

Page 45: Programming iOS in C#

class MyView : UIView {

public MyView () {

AddSubviews (Time, Progress, Title, Author);

Time.Frame = new RectangleF (20, 20, 300, 40);

Progress.Frame = new RectangleF (20, 200, 300, 40);

Title.Frame = new RectangleF (20, 300, 300, 80);

Author.Frame = new RectangleF (20, 380, 300, 80);

}}

Page 46: Programming iOS in C#

class MyView : UIView {

public MyView () {

AddSubviews (Time, Progress, Title, Author);

this.ConstrainLayout (() =>

Time.Frame.Left == this.Frame.Left + 20 &&Time.Frame.Right == this.Frame.Right - 20 &&Time.Frame.Top == this.Frame.Top + 20 &&

Progress.Frame.GetMidX () == Time.Frame.GetMidX () &&Progress.Frame.Top == Time.Frame.Bottom &&

Title.Frame.GetMidX () == this.Frame.GetMidX () &&Title.Frame.Top == PlayPause.Frame.Bottom &&Title.Frame.Width <= TitleMaxWidth &&

Author.Frame.GetMidX () == this.Frame.GetMidX () &&Author.Frame.Top == Title.Frame.Bottom &&Author.Frame.Width <= TitleMaxWidth);

}}

EasyLayout https://gist.github.com/praeclarum/6225853

Page 47: Programming iOS in C#

Custom UIViewsCustom Drawing

public override void Draw (RectangleF rect){

var c = UIGraphics.GetCurrentContext ();

var b = Bounds;c.SetLineWidth (1.0f);

c.SetRGBStrokeColor (202/255.0f, 202/255.0f, 202/255.0f, 1);

c.MoveTo (0, 0);c.AddLineToPoint (0, b.Height);c.StrokePath ();

c.MoveTo (b.Width, 0);c.AddLineToPoint (b.Width, b.Height);c.StrokePath ();

c.SetRGBStrokeColor (176/255.0f, 176/255.0f, 176/255.0f, 1);

c.MoveTo (0, b.Height);c.AddLineToPoint (b.Width, b.Height);c.StrokePath ();

}

Page 48: Programming iOS in C#

UIGestureRecognizer

• Easy recognition of single and multitouch events

• Multiple recognizers can be added to a view

• Can coordinate with other gesture recognizers

Page 49: Programming iOS in C#

UIXGestureRecognizer

• LongPress

• Pan

• Pinch

• Rotation

• ScreenEdgePan

• Swipe

• Tap

Page 50: Programming iOS in C#

UIMyGestureRecognizer

• Making your own is easy

• Just Respond to these events:

TouchesBegan

TouchesMoved

TouchesEnded

TouchesCancelled

Page 51: Programming iOS in C#

UIViewController

• To implement a screen of your app, inherit from UIViewController

• But there are built-in view controllers

Page 52: Programming iOS in C#

UINavigationController

• Maintains a stack of UIViewController like a web browser

• Provides a navigation bar and a toolbar for actions

• Fundamental controller for iPhone UI

• UIViewControllers are designed to work well within a UINavigationController

Page 53: Programming iOS in C#

UITabBarController

• Up to 5 discrete view controllers accessible by buttons at the bottom of the screen

• Often these view controllers are UINavigationControllers

Page 54: Programming iOS in C#

UISplitViewController

• Only available on iPad

• Must be the window’s RootViewController

• Provides automatic handling of master-detail type UIs

Page 55: Programming iOS in C#

UIPageViewController

• Built-in page turn effect

Page 56: Programming iOS in C#

UITableViewController &UICollectionViewControlle

r

Page 57: Programming iOS in C#

UIPopoverController

• Not a UIViewController, not a UIView, but works with them

• Only works on the iPad (crash on iPhone)

Page 58: Programming iOS in C#

Reacting to events

UIViewController

UIViewView

ModelUILabel

UIButton…

Subviews

User EventsUser EventsUser EventsUser EventsService Service EventsEventsService Service EventsEvents

Page 59: Programming iOS in C#

Many event sources• .NET events

• Overridable methods

• Async tasks

• NSNotificationCenter

• Responder Chain

• Delegate objects

Page 60: Programming iOS in C#

Delegate objects

• Instead of many events that can be subscribed to by many different objects,

• Events are overridable methods on an object that get called as if they were events

• Can also be used to pass data back to the calling object

Page 61: Programming iOS in C#

Delegate objects UISplitViewController splitVC = …;

splitVC.Delegate = new SplitDelegate ();

class SplitDelegate : UISplitViewControllerDelegate{

public override bool ShouldHideViewController (UISplitViewController svc, UIViewController viewController, UIInterfaceOrientation inOrientation)

{return true;

}

public override void WillHideViewController (UISplitViewController svc, UIViewController aViewController, UIBarButtonItem barButtonItem, UIPopoverController pc)

{}

public override void WillShowViewController (UISplitViewController svc, UIViewController aViewController, UIBarButtonItem button)

{}

}

Page 62: Programming iOS in C#

Reusable Views

In order to be fast and lean,

views with potentially many subviews

recycle offscreen views

(Virtual list mode in WinForms)

Page 63: Programming iOS in C#

Reusable Views

Usually these views have a

DataSource

property that is responsible for creating and data binding reusable subviews

Page 64: Programming iOS in C#

UITableView.DataSource

When a cell goes offscreen, it is removed from the view hierarchy and stored in a cache

When the table view needs a new cell to display, your code should take from this cache

public override UITableViewCell GetCell (UITableView tableView, NSIndexPath indexPath){

var cell = tableView.DequeueReusableCell (EpisodeTableViewCell.ReuseId) as EpisodeTableViewCell;

if (cell == null) {cell = new EpisodeTableViewCell ();

}

cell.Episode = Controller.episodes [indexPath.Row];

return cell;}

Page 65: Programming iOS in C#

UICollectionView.DataSource

collectionView.RegisterClassForCell (typeof(EpisodeCell), EpisodeCell.ReuseId);

public override UICollectionViewCell GetCell (UICollectionView collectionView, NSIndexPath indexPath)

{var cell = (EpisodeCell)collectionView.DequeueReusableCell (

EpisodeTableViewCell.ReuseId, indexPath)

cell.Episode = Controller.episodes [indexPath.Row];

return cell;}

Page 66: Programming iOS in C#

UIPageView&

UIPickerView

Page 67: Programming iOS in C#

Theming

• Theming is supported at the OS level

• Meant to be set once

• Appearance attributes can be set per class or per object

Page 68: Programming iOS in C#

Theming

UINavigationBar.Appearance.SetTitleVerticalPositionAdjustment (-1, UIBarMetrics.Default);UINavigationBar.Appearance.SetTitleVerticalPositionAdjustment (-4, UIBarMetrics.LandscapePhone);

UINavigationBar.Appearance.SetTitleTextAttributes (new UITextAttributes {TextColor = BarTextColor,TextShadowColor = BarTextShadowColor,TextShadowOffset = BarTextShadowOffset,Font = UIFont.FromName (TitleFontName, BarTitleFontSize),

} );

Page 69: Programming iOS in C#

MapsView Controller Hierarchy

UINavigationController MapViewController

View Hierarchy

UIWindow

UINavigationBar UIButton UISearchBar UIButton

MKMapView MKMapOverlay[]

UIToolbar UIButton[]

Page 70: Programming iOS in C#

View Controller Hierarchy

UINavigationController MapViewController

View Hierarchy

UIWindow

UINavigationBar UIButton UISearchBar UIButton

MKMapView MKMapOverlay[]

UIToolbar UIButton[]

Maps

Page 71: Programming iOS in C#

Mapsclass MapViewController : UIViewController{

override void ViewDidLoad (){

base.ViewDidLoad ();

// Set our View to the interactive mapView = new MKMapView ();

// Set (top) navigation bar buttonsNavigationItem.LeftBarButtonItem =

new UIBarButtonItem (UIImage.FromBundle ("Directions.png"),HandleDirections);

// Set (bottom) toolbar buttonsToolbarItems = new[] {

new UIBarButtonItem (UIImage.FromBundle ("Location.png"),HandleLocation);

} ;}

}

Window.RootViewController = new UINavigationController (

new MapViewController ());

Page 72: Programming iOS in C#

Demo

Page 73: Programming iOS in C#

Related Documents