[MonoTouch] Help by binding objective-c

Walter Szewelanczyk walterszewelanczyk at gmail.com
Tue Oct 11 06:29:57 EDT 2011


As a new to MonoTouch user myself, I had some issues with this as well.
 Primarily in what btouch actually did and how to setup to use it.   At
first I thought btouch would take the library itself (the .a file) and use
that automatically produce the .Net dll, then I was thinking it would take
the .h file and do it, but really thats not the case at all.

Really what happens is that you need to build a .cs file that has interfaces
that use annotations that tell btouch how to build the real classes that do
the mapping to the Obj-C lib.  Now if you have done some basic p/Invoke you
may be thinking that if you have already annotated a .cs file why do you
need btouch at all.  You technically dont, but btouch does save you a lot of
effort as there is a lot more work than with a typical c style interop.

The other thing to note is that the interface file will never actually
be referenced by your code base once you generate the dll in btouch.  It is
used only so that btouch knows what you need built, and you get real classes
and your interfaces are not actually referenced.

So to get some experience with this I built a simple Obj-C lib with the
following header :

--------------- BEGIN ObjC HEADER --------------------

#import <Foundation/Foundation.h>

typedef unsigned char                  UCHAR;

typedef struct
{
UCHAR messageSize;
UCHAR messageId;
UCHAR data1;
UCHAR data2;
} Stuff;

@protocol StructDelegate
- (void)processStruct:(Stuff)antMessage;
@end

@protocol SimpleDelegate
- (NSString*)process:(NSString*)msg;
@end

@interface TestLib : NSObject
{
    id <StructDelegate> del;
    id<SimpleDelegate> sDel;
}
- (NSString *) RepeatAfterMe:(NSString*) repeatMe;
- (NSString*) SayHello;
- (int) HardCodedValue;
- (NSString*) Send:(Stuff) s;
+ (NSString*) classMethod;
- (void) save:(id <StructDelegate>) d;
- (void) saveSimple:(id <SimpleDelegate>) d;
- (NSString*) callSimple:(NSString*) s;
@end

--------------- END ObjC HEADER --------------------


So you will notice we have a C style struct, some protocols and an Objective
C style class with static and instance methods.

Now I wanted to be able to use this in MonoTouch so here is the Interface
file I built for bTouch :


------ BEGIN btouch Mapping File ----------

using System;
using MonoTouch.Foundation;
using System.Runtime.InteropServices;
using MonoTouch.ObjCRuntime;


namespace libTestLib
{
[BaseType (typeof(NSObject))]
[Model]
interface StructDelegate
{
[Export ("processStruct:")]
    string processStruct (MyStuff s);
}
 [BaseType (typeof(NSObject))]
[Model]
interface SimpleDelegate
{
[Export ("process:")]
    string process (string s);
}

 [BaseType (typeof (NSObject))]
interface TestLib {
[Export ("RepeatAfterMe:")]
string RepeatAfterMe (string repeatMe);

[Export ("SayHello")]
string SayHello { get; }

[Export ("HardCodedValue")]
int HardCodedValue { get; }

[Static, Export ("classMethod")]
string ClassMethod { get; }
 [Export("saveSimple:")]
void SaveSimple(SimpleDelegate del);
 [Export("callSimple:")]
string CallSimple (string s);
 [Export ("save:")]
void Save(StructDelegate d);
 [Export ("Send:")]
string Send (MyStuff s);
}
}

------ END btouch Mapping File ----------

Now notice the struct is not referenced in this file. Btouch will only
output classes for the interfaces in the main file so we need to included
the extra things we need in other files.  In this case we need a file to
define the struct.


------ BEGIN btouch struct File ----------
namespace libTestLib
{
public struct MyStuff
{
public byte messageSize;
public byte messageId;
public byte data1;
public byte data2;
}
}
------ BEGIN btouch struct File ----------

to build the .dll run btouch -v theInterface.cs
anyExtraFilesLikeOurStructFile.cs

This will produce a .dll that you will include into our MonoTouch project.
 You do not need to include the interface or extra files into the project
itself as they are already built into the dll. ( I do include them in a sub
dir with the lib, but I have the build action set to nothing ).

Once you build the dll I would encourage you to look at the dll in
MonoDevelop and see how much extra stuff is generated for you by using
btouch.

now in your real code you can invoke things as follows :

var t = new TestLib();
var s = new MyStuff();
s.messageSize = 1;
s.messageId = 2;
s.data1 = 10;
s.data2 = 11;
Console.WriteLine ("This is from objC sayHello : {0}", t.SayHello);
Console.WriteLine ("This is from objC send  : {0}", t.Send (s));
Console.WriteLine ("This is from objC static : {0}", TestLib.ClassMethod);


Hopefully that helps a bit on how btouch actually works and helps you
started in the right direction.



Walt
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.ximian.com/pipermail/monotouch/attachments/20111011/a1bd6956/attachment.html 


More information about the MonoTouch mailing list