Monotouch – Tips and Tricks (Part 1)
I recently had the joy of writing the IPhone-implementation of our amazing Fusonic EventGuide Product.
Because of my C# .NET/Mono experience and love it was obvious to reuse skills and libraries and so we bought a licence of MonoTouch. This was after the lay-off of the monotouch team, but luckily mtouch 4.0 is very mature and from my point of view it is definitely worth investing this money even though you will have to buy the Xamarin IOS version sooner or later.
Although programming against the Monotouch APIs using monodevelop was a real joy and really straight forward, you will come over some problems or nasty bugs from time to time. The goal of my blog series is to summarize my experiences in the last two weeks, show you up some common pitfalls and tell you some tips + tricks and useful libraries I used for our product.
- Why is my app being killed when the debugger is attached after some seconds?
- Simulator does not start automatically or iphone shows a black screen (black screen bug)
This is a very annoying bug and I do not have a solution that will work in every case. Although here are some steps you can try to get back into game again:
But I have to warn you, the following may sound insane, but unfortunately I do not have a real profound solution:- Delete your simulator content (from the simulator menu choose: iPhone Simulator => Reset Content and Settings)
- Create a new empty monotouch project in your solution, set it as a startup project and let it run
- If the new project will show up in your simulator you have to copy over all files from the old project into the new one. Now your app should hopefully run again.
If someone has a more profound solution for solving this annoying problem, I’d be happy hearing it!
- Convert System.DateTime to NSDate via extension methods
- Do NOT name your resource-folders “Resources” or “resources”
You will likely need some resources in your application like icons, images or other files. In the .net-world it is common to add these files to a folder named “R/resources”, but in the iphone world you are not allowed to do so because this folder is already reserved for other purposes. If you try using a folder named “resources” (lowercase), it will work when running in the simulator, but as soon as you do a distribution build with code signing, Itunes will quit your installation with the following error: The app “YourAppName” was not installed on the IPhone “Test Iphone” because the app signature is not valid.
So in order to fix this => rename your folder :)
It’s important to know that your Iphone will kill your application if the startup process takes longer than 10 seconds. That means that you can not debug your FinishedLaunching method in the App-Delegate for longer than 10 seconds. I do recommend invoking the BeginInvokeOnMainThread method in the FinishedLaunching method. This way the method will return in time and your initialization code will be executed in a new dispatcher stack frame where you can happily debug it for as long as you want:
public override bool FinishedLaunching (UIApplication app, NSDictionary options)
{
window.AddSubview(navigationController.View);
window.MakeKeyAndVisible ();
BeginInvokeOnMainThread(() => {
// Invoke your initialization code here!
});
}
You will need to convert NSDate to DateTime and vise versa when using local notifications. This is how you can easily do it using extension methods:
public static class DateExtensions
{
public static NSDate ToNSDate(this DateTime dateTime)
{
return NSDate.FromTimeIntervalSinceReferenceDate((dateTime-(new DateTime(2001,1,1,0,0,0))).TotalSeconds);
}
public static DateTime ToDateTime(this NSDate nsDate)
{
return (new DateTime(2001,1,1,0,0,0)).AddSeconds(nsDate.SecondsSinceReferenceDate);
}
}
I hope you enjoyed reading this first part of my monotouch blog series. In Part-2 of my blog-series I wrote about:
- How to create and use NSNotifications
- How to simulate NSNotifications behavior when the application has already been killed. (handling it in the FinishedLaunching method)
- A simple UIAlertView helper.


[...] – Monotouch – Tips and Tricks (Part 2) – Monotouch – Tips and Tricks (Part 1) [...]