Sunday 30 August 2009

MonoTouch Settings.bundle

Property Lists (plist) and Bundles are probably so obvious to seasoned Mac/iPhone developers that they're not even worth mentioning, however for someone with a '100% .NET' background you can't take anything for granted. I had a -lot-bit of trouble getting iPhone Settings working with MonoTouch (see here), but now that I've figured it out it seems very simple. Here's a quick guide:

1. Bundles are folders
Within your MonoTouch project, create a new folder (right-click solution → Add → New Folder) and call it Settings.bundle


2. Plist files are some sort of hybrid Xml key-value
(right-click solution → Add → New File... → Empty Text File) and call it Root.plist. You could add an existing plist-xml-formatted file if you like - but we're just going to create one from scratch (in the next step).


3. Edit Root.plist with the Property List Editor
The structure of the plist is very specific - look for some doco or Chapter 10 of Beginning iPhone Development. In this example I've only used PSGroupSpecifier and PSTextFieldSpecifier but there are many other types (PSMultiValueSpecifier, PSToggleSwitchSpecifier, PSChildPaneSpecifier...).
Double-click the Root.plist file inside MonoDevelop to open the Property List Editor - type carefully as there is no 'validation' and if you mis-spell (or mis-capitalize) something it just won't work:


4. Set the correct build action for Root.plist
This is important (and I think the cause of my earlier problem) - you MUST tell the IDE to make sure this file is copied to the phone.


5. Access the settings
Use the NSUserDefaults.StandardUserDefaults property to access the values set by the user.


6. See/Edit "Settings" on the iPhone


It's worthwhile noting that the 'useability' for these kinds of Settings is 'unusual' for desktop applications, but probably familiar to iPhone users... the settings for a whole swag of unrelated applications are grouped under the Settings icon (it's not necessarily obvious from with the application that they are available). The user must also quit the application to get to these settings - which means they're most useful for things that don't change much (eg. the users identity and servers in the Mail.app).

Thursday 27 August 2009

MonoTouch "iSOFlair"

UPDATE: 6-Oct version 2.0 adds some functionality and the source code.

I didn't get as far as I'd have liked with today's MonoTouch app - a pretty basic rendering of your 'score' on stackoverflow.com (via their 'flair' feature). Yep, definitely not a 'practical' application.

The main goals were to play with WebClient, the 'filesystem' and then to get a multi-page/view app working with proper Settings (to configure the various stackoverflow variants). As you can see from the screenshots below I only got half-way there...


Here's the code: Main.cs, MainWindow.xib.designer.cs and root.plist. At least the WebClient for text and images worked well, as did saving and loading from the local file system. You'll have to excuse the dodgy Html parsing ;)

Unfortunately I've wasted a bit of time trying to figure out the Settings and how to get the plist setup correctly. I first created a folder in MonoDevelop called Settings.bundle with the root.plist inside. It has to be "partly" correct because the application now appears in the Settings menu - which it wasn't before - BUT when you select it the screen is empty of inputs :-(
I'm probably missing something really basic - the root.plist file is shown in the editor below:

Anyway, I'll do some more reading and try again tomorrow!

Obligatory Interface Builder screenshot...

Wednesday 26 August 2009

MonoTouch "Restaurant Bill Splitter"

Okay - my first "real" iPhone via MonoTouch beta application is complete... welcome to Restaurant Bill Splitter, the easiest way to determine who pays the check :-)

Here it is before any data entry and after you have entered the total restaurant bill (plus the subtotal of any alcohol in that figure) and chosen the number of drinkers versus non-drinkers who are going to pay.



Here is the Interface Builder screen shot


You can also download the MonoTouch BillSplitter solution (11Kb) and view the c# code for Main.cs and MainWindow.xib.designer.cs. Even though MonoTouch is currently still in (closed) beta, it is VERY cool to be able to write c#/.NET targetting the iPhone platform. There is still quite a bit to get my head around (mostly the Apple stuff: XIBs, Views, delegates, events, Interface Builder, etc) but hopefully once I get the hang of it, much more complex applications will be forthcoming...

NOTE: there is one known bug in there... I'm having trouble making the keyboard disappear when you press the [Done] button - so if you're wondering what all the ResignFirstResponder() delegates are for, it's my attempt to debug/fix the keyboard problem. That is also the reason for the [Split] button, which otherwise wouldn't really be required since the calculations happen 'on change' for each of the input controls...


