Tuesday, 10 November 2009

iPhone Obj-C books for MonoTouch devs (first look)

iPhone BooksI recently received yet another addition to my 'Objective-C books for iPhone Development' collection - initially started to learn Obj-C (never happened) they are now more of a reference while learning MonoTouch.

Firstly, I have NOT read them cover-to-cover... I've done a bit of flicking, a bit of skimming and read a chapter or two. Secondly, I'm not really evaluating them according to their original purpose - to teach Objective-C - rather I'm making a personal judgement on their suitability as reference material for .NET developers.

I've listed them in their "order of usefulness" for MonoTouch .NET devs (according to me :)

Beginning iPhone Development
Dave Mark; Jeff LaMarche (APress)

This was the first iPhone book I purchased, so I've read it more closely than the rest. I learned quite a bit from various examples and discussions in this book - it's probably at the top of the list for this reason. The writing style is easy to read and there isn't too much emphasis on Xcode (other books do seem to focus on Xcode, the Head First one most heavily).

The initial chapters are probably best replaced with a MonoTouch 'Hello World' screencast - you don't really need to know all the Xcode, pch files and @synthesize that the book discusses (and there are quite a few free resources around that cover "your first MonoTouch app").

Pros
• Interface Builder examples are relevant
• Friendly colloquial tone
• Widest variety of topics (includes localization, accelerometer)

Cons
• Large swaths of Objective-C in some chapters
• Still need to decode Objective-C in the examples

Overall, though, my favorite of the bunch.

iPhone Developer's Cookbook
Erica Sadun (Addison Wesley)

I'm a big fan of Erica's from her blog and applications when the iPhone and iPod Touch first appeared and were programmable. Text is easy to read. Erica often references "when the iPhone first appeared" or "during the Beta period" - often interesting insights that add some fun to the text. There are also references to many 'undocumented' APIs - they're clearly highlighted to avoid confusion, but unfortunately they're even more difficult to use from MonoTouch than Objective-C... I felt a bit "left out" on first reading, although with btouch in MonoTouch 1.2 I'm wondering whether it might be easier to access these APIs now? (even though we're not supposed to...)

Chapter 2 - Views - does a really nice job of gradually introducing various concepts, and the Objective-C is minimal. That chapter on it's own is probably worth reading for anyone coming to iPhone programming. The rest of the first half is also fairly easy to follow (View Controllers, Basic Tables) - if the examples were in C# they might be my favorite 'introduction to iphone programming' :)

