48
What is new on Xamarin.iOS Xamarin Inc

What's new in Xamarin.iOS, by Miguel de Icaza

  • Upload
    xamarin

  • View
    1.452

  • Download
    7

Embed Size (px)

Citation preview

Page 1: What's new in Xamarin.iOS, by Miguel de Icaza

What is new on Xamarin.iOS

Xamarin Inc

Page 2: What's new in Xamarin.iOS, by Miguel de Icaza

New in Xamarin.iOS• Features added in the last year– Based on the Mono 2.10/Silverlight-ish stack

• Features based on the Beta channel:– Mono 3.0-based stack– SGen GC– Based on the .NET 4.5 API surface

Released

Beta

Page 3: What's new in Xamarin.iOS, by Miguel de Icaza

Visual Studio!Released

Page 4: What's new in Xamarin.iOS, by Miguel de Icaza

F# Support• Functional Programming comes to iOS!

• The pitch is simple:– Fewer bugs– Focus on intent– Reuse C# libraries, experience– More features, less time

Beta

Page 5: What's new in Xamarin.iOS, by Miguel de Icaza

SIZE REDUCTIONS

Page 6: What's new in Xamarin.iOS, by Miguel de Icaza

TweetStation – Reference App• Comprehensive app• Networking• SQL • Background• Retina artwork• Full (unlinked)

MonoTouch.Dialog

Page 7: What's new in Xamarin.iOS, by Miguel de Icaza

TweetStation Size Reduction

4.2.2 5.0.4 5.2.13 5.4.1 6.0.10 iOS 6

6.2.1 6.3.3.2 Async

(Mono3)

6,200

6,600

7,000

7,400

Size in KB

Size in KB

Released

Page 8: What's new in Xamarin.iOS, by Miguel de Icaza

New: SmartLink• By default, all native code is linked-in

• SmartLink merges Mono and System linker– Only code that is directly referenced is included– Caveat: dynamic Objective-C code can fail– Big savings:

Beta

Page 9: What's new in Xamarin.iOS, by Miguel de Icaza

SmartLink effect on Samples

ATMHud

BeebleS

DK

Couchbase

Datatra

ns

DropBoxS

ync -

DropBoxS

yncSa

mpleiOS

DropBoxS

ync -

DropBoxS

yncSa

mpleMTD

Flurry

Analytics

GCDiscree

tNotificati

on

GoogleAnaly

tics

GoogleMap

s

MBProgre

ssHud

MGSplitV

iewContro

ller

RedLas

er

SDSe

gmen

tedContro

l

TestF

light

TimesS

quare

WEP

opover

ZipArch

ive -

2,000

4,000

6,000

All codeSmart Linking

Beta

Page 10: What's new in Xamarin.iOS, by Miguel de Icaza

Size Savings

ATMHud

BeebleS

DK

Couchbase

Datatra

ns

DropBoxS

ync -

DropBoxS

yncSa

mpleiOS

DropBoxS

ync -

DropBoxS

yncSa

mpleMTD

Flurry

Analytics

GCDiscree

tNotificati

on

GoogleAnaly

tics

GoogleMap

s

MBProgre

ssHud

MGSplitV

iewContro

ller

RedLas

er

SDSe

gmen

tedContro

l

TestF

light

TimesS

quare

WEP

opover

ZipArch

ive0

100,000200,000300,000400,000500,000

Savings

Page 11: What's new in Xamarin.iOS, by Miguel de Icaza

RUNTIME IMPROVEMENTS

Page 12: What's new in Xamarin.iOS, by Miguel de Icaza

Generic Value Type Sharing• Reduces these kinds of errors:

Unhandled Exception: System.ExecutionEngineException: Attempting to JIT compile method

• By generating generics code that can be shared across value types– like reference types today

• Enables more LINQ, RX and other generic-rich apps to run without changes, or without manual static stubs

Beta

Page 13: What's new in Xamarin.iOS, by Miguel de Icaza

Source of the Error• Static compiler is unable to infer that certain

generic methods will be used at runtime with a specific value type.

• Because the instantiations are delayed until runtime.

Beta

Page 14: What's new in Xamarin.iOS, by Miguel de Icaza

Generic Sharing• Consider: List<T>.Add (T x)• If “T” is a reference type:– Code can be shared between all reference types– Only one List<RefType>.Add (RefType x) exists

– Because all reference types use same storage (one pointer).

Beta

Page 15: What's new in Xamarin.iOS, by Miguel de Icaza

Generics and Value Types• Value types use different storage:– byte – 1 byte– int – 4 bytes– long – 8 bytes

• List<byte>.Add (byte x) has different code than List<long>.Add (long x)

Beta

Page 16: What's new in Xamarin.iOS, by Miguel de Icaza

Generic Sharing Today• Using string, Uri, object, int, long on Add:– Generates one shareable for Uri, object, string– Generates one for int– Generates one for long

Page 17: What's new in Xamarin.iOS, by Miguel de Icaza

