In this two-part article, I use MonoTouch to build an application that has a browser you can use to browse the Internet, using UIWebView to display content that we've packaged in the application. In Part 1, I create the web browser and in Part 2 examine how to display the local content. (The complete source code accompanying this article is available here.) MonoTouch is a framework that lets you build iPhone and iPad applications using C#. This frees you from dealing with memory management, pointers, etc., and instead lets you focus on the application functionality. MonoTouch is a commercial product that is part of the larger, open source Mono project, Novell's open-source implementation of the .NET standard created by Microsoft and published as a set of international standards. For more on MonoTouch's capabilities, see my article MonoTouch and the iPhone: The GPS and Compass Functions.
The recently released MonoTouch 2.0 is focused specifically on application development for the iPad, iPhone, and iPod Touch using the .NET Framework, including C# and other .NET programming languages. MonoTouch simplifies iPad and iPhone development by letting you use code and libraries written for .NET. While the iPad developer license restricts the distribution of scripting engines or Just-in-Time (JIT) compilers required by managed runtimes such as .NET for code execution, developers can use MonoTouch while fully complying with Apple license terms because MonoTouch delivers only native code.
So let's build an application....
Building the App
To start, open up MonoDevelop; it should look something like this:
Next, create a new iPhone MonoTouch project. To do this, select File/New/Solution:
Choose iPhone Window-based Project from the C#/ iPhone templates and name the solution "Example_UIWebView":
Click OK on the Project Features dialogue (they're not relevant to what we're doing right now):
The project should now look something like this:
The application is going to consist of three screens:
- A home screen with two buttons
- A web browser screen
- A local content browser.
I'll use a Navigation Controller to manage the screen navigation and the buttons on the home screen to push screens (views) onto the Navigation controller.
Let's add the main screen, which will hold the Navigation Controller and the buttons. Right-click on the project name and choose Add, and then New File:
On the next screen, choose iPhone from the left pane, and View Interface Definition with Controller on the right pane. Name it, "MainNavigationView," and click New:
Now let's open our MainNavigationView in Interface Builder by double-clicking on the MainNavigationView.xib file in MonoDevelop. Interface Builder will load and the Document Window of your view should look like this:
We want our Navigation Controller to be the top level View Controller, so delete the View item in the Document Window. Once you've done that, drag a "Navigation Controller" control from the Library Window onto the Document Window:
The Navigation Controller doesn't come with a view that we can put stuff in, so drag a View control onto it:
Your Document should now look like this:
Now that we have a View, we have a surface on which to put stuff, so let's drag a couple of Rounded Rect Buttons from the Library Window onto our newly created view. Resize them so that they look approximately like the following, and edit their text labels by double-clicking on them:
Set the text one of them to be "Web Browser," and the other to be "Local Content Browser." While we're here, change the text for the root navigation item from "Root View Controller," to "Browser Example." Do this as you did the buttons, simply double-click on the label and enter the new text: