41
1 Băluță Cristian https://github.com/ralcr

Native iOS apps with haxe objective-C target

Embed Size (px)

DESCRIPTION

wwx2013 speech: Cristian Băluță "Native iOS apps with haxe objective-C target"

Citation preview

Page 2: Native iOS apps with haxe objective-C target

Things to cover

Architecture

Implementation

Cocoa vs Flash

Future

2

Page 3: Native iOS apps with haxe objective-C target

Why?

Fun

No viable alternative

Speed

Passion

Knowledge

3

Page 4: Native iOS apps with haxe objective-C target

4

iOS appscan be built with: Xcode, Haxe NME, one of the hundreds of HTML5 frameworks, Flash IDE, Unity

Page 5: Native iOS apps with haxe objective-C target

5

Beautiful syntax- (int)changeColorToRed:(float)r green:(float)g blue:(float)b; [myColor changeColorToRed:5.0 green:2.0 blue:6.0];

function changeColorToRed (red:Float, green:FLoat, blue:FLoat) :Int; myColor.changeColorToRed (5.0, 2.0, 6.0);

Page 6: Native iOS apps with haxe objective-C target

Architecture

6

Page 7: Native iOS apps with haxe objective-C target

7

.hxHxobjc

haxelibs

SupportingFiles

hxcocoa

Page 8: Native iOS apps with haxe objective-C target

8

Compiler flags-objc-platform : iphone, ipad, ios, universal

-objc-version : 4.0 - 6.1

-objc-bundle-version : 1.0

-objc-identifier

-objc-owner

-objc-bundle-name

-objc-supporting-files

Page 9: Native iOS apps with haxe objective-C target

9

Compiler flags-objc-lib

-objc-framework

-objc-linker-flag : ObjC

-ios-orientation : UIInterfaceOrientationPortrait

Page 10: Native iOS apps with haxe objective-C target

Implementation

10

Page 11: Native iOS apps with haxe objective-C target

11

Genobjc.ml3400 lines of code

stole code from genas3 and gencpp

failed to split in genobjc + genxcode

camelCase

dozens of repetitive matches of type ‘t’

Page 12: Native iOS apps with haxe objective-C target

12

The magic behindString -> NSMutableString category

Array -> NSMutableArray category

Anonymous Object -> NSMutableDictionary

Date -> NSDate category

primitive -> primitive

static -> static + getter + setter methods

dynamic func -> property + method + block

new -> init. other inits are disabled

Page 13: Native iOS apps with haxe objective-C target

13

The magic behindarray access -> hx_objectAtIndex + casting

optional arguments -> if then

class -> interface + implementation

interface -> protocol

enum -> enum

Page 14: Native iOS apps with haxe objective-C target

14

Special classesnew SEL(method) -> @selector(method:arg:)

new CGRect -> CGGeometry.CGRectMake()

Concurrency -> static C methods

Iterator?

Page 15: Native iOS apps with haxe objective-C target

15

Nullreplaced by nil or [NSNull null] in arrays

objective-c is a dynamic language

when it encounters a nil is not doing anything

your app will not crash

but you might wonder why is not working

Page 16: Native iOS apps with haxe objective-C target

16

Dynamicit’s replaced by ‘id’

‘id’ is used as a return of a constructor or can hold any object

but can’t hold primitive values

‘id’ does not require the *

Page 17: Native iOS apps with haxe objective-C target

17

Metadata@:include -> #import <someheader.h>

@:import -> #import “someheader.h”

@:sel(“methodName:arg1:arg2:”)

@:framework

@:weak

@:c

@:category

@:getterBody

Page 18: Native iOS apps with haxe objective-C target

18

Not workingReflect, Type, Ereg, Xml, Resources

primitives as objects

sys, unit tests

Dynamic and untyped calls

complex enums

Int32, Int64, Utf8, io

string + primitive concatenation

import one file into another and vice versa.

Page 19: Native iOS apps with haxe objective-C target

19

Memoryit’s done with ARC (automatic ref. counting)

Page 20: Native iOS apps with haxe objective-C target

20

Memoryif you follow good programming principles you’ll not have leaks, but if you have try the @:weak metadata

Page 21: Native iOS apps with haxe objective-C target

Cocoa, CocoaTouch

21