With new Generic Value Type Sharing• Using string, Uri, object, int, long on Add:– Generates one shareable for Uri, object, string– Generates one for int– Generates one for long– Generates a value-type shareable• Can be used by dynamic users of Add<ValueType>

Beta

Page 18: What's new in Xamarin.iOS, by Miguel de Icaza

Shareable Value Type Generic• Runtime fallback for generic instantiations that

could not be statically computed• Uses a “fat” value type• Generates code that copies “fat” value types• Limitation: large value types can not be shared• You can disable if not needed:– -O=-gsharedvt

Beta

Page 19: What's new in Xamarin.iOS, by Miguel de Icaza

Cost of Value Type Sharing

4.2.25.0.4

5.2.135.4.1

6.0.10 iOS 6 6.2.1

6.3.3.2 Asyn

c ...

VT Shari

ng6,0006,5007,0007,5008,000

Size in KB Size in KB

394 Kb

Today: For TweetStation, the cost is 394kbWe are working to reduce this

Beta

Page 20: What's new in Xamarin.iOS, by Miguel de Icaza

Native Crypto• System.Security.Cryptography now powered

by iOS native CommonCrytpo• Covers – Symmetric crypto– Hash functions

• Hardware accelerated – AES and SHA1 (when plugged)

• Made binaries using crypto/https 100k lighter

Released

Page 21: What's new in Xamarin.iOS, by Miguel de Icaza

SGen• SGen can now be used in production on iOS

• It is no longer slower than Boehm• And much faster on most scenarios

Beta

Page 22: What's new in Xamarin.iOS, by Miguel de Icaza

DEBUGGING AIDS

Page 23: What's new in Xamarin.iOS, by Miguel de Icaza

NSObject.Description• New API that provides Objective-C “ToString”• NSObject.ToString() calls Description• Sometimes overwritten– You can always call NSObject.Description

Released

Page 24: What's new in Xamarin.iOS, by Miguel de Icaza

Helping you write Threaded Apps• UIKit is not thread safe– No major commercial UI toolkit is thread safe– Thread safety is just too hard for toolkits

• Very few UIKit methods are thread safe

• During debug mode, you get an exception

Released

Page 25: What's new in Xamarin.iOS, by Miguel de Icaza

UIApplication.CheckForIllegalCrossThreadCalls

• If set, accessing UI elements– Throws UIKitThreadAccessException

• Most UIKit classes/methods• UIViewController implemented outside UIKit

• Checks enabled on Debug, disabled on Release builds– Force use: --force-thread-check– Force removal: --disable-thread-check

Released

Page 26: What's new in Xamarin.iOS, by Miguel de Icaza

Thread Safe APIsThread Safe Classes• UIColor• UIDocument• UIFont• UIImage• UIBezierPath• UIDevice

Key Thread Safe Methods• UIApplication.SharedApplica

tion, Background APIs• UIView.Draw

See API docs for [ThreadSafe] attribute

Released

Page 27: What's new in Xamarin.iOS, by Miguel de Icaza

ASYNC

Page 28: What's new in Xamarin.iOS, by Miguel de Icaza

Async Support• Base Class Libraries:

– Expect all the standard Async APIs from .NET BCL– Coverage is .NET 4.5 Complete for BCL

• MonoTouch specific:• 114 methods on the beta

• Easy to turn ObjC methods into Async ones– Details on the Objective-C bindings talk

Beta

Page 29: What's new in Xamarin.iOS, by Miguel de Icaza

Async Synchronization Contexts• For UI thread, we run a UIKit Sync Context

– What you get from the main thread– Code resumes execution on UIKit thread– Using async just works– Transparent

• Grand Central Dispatch Sync Context– If you are running on a GCD DispatchQueue– Will resume/queue execution on the current queue

Beta

Page 30: What's new in Xamarin.iOS, by Miguel de Icaza

General Async Pattern• Methods with the signature:

void Operation (…, Callback onCompletion)

• Become:Task OperationAsync (…)

Beta

Page 31: What's new in Xamarin.iOS, by Miguel de Icaza

API IMPROVEMENTS

Page 32: What's new in Xamarin.iOS, by Miguel de Icaza

More Strong Type Constructors• Where you saw – NSDictionary options

• We now support strongly typed overloads– Give preference to strongly typed overloads– Reduce errors– Trips to the documentation– Unintended effects

Released

Page 33: What's new in Xamarin.iOS, by Miguel de Icaza

Screen Capture• To capture UIKit views:– UIScreen.Capture ()

• To capture OpenGL content:– iPhoneOSGameView.Capture ()

Released

Page 34: What's new in Xamarin.iOS, by Miguel de Icaza