UPDATE: this is the class diagram for the app (obviously excludes stuff *in* the XIB, but includes the .xib.designer.cs). I've also posted the class diagrams for Foundation and UIKit online.

Tuesday 25 August 2009

MonoTouch "Hello World"

First, let me say that this is the MonoTouch team's "Hello World" code. I'm still trying to figure this whole MonoTouch thing out, so I've given @redth's YouTube screencast a try, and posted my own screenshots (even though they look a lot like these)...

Massive thanks for @redth for the screencast - hopefully I'll be posting something of my own creation very soon. It made a lot more sense (more quickly) than reading.

Anyway, here's the quick links:

0. Read "Hello World" on the MonoTouch official site

1. Watch @redth's screencast

2. Check out my screenshots
Here are a couple of them...


Wednesday 19 August 2009

Parsing XML with PHP4

PHP is not something I'm familiar with, but I was recently helping out a friend and was surprised how difficult it was to find some sample code for downloading/parsing an XML (specifically using PHP4, apparently PHP5 has some built-in functionality).

Thankfully I stumbled across Taha Paksu's SimpleXML for PHP4 - you must join/login to download it, but it's worth it.

Once you have downloaded the ZIP file, place simplexml.class.php in your PHP 'app', and use the supplied example test.php to ensure everything is working:
<?
require_once "simplexml.class.php";

echo "<pre>";
$file = "http://musicbrainz.org/ws/1/track/?query=metallica&type=xml";
$sxml = new simplexml;
$data = $sxml->xml_load_file($file);
print_r($data);
?>
which prints out a 'tree view' of the XML data.

If that works, you can then address individual elements (say your XML looks like this - an example from the download)
<?xml version="1.0" encoding="ISO-8859-1"?>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>
<format>text</format>
<content>Don't forget me this weekend!</content>
</body>
</note>
then your PHP might look like this:
<?
require_once "simplexml.class.php";

$file = "http://SOMESERVER.com/example.xml";
$sxml = new simplexml;
$data = $sxml->xml_load_file($file);
?>
<html>
<body>
<table>
<td>to:</td><td><?php echo $data->to; ?></td>
<td>sender:</td><td><?php echo $data->from; ?></td>
<td>subject:</td><td><?php echo $data->heading; ?></td>
<td>message:</td><td><?php echo $data->body->content; ?></td>
</table>
</body>
</html>


You can read more in the SimpleXML for PHP4 forum... That's enough PHP for now - back to c#/t-sql/xaml/html/javascript... and hopefully soon MonoTouch!

Friday 14 August 2009

When Mono "just works" - Searcharoo 'ported'

Decided to have another play with Mono today, in preparation for MonoTouch to come out of beta (or for my beta-participation to be approved)...

It was very easy to install Mono and MonoDevelop on my Mac, and because MonoDevelop supports Visual Studio 2008 solution files the current release of Searcharoo opened straight away*, compiled and could be tested with the 'provided' XSP2 webserver:


* I removed the Silverlight project from the Searcharoo solution (playing with Moonlight is a project for another day), but the Silverlight XAP from my Windows machine runs happily on the Mac against the Mono-version of the website:


The other cool thing about MonoDevelop is that it already supports the iPhone/MonoTouch 'project types'. You can't compile/test/run anything (you need MonoTouch.Foundation and MonoTouch.UIKit which are in 'private beta'), but you can browse the MonoTouch Samples - so at least you can see what C# code for the iPhone looks like. Fingers crossed Miguel and the team release the bits publicly ASAP :-)

Wednesday 12 August 2009

City2Surf 2009 on RaceReplay.net with Flickr

The world's "largest timed footrace" - Sydney's City2Surf - is now viewable on RaceReplay.net thanks to Silverlight and BingMaps.

There are two 'new' features:

1) Flickr photo integration (try it)

Using the Flickr photo search API, the Silverlight client renders small pink-and-blue dots for photos that are both geo-tagged and tagged:"city2surf". Clicking on the dot will open the Flickr page.



2) Visualizing the entire race (try it)

It is (unfortunately) impossible to animate the 65,000 or so points that would represent every runner and walker taking part in the event. By aggregating the finishers into timebands (and using both size & opacity to represent 'value') you can get some idea of the distribution of participants over time, over the course.



Add yourself!

As always, you can search for and add more 'runners' to the animation. Start typing a name into the autocomplete box, select from the list (optionally choose a color from the color-picker) and click Add runner.