Page 22: Native iOS apps with haxe objective-C target

22

Do not reinvent the wheel

Page 23: Native iOS apps with haxe objective-C target

23

Hxcocoahaxelib

3 main packages: objc, ios, osx

objc: foundation, graphics, network, location, store, audio video

ios: ui, map, gl, assets, addressbook, iAd, mediaplayer, social, telephony

osx: addkit, webkit

SupportingFiles : ios launch images and icons

Page 24: Native iOS apps with haxe objective-C target

24

Lib.hxeach package has it’s own Lib.hx

generic: log, printf, println, location, getURL

ios: attach, enumerateLibraryGroups, fetchCameraItemsInGroup, isIpad, isIpod, alert

Page 25: Native iOS apps with haxe objective-C target

25

MVC

Controller

ViewModel

Page 26: Native iOS apps with haxe objective-C target

26

MVC

Controller

ViewModel

will, should, did

data at, count

data source

delegate action

target

Notifications & KVO

Page 27: Native iOS apps with haxe objective-C target

27

ViewController lifecycle

public function new ()

override public function viewDidLoad ()override public function viewWillAppear () override public function viewDidAppear ()

override public function didReceiveMemoryWarning ()

override public function viewDidDisappear ()override public function dealloc ()

Page 28: Native iOS apps with haxe objective-C target

28

Flash vs CocoaSprite -> UIView

x, y, width, height -> frame.origin, frame.size

addChild -> addSubview

clipsToBounds, backgroundColor

var sprite = new Sprite();sprite.x = 0;sprite.y = 0;

var sprite = new UIView();var frame:CGRect = sprite.frameframe.origin.x = 0;frame.origin.y = 0;sprite.frame = frame;

Page 29: Native iOS apps with haxe objective-C target

29

Flash vs CocoaEventDispatcher -> Delegate and UINotificationCentre

Page 30: Native iOS apps with haxe objective-C target

30

Flash vs CocoaTouchEvent -> UITouch

override public function touchesBegan (touches:NSSet, withEvent:UIEvent) :Void { var touches :Array = withEvent.touchesForView(this); var aTouch :UITouch = touchesForView.anyObject(); var pos :CGPoint = aTouch.locationInView(this);}

this.addEventListener (TouchEvent.TOUCH_BEGIN, tapHandler);function touchHandler(e:TouchEvent) { var x = e.localX; var y = e.localY;}

Page 31: Native iOS apps with haxe objective-C target

31

Flash vs CocoaMouseClick -> target-action pattern

but.addTarget (this, new SEL(touch), UIControlEventTouchUpInside);

but.addEventListener (MouseEvent.MOUSE_CLICK, touch);

Page 32: Native iOS apps with haxe objective-C target

32

CoreAnimationUIView animation

hidden, frame, alpha, transform

the properties are set immediately but the appearance is animated

UIView.animateWithDuration (duration:Float, delay:Float, options:UIViewAnimationOptions, animations:Void->Void,completion:Bool->Void);

Page 33: Native iOS apps with haxe objective-C target

33

Debuggingtraces are converted to println with Haxe pos information

native NSLog

Instrumens for finding leaks and profiling

Page 34: Native iOS apps with haxe objective-C target

34

BenchmarksCompile time:

Hxobjc - all utest takes 0.8 sec

NME - seconds to minutes

Size:

Hxobjc - almost as the .hx files

NME - beyond imagination

Page 35: Native iOS apps with haxe objective-C target

35

BenchmarksRuntime

1mil mutable strings takes 2.5 sec comparing to 1mil strings 0.012 sec

Adding this strings to an array takes 5sec for the mutable string and 0.46 for the simple strings

Page 36: Native iOS apps with haxe objective-C target

Future

36

Page 37: Native iOS apps with haxe objective-C target

37

FutureImplement all the standard library

objc and ios externs

osx externs

bypass Xcode manual compilation

sdk.ralcr hxobjc support

Page 38: Native iOS apps with haxe objective-C target

38

.hx

Hxobjchaxelibs

SupportingFiles

Page 39: Native iOS apps with haxe objective-C target

39

Distant future

Page 40: Native iOS apps with haxe objective-C target

40

2025Compile serverside would be nice

HxobjcServer

Page 41: Native iOS apps with haxe objective-C target

Questions?

41