UIGestureRecognizersBeforevoid Setup (){ var pan= new UIPanGestureRecognizer (

this, new Selector (”panHandler")); view.AddGestureRecognizer (pan);}

[Export(”panHandler")]void PanImage (UIPanGestureRecognizer rec){

// handle pan}

Problems:• Error prone: Parameter of PanImage must be right type• No checking (mismatch in selector names)

Nowvoid Setup (){ var pan = new UIPanGestureRecognizer (PanImage); view.AddGestureRecognizer (pan);}

void PanImage (UIPanGestureRecognizer rec){ // handle pan}

Pros:• Compiler enforced types• No selectors to deal with

Released

Page 35: What's new in Xamarin.iOS, by Miguel de Icaza

UIAppearance• Previously:

var appearance = CustomSlider.Appearance;appearance.SetMaxTrackImage (maxImage, UIControlState.Normal);

– Problem: does not work for subclasses

• Now:var appearance = CustomSlider.GetAppearance<CustomSlider> ();appearance.SetMaxTrackImage (maxImage, UIControlState.Normal);

Released

Page 36: What's new in Xamarin.iOS, by Miguel de Icaza

Notifications• Used in iOS/OSX to broadcast messages– Untyped registration– Notification payload is an untyped NSDictionary

• Typically require a trip to the docs:– To find out which notifications exist– To find what keys exist in the dictionary payload– To find out the types of the values in dictionary

Released

Page 37: What's new in Xamarin.iOS, by Miguel de Icaza

Xamarin.iOS Notifications• Discoverable, nested “Notifications class”• For example: UIKeyboard.Notifications• Pattern:– Static method ObserveXXX– Returns observer token– Calling Dispose() on observer unregisters interest

Released

Page 38: What's new in Xamarin.iOS, by Miguel de Icaza

Example – Full Code Completionnotification = UIKeyboard.Notifications.ObserveDidShow ((sender, args) => {

// Access strongly typed args Console.WriteLine ("Notification: {0}", args.Notification);

// RectangleF values Console.WriteLine ("FrameBegin", args.FrameBegin); Console.WriteLine ("FrameEnd", args.FrameEnd);

// double Console.WriteLine ("AnimationDuration", args.AnimationDuration);

// UIViewAnimationCurve Console.WriteLine ("AnimationCurve", args.AnimationCurve);});

// To stop listening:notification.Dispose ();

Released

Page 39: What's new in Xamarin.iOS, by Miguel de Icaza

NSAttributedString• On iOS 5:– Introduced for use in CoreText

• On iOS 6:– It became ubiquitous on UIKit– UIKit views use it everywhere

• Cumbersome to create

Released

Page 40: What's new in Xamarin.iOS, by Miguel de Icaza

NSAttributedString• Standard Objective-C esque approach:

//// This example shows how to create an NSAttributedString for// use with UIKit using NSDictionaries//var dict = new NSMutableDictionary () { { NSAttributedString.FontAttributeName, UIFont.FromName ("HoeflerText-Regular", 24.0f), }, { NSAttributedString.ForegroundColorAttributeName, UIColor.Black }};

• Error prone:– Get right values for keys– Get right types for values– Requires documentation trips

Released

Page 41: What's new in Xamarin.iOS, by Miguel de Icaza

NSAttributedString – C# 4 stylevar text = new NSAttributedString ( "Hello, World", font: UIFont.FromName ("HoeflerText-Regular", 24.0f), foregroundColor: UIColor.Red);

• Uses C# named parameters• Uses C# optional parameter processing• Use as many (or few) as you want

Released

Page 42: What's new in Xamarin.iOS, by Miguel de Icaza

AudioToolbox, AudioUnit, CoreMidi• Completed MIDI support

– Our CoreMIDI binding is OO, baked into system– Equivalent to what 3rd party APIs people use

• AudioUnit– Now complete supported (since 6.2)– Full API coverage

• AudioToolbox– Improved support for multiple audio channels– Strongly typed APIs

Released

Page 43: What's new in Xamarin.iOS, by Miguel de Icaza

CFNetwork-powered HttpClient• Use Apple’s CFNetwork for your HttpClient

– Avoid StartWWAN (Url call)– Turns on Radio for you – Integrates with Async

• Replace:var client = new HttpClient ()

• With:var client = new HttpClient (new CFNetworkHandler ())

Beta

Page 44: What's new in Xamarin.iOS, by Miguel de Icaza

New Dispose Semantics• Changes in NSObject() Dispose• If native code references object:– Managed object is kept functional– Actual shut down happens when native code

releases the reference.• Allows Dispose() of objects that might still be

used by the system.

Released

Page 45: What's new in Xamarin.iOS, by Miguel de Icaza

Razor Integration• Sometimes you want to generate HTML• Razor offers a full template system– Blend HTML and C#– Code completion in HTML

• Easily pass parameters from C# to Template

Released

Page 46: What's new in Xamarin.iOS, by Miguel de Icaza

New File -> Text Template -> RazorReleased

Page 47: What's new in Xamarin.iOS, by Miguel de Icaza

Razor• Creating template, passing data:

– Model property, typed in cshtml filevar template = new HtmlReport () { Model = data };

• Run:class HtmlReport {

string GenerateString () void Generate (TextWriter writer)}

Released

Page 48: What's new in Xamarin.iOS, by Miguel de Icaza

THANK YOU