iOS Integration

Monkey Archive Forums/Monkey Tutorials/iOS Integration

Rob Pearmain(Posted 2011) [#1]
This is a simple example of integrating iOS functionality with Monkey.

For this tutorial, I am going to call my class "apple",

Step 1. Folders

Under your project folder create a folder for your class (In this case "apple"), and under that create a "native" folder.

So you will have
mygame.monkey
/apple
/apple/native


Step 2. - CPP

Under your "native" folder, create your "cpp" file (class.ios.cpp), in this case "apple.ios.cpp"

/* 	
	filename:	apple.ios.cpp
	author:		Rob Pearmain
	description:	Class for monkey, to allow access to ios functions
*/

//.h
class apple;

#import <UIKit/UIKit.h>

//.cpp
class apple : public gxtkObject
{
public:
	virtual float GetBatteryLevel();
	virtual String GetDeviceName();
	virtual String GetSystemVersion();
	virtual String GetSystemName();
	virtual String GetDeviceModel();
};

float apple::GetBatteryLevel()
{
        // EDIT: Added setBatteryMonitoringEnabled
        [[UIDevice currentDevice] setBatteryMonitoringEnabled:YES];
	float BatteryLevel = [[UIDevice currentDevice] batteryLevel];
	return BatteryLevel;	
}

String apple::GetDeviceName()
{

	NSString *name =  [[UIDevice currentDevice] name];
	return String(name);
}

String apple::GetSystemVersion()
{
	NSString *ver =  [[UIDevice currentDevice] systemVersion];
	return String(ver);
}

String apple::GetDeviceModel()
{

	NSString *model =  [[UIDevice currentDevice] model];
	return String(model);
}

String apple::GetSystemName()
{
	NSString *systemname =  [[UIDevice currentDevice] systemName];
	return String(systemname);
}


Step 3. - Extern

In your "apple" folder create "apple.monkey" and add the following code:


' Point to the C++
Import "native/apple.ios.cpp"

' Let monkey know we are referencing an external class
Extern

' Create a monkey class to map to the external class
Class Apple="apple"

	' Battery Level (Returns -1 in Simulator)
	Method GetBatteryLevel:Float()
	
	' Device Name (e.g. iPad Simulator)
	Method GetDeviceName:String()
	
	' iOS Version (e.g. 4.3.2)
	Method GetSystemVersion:String()
	
	' Device Model (e.g. iPhone)
	Method GetDeviceModel:String()
	
	' System Name (e.g. iPhone)
	Method GetSystemName:String()

	Method SetOrientationLeft:Void()
End


Step 4. - Using the functions

In your "mygame.monkey" add an Import:

Import apple


And add code to access the class

app:Apple = New Apple()

Local devicename:String = app.GetDeviceName()
Local systemversion:String = app.GetSystemVersion()
Local devicemodel:String = app.GetDeviceModel()
Local systemname:String = app.GetSystemName()
Local batterylevel:Float = app.GetBatteryLevel()


(Note: batterylevel will always return -1 on the simulator)


Shinkiro1(Posted 2011) [#2]
Thanks , that will be helpful.


CopperCircle(Posted 2011) [#3]
Thanks Rob, Im still trying to get iOS buttons working, any ideas? I can 't seem to get the superview to work.


Rob Pearmain(Posted 2011) [#4]
Do you want to post your code/link, I will take a look. What is the error you are getting?


CopperCircle(Posted 2011) [#5]
This is the cpp code, I can't seem to pickup the monkey superview to draw the button.




Rob Pearmain(Posted 2011) [#6]
Not sure.

I tried:

[[UIView currentView] addSubview:myButton]


Although it compiled, it didn't work :-(


NoOdle(Posted 2011) [#7]
brilliant tutorial, I have been wondering how to go about doing this. Thank you for taking the time to explain :)


MonkeyPlotter(Posted 2012) [#8]
Looking forward to giving this a try soon


CodeGit(Posted 2013) [#9]
Does anybody know if this works??

[self.view addSubview: myButton];


rIKmAN(Posted 2013) [#10]
I have tried following the instructions in the OP, but I keep getting errors when compiling and I'm not sure if it's something I've done/not done or something that is happening due to Monkey being updated since this was posted?

Has it worked for anybody else?

Here is the last part of the error I get when trying to compile for iOS in Release mode.

/Users/Rik/Desktop/iosIntegration/iosIntegration.build/ios/main.mm:3907:1: error: expected unqualified-id
/ *
^
/Users/Rik/Desktop/iosIntegration/iosIntegration.build/ios/main.mm:3919:22: error: expected class name
class apple : public gxtkObject
                     ^
2 errors generated.


** BUILD FAILED **


The following build commands failed:
	CompileC build/MonkeyGame.build/Release-iphonesimulator/MonkeyGame.build/Objects-normal/i386/main.o main.mm normal i386 objective-c++ com.apple.compilers.llvm.clang.1_0.compiler
(1 failure)
TRANS FAILED: Error executing 'xcodebuild -configuration Release -sdk iphonesimulator', return code=16640
Done.

Any help appreciated!

PS. Sorry for the codebox breaking the page width!


AdamRedwoods(Posted 2013) [#11]
I am quite sure things have changed in Monkey since this was posted.
Without testing this specific example, I think classes should be extending Object, rather than gxtkGraphics. That's how I usually do it.


rIKmAN(Posted 2013) [#12]
Thanks Adam, you were right that has sorted it - the other error was an errant space on one of the comments.

Working now, cheers! :)