IT tutorials
 
Mobile
 

iPhone Programming : Other Native Platforms - MonoTouch

2/14/2013 6:06:05 PM
- How To Install Windows Server 2012 On VirtualBox
- How To Bypass Torrent Connection Blocking By Your ISP
- How To Install Actual Facebook App On Kindle Fire

The MonoTouch platform from Novell allows you to build C#- and .NET-based applications on the iPhone and iPod touch. It comes in two editions: Professional and Enterprise.

A license for the Professional Edition, intended for individual use, costs $399 per year. The Enterprise Edition, intended for corporate use, costs $999 per year (although you can buy a five-developer pack for $3,999 per year). Alternatively, you can download an evaluation version that enables development and testing against iPhone Simulator only.

1. Download and Installation

Before downloading and installing MonoTouch, you must download the latest release of Mono, the open source development platform based on the .NET Framework that allows developers to build cross-platform applications in C#. You can obtain it from http://www.mono-project.com/.

The Mono framework downloads as a disk image file containing a package installer file. Double-click on this package file to start the Mono installer, as shown in Figure 1.

Figure 1. Installing Mono


After installing the Mono framework, you need to install the MonoDevelop environment before you can install MonoTouch itself. MonoDevelop is an IDE primarily designed for C# and other .NET languages and you can download it from http://monodevelop.com/; it comes as a disk image, and installation is simply a matter of dragging and dropping the MonoDevelop.app application from the disk image to your Applications folder.


Warning:

You need to use the latest MonoTouch version of MonoDevelop for Mac OS X, as it contains several fixes that are not in the mainstream version of the application. This version is linked from the MonoTouch website.


After installing Mono and MonoDevelop, you can download the trial version of the MonoTouch SDK from http://monotouch.net/DownloadTrial. MonoTouch is distributed as a package file that will automatically start the Installer when it downloads, as shown in Figure 2.

Figure 2. Installing MonoTouch


2. Building a MonoTouch Project

Double-click on the MonoDevelop application that you installed in your Applications folder, and you will be presented with something similar to Figure 3. Select FileNew Solution from the MonoDevelop menu to open the New Solution window (Figure 4). From there, choose a new C#iPhoneiPhone MonoTouch Project.

After entering the solution name, click the Forward button and then the Okay button to complete the setup process. You do not need to choose any of the optional project features (e.g., Packaging or Unix Integration). In the Solution pane, click the disclosure triangle next to the solution name to expand it, and then expand each subfolder in the same way, and you’ll be presented with something that looks a lot like Figure 5.

The default template generated by the MonoTouch SDK produces a Main.cs file that is used to start your application event loop:

using System;
using System.Collections.Generic;
using System.Linq;
using MonoTouch.Foundation;
using MonoTouch.UIKit;

namespace HelloWorld
{
    public class Application
    {
        static void Main (string[] args)
        {
            UIApplication.Main (args);
        }
    }

    // The name AppDelegate is referenced in the MainWindow.xib file.
    public partial class AppDelegate : UIApplicationDelegate
    {
        // This method is invoked when the application has loaded its UI
        // and its ready to run public override bool FinishedLaunching
        // (UIApplication app, NSDictionary options)
        {
            // If you have defined a view, add it here:
            // window.AddSubview (navigationController.View);

            window.MakeKeyAndVisible ();

            return true;
        }

        // This method is required in iPhoneOS 3.0
        public override void OnActivated (UIApplication application)
        {
        }
    }
}

					  

Figure 3. The main MonoDevelop window


Figure 4. The MonoDevelop New Solution window


Figure 5. The Hello World application in the MonoDevelop Solution Pad


The default template also creates a MainWindow.xib.designer.cs file that MonoTouch will update each time you edit the MainWindow.xib file inside Interface Builder. This file will mirror all of the views, controllers, outlets, and actions that you add to your UI and then map those elements to C# properties that you can access from your own code. Here’s the default MainWindow.xib.designer.cs:

namespace HelloWorld
{
    // Base type probably should be MonoTouch.Foundation.NSObject or subclass
    [MonoTouch.Foundation.Register("AppDelegate")]
    public partial class AppDelegate
    {

        [MonoTouch.Foundation.Connect("window")]
        private MonoTouch.UIKit.UIWindow window {
         get {
           return ((MonoTouch.UIKit.UIWindow)(this.GetNativeField ("window")));
         }
         set { this.SetNativeField ("window", value); }
        }
    }
}

					  

If you select RunRun from the MonoDevelop menu bar at this point, the application will be built, compiled to native code, and started inside iPhone Simulator. You should see something very similar to Figure 6.

