This tutorial will guide you through the process of making a console application that leverage the power of the Monobjc bridge. The project is part of the sample applications and can be found in the distribution.

Before following this tutorial, make sure that you have correctly installed both the Monobjc bridge.

The tutorial is divided into the following steps:

  • Creating the project structure
  • Writing the .NET code
  • Building
  • Running

Creating the project structure

Before starting the coding of the application, you need to create a directory that will contains the source code and the build script.

Note

The proposed directory structure is a way to organize the files for the development. You can perfectly use another structure, once you are familiar with the building and the packaging.

Writing the .NET code

The code to write contains both the bootstrapping of the application and its logic. Create a file named Program.cs under the root of the project and copy/paste the following code:

using System;
using Monobjc;
using Monobjc.AppKit;
using Monobjc.Foundation;

namespace Monobjc.Samples.ConsoleApp
{
    /// 
    /// Entry point for the application
    /// 
    internal class Program
    {
        private static void Main(String[] args)
        {
            // Load any required framework
            ObjectiveCRuntime.LoadFramework("Foundation");
            ObjectiveCRuntime.LoadFramework("AppKit");
            // Do the bridge initialization
            ObjectiveCRuntime.Initialize();

            // Allocate a pool for memory management
            NSAutoreleasePool pool = new NSAutoreleasePool();

            Console.WriteLine("Username: " + FoundationFramework.NSFullUserName());
            Console.WriteLine("Home Dir: " + FoundationFramework.NSHomeDirectory());
            Console.WriteLine("Screen  : " + NSScreen.MainScreen.Frame);

            NSDictionary dict = NSDictionary.DictionaryWithContentsOfFile("/System/Library/Frameworks/AppKit.framework/Resources/version.plist");
            Console.WriteLine("AppKit Version: " + dict[(NSString) "CFBundleVersion"].CastTo<NSString>());

            // Release any used memory
            pool.Release();
        }
    }
}

Note

This file contains code that is present in every application at it bootstraps the Monobjc runtime. See the Bootstrapping page more details on the code.

Building

For the building and the packaging, the preferred way to go is to use some scripting, with a NAnt build file. Create a file named appl.build and write the following content:

<?xml version="1.0"?>
<project name="Monobjc Application" default="build" basedir=".">
  <description>Monobjc Application Build File</description>

  <property name="app.name" value="ConsoleApp" overwrite="false"/>

  <!-- ===============================================================================
  Build the application
  ================================================================================ -->
  <target name="build" description="Build the application">

    <!-- Compile source files -->
    <csc target="exe" output="${app.name}.exe">
      <sources>
        <include name="**/*.cs"/>
      </sources>
      <references>
        <include name="System.dll"/>
        <include name="System.Core.dll"/>
      </references>
      <pkg-references>
        <package name="monobjc-10.6"/>
      </pkg-references>
    </csc>

  </target>

</project>

Open a Terminal window and go at the root of the project. Type the nant command. You should see an output like this:

NAnt 0.90 (Build 0.90.3780.0; release; 05/08/2010)
Copyright (C) 2001-2010 Gerry Shaw
http://nant.sourceforge.net

Buildfile: file:///Users/monobjc/Projects/monobjc-samples/ConsoleApp/appl.build
Target framework: Mono 2.0 Profile
Target(s) specified: build


build:

      [csc] Compiling 1 files to '/Users/monobjc/Projects/monobjc-samples/ConsoleApp/ConsoleApp.exe'.

BUILD SUCCEEDED

Total time: 0.8 seconds.

The application is now build and ready to run.

Running

Console application must be launched with the monobjc command in order to use the Monobjc bridge runtime. The output should look like:

Mac:ConsoleApp$ monobjc ConsoleApp.exe
Username: Monobjc
Home Dir: /Users/monobjc
Screen  : NSRect(NSPoint(0, 0) - NSSize(1680, 1050))
AppKit Version: 1038.35

The result will of-course depends on the user and on the monitor attached to your machine.