Towards the end of the book the amount of Objective-C does increase to a few multi-page listings that are more difficult to read (and probably beyond beginning C#ers) but overall the book is good value.

There is a 2nd edition due in January 2010 which will probably address my other main concern with this book: that it pre-dates the latest iPhone OS releases and hardware. Not a big deal really, but it's always nice to have info on the latest and greatest.

iPhone SDK
Jonathan Zdziarski (O'Reilly)

Less "reading" in this book - rather than longer explanatory paragraphs the author uses smaller chunks of text interspersed with bullet points, callouts and small and large Objective-C listings. This actually makes it better as a reference book than a front-to-back learning tool - and that makes it more suitable for MonoTouch in my view.

As with the other titles, the initial chapters cover Objective-C concepts like messaging, implementation, protocols, categories and posing(!) which you aren't likely to need as you start out with MonoTouch (although you might return to read about them later, to get your head around more complex examples).

I've used the remainder of the book to 'look up' various things that I want to learn about or need examples of, usually as a backup/alternative to the material that I've found in other books or on the web. It doesn't have the breadth of "Beginning iPhone Dev" nor the easy readability of Erica's stuff, but I have definitely picked up some useful tidbits from this book.


Head First iPhone Development
Dan Pilone; Tracey Pilone (O'Reilly)

I love the Head First series from O'Reilly, so I was determined to like this book; and it definitely lives up to it's promise as a Head First title. The writing, imagery, exercises and screenshots are all as you'd expect and it is really good at explaining Objective-C, Xcode and Interface Builder concepts... however that's partly what makes this volume less useful for C#ers: the writers go out of their way to explain/show how to do everything with Xcode and Interface Builder! As a MonoTouch developer I find myself only in MonoDevelop writing code and building my interfaces in code... so to my dismay I find myself unable to put this book at the top of my list.

It is still a fun read, and the non-techy bits (of which there are many) are extremely relevant to ALL iPhone devs. The recommendation to design your UI on paper, to follow Apple's Human Interface Guidelines, to remember to test on a Touch, and much more... if I ever decide to really learn Objective-C, I'll probably come back to this book and run through all the examples from go-to-whoa since it does try to teach the language and the SDK (and I really need help with my memory management!).


iPhone Cool Projects
Bennett et al (Apress)

The most 'attractive' of the five, due to the easy-to-read typesetting and colorful (yes COLOR) layout and imagery; this book probably doesn't really even belong in the list. It is more of a 'discussion' of iPhone application design - there is a fair amount of Objective-C throughout but it's mainly used to illustrate points of discussion, and the chunks are small enough that a C# developer can easily scan them.

Definitely fun to read - but you'll want any/all of the other books first to advance your knowledge of the iPhone SDK and Objective-C before you start trying to use the examples in this book. It probably isn't a good choice as a 'reference' or a learning tool for C#ers until you've got a few apps under your belt.


Finally...
As time passes and my knowledge increases my opinion of these books might change. Perhaps others have different opinions on what makes a good "Obj-C for C#" book, or have come across other titles? I'm sure there'll be C#/MonoTouch specific titles on the market eventually - it will be interesting to see how they read too!

Monday, 9 November 2009

Another MonoTouch 'conference app' - PDC09

PDC09I'm not planning on making a habit of this, but having spent the time putting together the Monospace conference app and seeing tweets from various Mono people that they were heading to PDC09, I figured it wouldn't take too long to copy some PDC data into the existing MonoTouch C# app 'framework'.

A new view (Sessions), a new icon (Speakers) and a new location (Underground) are the main changes - the rest is just SQLite and graphic tweaks. The source is available for download - PDC09_01.zip (124Kb).



NOTE: This is for demonstration purposes only - the Speakers data is incomplete and the Schedule & Sessions are subject to change! Use at your own risk :)

UPDATE: to be clear, this app is not available on the AppStore -- it is just an example of the kind of thing you can write in C# for the iPhone using MonoTouch from Novell. If you have a MonoTouch licence then you can download the source (124Kb) and install it on your phone.

Monday, 2 November 2009

MonoTouch UINavigationController Placemat

In one of my other posts I included a 'placemat' diagram to help make sense of the code.

Someone commented that they found the presentation useful, so as a quick post today I decided to placemat the Roget's 1911 Thesaurus sample.

Rogets 1911 Placemat

Note that I'm not suggesting this is the neatest, tidiest, best-practice way to structure a MonoTouch application (I'm learning along with everyone else). This is purely to help you find your way around the example code.

For example... on thing I learned is that I didn't really need to pass an instance of the 'root' MainViewController around - each UIViewController has a NavigationController property referencing the UINavigationController that 'contains' it. Therefore any given UIViewController that has been added to a UINavigationController can call this.PushViewController (or else pass a reference to this into the UITableViewDelegate constructor to use in RowSelected.

Monday, 26 October 2009

MonoTouch "Corp411" (part III: Revenge of System.Data)

As promised, the Corp411 sample app has been 'converted' from sqlite-net to System.Data. MonoTouch 1.2 isn't quite out yet, so you must follow the instructions on this page to download the System.Data assemblies to your installation, put them in the correct place AND add references to your MonoDevelop solution for Mono.Data.Sqlite.dll, Mono.Data.Tds.dll, System.Data.dll, System.Transactions.dll (I'm not sure if they're all required, but it seemed like a sensible set).

It's pretty easy to use after all that:

// db is the path to the SQLite database file
var conn = new SqliteConnection("Data Source=" + db);
and
listData = new List<Employee>();
// System.Data from http://monotouch.net/Documentation/System.Data
var sd = new SystemDataHelper("phonebook");
var connection = sd.GetConnection();
using (var cmd = connection.CreateCommand())
{
connection.Open ();
cmd.CommandText = "SELECT Firstname, Lastname, Work, Mobile,"
+ " Department, Email "
+ " FROM Phonebook ORDER BY Lastname";
using (var reader = cmd.ExecuteReader ())
{
while (reader.Read ())
{
var emp = new Employee();
emp.Firstname = (string)reader["Firstname"];
emp.Lastname = (string)reader["Lastname"];
emp.Work = (string)reader["Work"];
emp.Mobile = (string)reader["Mobile"];
emp.Department = (string)reader["Department"];
emp.Email = (string)reader["Email"];
Console.WriteLine("Column {0}",reader["Lastname"]);
listData.Add(emp);
}
}
}
Cool eh? Doesn't that look familiar... good old ADO.NET :-)


THE CODE
That's a small part of the System.Data namespace, so the code is pretty small :) here 'tis Corp411.3.zip (38Kb)

... and proof it runs on a device ...

Friday, 23 October 2009

MonoTouch "Corp411" (part II)

A minor update to the Corporate Phonebook incorporating the custom UITableViewCell from Simon (UITableView now shows name and department in each cell; also added a disclosure indicator). Handles RowSelected and uses OpenUrl to call and email.

The MonoDevelop solution is available to download (41Kb).



//TODO: give the new MonoTouch 1.2 System.Data implementation a try!

//HACK: System.Data example now available (26-Oct)

Wednesday, 21 October 2009

MonoTouch flip-flop with UIView animation

MonospaceThanks to Beginning iPhone Development's Objective-C examples the Monospace app now does a flip-flop between two views (the map and a new list of map locations to 'pan to'). The source code is available for download (you'll also notice the UI has been made "black"... I just like the way it looks).