Figure 6. The default MonoTouch template running in iPhone Simulator


Quit from iPhone Simulator and return to the MonoDevelop environment. Double-click on the MainWindow.xib file to open the NIB file in Interface Builder. Drag a button (UIButton) and a label (UILabel) into the main view window.

Next, click on the app delegate in the MainWindow.xib file and go to the Classes segment of the multisegmented control at the top of the Library window. Click on AppDelegate in the list of objects, and then click on the Outlets segment of the multisegmented control underneath the AppDelegate object. Click on the plus sign button below the outlets list to add a new outlet. Add two new outlets, calling them “button” and “label”, respectively; see Figure 7.

Figure 7. Adding the button and label outlets


In the Connections Inspector (⌘-2), connect the button and label outlets to the button and label elements as you’ve done in other projects. Figure 8 shows what the finished NIB should look like (you can use the Attributes Inspector to change the appearance of the button and label).

Figure 8. The MainWindow.xib file in Interface Builder


Save your changes and return to the MonoDevelop environment. If you look again at the MainWindow.xib.designer.cs file, you should see that it reflects the changes you made to the view inside Interface Builder. The changes that MonoDevelop made are shown in bold:

namespace HelloWorld {


    // Base type probably should be MonoTouch.Foundation.NSObject or subclass
    [MonoTouch.Foundation.Register("AppDelegate")]
    public partial class AppDelegate {

        #pragma warning disable 0169
        [MonoTouch.Foundation.Connect("window")]
        private MonoTouch.UIKit.UIWindow window {
         get {
          return ((MonoTouch.UIKit.UIWindow)(this.GetNativeField("window")));
         }
         set {
          this.SetNativeField("window", value);
         }
        }

        [MonoTouch.Foundation.Connect("button")]
					private MonoTouch.UIKit.UIButton button {
					get {
					return ((MonoTouch.UIKit.UIButton)(this.GetNativeField("button")));
					}
					set {
					this.SetNativeField("button", value);
					}
					}
					[MonoTouch.Foundation.Connect("label")]
					private MonoTouch.UIKit.UILabel label {
					get {
					return ((MonoTouch.UIKit.UILabel)(this.GetNativeField("label")));
					}
					set {
					this.SetNativeField("label", value);
					}
					}
    }
}

					  


Warning:

You should not make any changes to the MainWindow.xib.designer.cs file, as the MonoTouch framework updates it automatically each time you edit your NIB file in Interface Builder.


Double-click on the Main.cs file to open it in the MonoTouch editor and add the following code directly before the line where the window is made visible (window.MakeKeyAndVisible ();):

button.TouchDown += delegate {
     label.Text = "Pushed Button";
};

Save your modifications and select RunRun from the MonoDevelop menu bar. Once the application has been built and been deployed into iPhone Simulator, tap the Push! button. You should see something like Figure 9.

Figure 9. The MonoTouch Hello World application


You’ve just built your first iPhone application with MonoTouch.
 
Others
 
- iPhone Programming : Other Native Platforms - PhoneGap
- Windows Phone 8 : Phone-Specific Controls (part 2) - Pivot Control
- Windows Phone 8 : Phone-Specific Controls (part 1) - Panorama Control
- BlackBerry Tablet Applications : Exploring the APIs - Busy Indicator
- BlackBerry Tablet Applications : Exploring the APIs - Multi-Touch
- BlackBerry Tablet Applications : Exploring the APIs - Microphone
- Android : Getting Fancy with Lists - Inflating Rows Ourselves
- Android : Getting Fancy with Lists - A Dynamic Presentation
- Android : Getting Fancy with Lists - Getting to First Base
- Windows Phone 7 : Getting Started with XNA - Displaying Text
 
 
Top 10
 
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
programming4us programming4us
 
Popular tags
 
Video Tutorail Microsoft Access Microsoft Excel Microsoft OneNote Microsoft PowerPoint Microsoft Project Microsoft Visio Microsoft Word Active Directory Biztalk Exchange Server Microsoft LynC Server Microsoft Dynamic Sharepoint Sql Server Windows Server 2008 Windows Server 2012 Windows 7 Windows 8 Adobe Indesign Adobe Flash Professional Dreamweaver Adobe Illustrator Adobe After Effects Adobe Photoshop Adobe Fireworks Adobe Flash Catalyst Corel Painter X CorelDRAW X5 CorelDraw 10 QuarkXPress 8 windows Phone 7 windows Phone 8 BlackBerry Android Ipad Iphone iOS