Embeddinator-4000

Tools to turn .NET libraries into native libraries that can be consumed on Android, iOS, Mac, Linux and other platforms.

View the Project on GitHub

Getting started with iOS

Requirements

In addition to the requirements from our Getting started with Objective-C guide you’ll also need:

Hello world

First let’s build a simple hello world example in C#.

Create C# sample

Open Visual Studio for Mac, create a new iOS Class Library project, name it hello-from-csharp, and save it to ~/Projects/hello-from-csharp.

Replace the code in the MyClass.cs file with the following snippet:

using UIKit;
public class MyUIView : UITextView
{
	public MyUIView ()
	{
		Text = "Hello from C#";
	}
}

Build the project, the resulting assembly will be saved as ~/Projects/hello-from-csharp/hello-from-csharp/bin/Debug/hello-from-csharp.dll.

Bind the managed assembly

Run the embeddinator to create a native framework for the managed assembly:

cd ~/Projects/hello-from-csharp
objcgen ~/Projects/hello-from-csharp/hello-from-csharp/bin/Debug/hello-from-csharp.dll --target=framework --platform=iOS --outdir=output -c --debug

The framework will be placed in ~/Projects/hello-from-csharp/output/hello-from-csharp.framework.

Use the generated output in an Xcode project

Open Xcode and create a new iOS Single View Application, name it hello-from-csharp and select the Objective-C language.

Open the ~/Projects/hello-from-csharp/output directory in Finder, select hello-from-csharp.framework, drag it to the Xcode project and drop it just above the hello-from-csharp folder in the project.

Drag and drop framework

Make sure Copy items if needed is checked in the dialog that pops up, and click Finish.

Copy items if needed

Select the hello-from-csharp project and navigate to the hello-from-csharp target’s General tab. In the Embedded Binary section, add hello-from-csharp.framework.

Embedded binaries

Open ViewController.m, and replace the contents with:

#import "ViewController.h"
#include "hello-from-csharp/hello-from-csharp.h"

@interface ViewController ()
@end

@implementation ViewController
- (void)viewDidLoad {
    [super viewDidLoad];

    MyUIView *view = [[MyUIView alloc] init];
    view.frame = CGRectMake(0, 200, 200, 200);
    [self.view addSubview: view];
}
@end

Finally run the Xcode project, and something like this will show up:

Hello from C# sample running in the simulator