Friday, 25 January 2013

Storing and Retrieving Setting Values

Some times it is required to store few setting of the application. The .NET programming world in Windows, setting can be used for this purpose. In Objective C, NSUserDefaults is used for this purpose. It is very simple and straight forward.
To store a setting value:

And to retrieve stored setting:
Please also note that in case of retrieving a value which is not stored, ValueForKey returns Null (nil in Objective C) and thus, calling .ToString() method, causes an exception.

Thursday, 24 January 2013

Auto close application after main form closes

Your Xamarin.Mac Cocoa application would not terminate after you close the main form. Automatically terminating application when the last form is closed is some thing that C# developers in Windows used to it. In Cocoa application, in order to have the same functionality, you need to override a method in AppDelegate.cs:

Get Mac Computer's Unique Id in Xamarin.Mac

I've been developing and publishing many betting tools for a while and for all of those applications, we had some kind of activation process in which a unique identifier for each computer is generated and based on that we authorize the computer and so forth. For Windows, I used a combination of MAC address with CPU serial number to generate this machine specific unique identifier. For Mac computers, I searched the internet for unique identifier and found many commands which return a supposedly unique identifier, here is a link of one of them.

As I said in my previous post, there is not that much code samples for MonoMac and Xamarin.Mac, but for Objective C, there are good samples. And that's why for a Xamarin.Mac developer it is really required to be familiar with Objective C.

In this post, I'm providing the code translation of the function which was originally written in Objective C (which is taken from the StackOverflow) to get the unique identifier of a Mac computer:

Hello Mac!

Being totally new to the development world of Mac, with my .NET and Windows programming background, I've experienced and actually experiencing many new things in this new world. I've decided to share my experience here on my blog.

Having .NET and C# background, I really preferred to use Mono and MonoMac. Eventually when I see the style of programming, I was totally disappointed. I was mostly thinking that because I'm not using the very original tools (mean the Objective C and XCode), that's why every thing seems to be this hard. I should also mention that when I'm talking Mono, I'm not really meaning to use WinForms on Mac. They really really look very odd and actually no Mac user prefers that style. So the option is MonoMac. Anyway, so I was convinced that I really need to learn Objective C in depth. So I started with few books and videos. Learner Objective C in depth (not really ;) ) and the Foundation framework and Cocoa and so forth. After around a week or a more, I was thinking that how easy was MonoMac! But I tried to continue with Objective C. I was thinking that Mac world is totally different form Windows world and I should not think the way I thought in Windows and etc. I talked with many Mac developers and .NET developers about my experience with Objective C. Mac developers mostly said that this is a completely new world, however, the amount of work to create simple things in XCode and Objective C was not really acceptable for me. With C# .NET and Visual Studio I was very very fast. XCode and Objective C remembered me MFC applications! You really need to take care of every details and write too much frustrating codes and so forth.

Objective C really looks like an out dated programming language. However, in its version 2.0 many new features have been added, but still too far from an elegant and modern language such as C#. As a result, I retried to use MonoMac as well as Xamarin.Mac. I go ahead and purchased a professional license of Xamarin.Mac and I'm actually experiencing new things with it. There are few issues with Xamarin.Mac. The developers are not that much and thus, you can not find many sample codes for it on internet. Furthermore, you hardly find answers to your questions. However, issues are them same for Objective C developers. Thus, you can find solutions to your problems in Objective C and by knowing a little bit of its weird syntax, you can easily convert that code in to C#.

Anyway, this was a brief history of my experience in Mac development world and I'll be posting new things here.