Monospace22.zip (188Kb)




The view is driven by the MapFlipViewController (which is what we add to the TabViewController). It is responsible for creating/displaying the two 'flippable' views using UIView animation features.

The two views - MapViewController & MapLocationViewController - have a constructor which accepts a reference to the 'flipper' so they can call back to it's Flip() method, which looks something like this...

UIView.BeginAnimations("Flipper");
UIView.SetAnimationDuration(1.25);
UIView.SetAnimationCurve(UIViewAnimationCurve.EaseInOut);
if (mapView.View.Superview == null)
{
Console.WriteLine("to map");
UIView.SetAnimationTransition
(UIViewAnimationTransition.FlipFromRight, this.View, true);
locationView.ViewWillAppear(true);
mapView.ViewWillDisappear(true);

locationView.View.RemoveFromSuperview();
this.View.AddSubview(mapView.View);

mapView.ViewDidDisappear(true);
locationView.ViewDidAppear(true);
}
else
{
Console.WriteLine("to list");
UIView.SetAnimationTransition
(UIViewAnimationTransition.FlipFromLeft, this.View, true);
mapView.ViewWillAppear(true);
locationView.ViewWillDisappear(true);

mapView.View.RemoveFromSuperview();
this.View.AddSubview(locationView.View);

locationView.ViewDidDisappear(true);
mapView.ViewDidAppear(true);
}
UIView.CommitAnimations();
There is other code to actually set the map to a specific location - but that's actually pretty simple :)
mapView.SetLocation(toLocation);

Monday, 19 October 2009

Monospace app .1 release

MonospaceThe first version of the Monospace schedule app had a few issues (extra UITableView lines, for example); here is an updated version (with at least one less bug):

Monospace21.zip (183Kb)

A couple of additional features were also added...

UITabBarController More & Edit
Two of the cool things about UITabBarController are (a) when you add more than five items, it automatically creates the More... option; and (b) when the More list is visible you can also edit the order/position of items in the tab bar. Bonus!

There is a new 'Sponsors' ViewController to show this functionality:

I've used a UIWebView with images saved locally as a quick hack to get the sponsors page working. Setting the baseUrl makes is easy to reference images saved in the application bundle from local Html.

string basedir = Environment.GetFolderPath 
(System.Environment.SpecialFolder.Personal);
basedir = basedir.Replace("Documents", "Monospace2.app");
webView.LoadHtmlString(FormatText(),
new NSUrl(basedir+"/Sponsors/", true));
CoreLocation with MapKit
The MapViewController now also uses CoreLocation.CLLocationManager and custom delegate to track your location and calculate the distance to the conference center:

It also includes a new UISegmentedControl to change the map style.