Miguel's OSX and iOS blog Miguel's OSX and iOS blog http://tirania.org/monomac//index.html Miguel de Icaza miguel@gnome.org Thu, 19 Apr 2012 18:12:00 -0500 http://backend.userland.com/rss lb# Key-Value-Observing on MonoTouch and MonoMac <p>This morning Andres came by IRC asking questions about Key Value Observing, and I could not point him to a blog post that would discuss the details on how to use this on C#. <p> <a href="https://developer.apple.com/library/mac/#documentation/cocoa/Conceptual/KeyValueObserving/KeyValueObserving.html">Apple's Key-Value Observing</a> document contains the basics on how to observe changes in properties done to objects. <p>To implement Key-Value-Observing using MonoTouch or MonoMac all you have to do is pick the object that you want to observe properties on, and invoke the "AddObserver" method. <p>This method takes a couple of parameters: an object that will be notified of the changes, the key-path to the property that you want to observe, the observing options and a context object (optional). <p>For example, to observe changes to the "bounds" property on a UIView, you can use this code: <pre class="code-csharp"> view.AddObserver ( observer: this, keyPath: new NSString ("bounds"), options: NSKeyValueObservingOptions.New, context: IntPtr.Zero); </pre> <p>In this example, I am using the C# syntax that uses the Objective-C style to highlight what we are doing, but you could just have written this as: <pre class="code-csharp"> view.AddObserver ( this, new NSString ("bounds"), NSKeyValueObservingOptions.New, IntPtr.Zero); </pre> <p>What the above code does is to add an observer on the "view" object, and instructs it to notify this object when the "bounds" property changes. <p>To receive notifications, you need to override the ObserveValue method in your class: <pre class="code-csharp"> public override void ObserveValue (NSString keyPath, NSObject ofObject, NSDictionary change, IntPtr context) { var str = String.Format ( "The {0} property on {1}, the change is: {2}", keyPath, ofObject, change.Description); label.Text = str; label.Frame = ComputeLabelRect (); } </pre> <p>This is what the app shows if you rotate your phone: <center> <img src="http://tirania.org/s/5a85f5d0.png"> </center> <p>The complete sample has been uploaded to <a href="https://github.com/xamarin/monotouch-samples/tree/master/KeyValueObserving">GitHub</a>. http://tirania.org/monomac/archive/2012/Apr-19.html miguel@gnome.org (Miguel de Icaza) http://tirania.org/monomac/archive/2012/Apr-19.html Thu, 19 Apr 2012 22:12:00 -0500 Call for Comments: Strongly Typed Notifications <p>I am adding support for strongly typed notifications to MonoTouch and MonoMac. The idea behind this is to take guesswork, trips to the documentation and trial and error from using notifications on iOS and MacOS. <p>The process is usually: (a) find the right notification; (b) look up apple docs to see when the notification is posted; (c) look up each of the keys used to retrieve the data from the dictionary. <p>Currently, listening to a notification for a keyboard-will-be-shown notification looks like this in MonoTouch: <pre> void DoSomething ( UIViewAnimationCurve curve, double duration, RectangleF frame) { // do something with the above } var center = NSNotificationCenter.DefaultCenter; center.AddObserver (UIKeyboard.WillShowNotification, PlaceKeyboard); [...] void PlaceKeyboard (NSNotification notification) { // Get the dictionary with the interesting values: var dict = notification.UserInfo; // Extract the individual values var animationCurve = (UIViewAnimationCurve) (dict [UIKeyboard.AnimationCurveUserInfoKey] as NSNumber).Int32Value; double duration = (dict [UIKeyboard.AnimationDurationUserInfoKey] as NSNumber).DoubleValue; RectangleF endFrame = (dict [UIKeyboard.FrameEndUserInfoKey] as NSValue).RectangleFValue; DoSomething (animationCurve, duration, endFrame) } </pre> <p>Currently we map the Objective-C constant "FooClassNameNotification" into the C# class "Foo" as the member "NameNotification" of type NSString. <p>What we want to do is to expose the notifications as strongly typed C# events. This will provide auto-complete support in the IDE to produce the lambdas or helper methods, auto-complete for all the possible properties of the notification, strong types for the data provided and live documentation for the values in the notification. <p>This means that the above code would instead be written like this: <pre> var center = NSNotificationCenter.DefaultCenter; center.Keyboard.WillShowNotification += PlaceKeyboard; void PlaceKeyboard (object sender, KeyboardShownEventArgs args) { DoSomething (args.AnimationCurve, args.Duration, args.EndFrame); } </pre> <p>The question is where should these notifications be exposed in the API? In the example above we do this by the event "WillShowNotification" on a class "Keyboard" inside the "NSNotificationCenter". We have a few options for this. <p>We could host the notification in the class that defines the notification, but we would have to come up with a naming scheme to avoid the name clash with the existing NSString constant: <pre> class UIKeyboard { public NSString WillShowNotification { get; } // replace "Notification" with the class name: public event EventHandler<KeyboardShowEventArgs> WillShowKeyboard; // prefix the event: public event EventHandler<KeyboardShowEventArgs> KeyboardWillShow; // plain, does not work on all types though: public event EventHandler<KeyboardShowEventArgs> WillShow; } // Consumer code would be one of: UIKeyboard.WillShowKeyboard += handler; UIKeyboard.KeyboardWillShow += handler; UIKeyboard.WillShow += handler; </pre> <p>Another option is to add everything into NSNotificationCenter: <pre> class NSNotificationCenter { // Existing implementation public event EventHandler<KeyboardShowEventArgs> UIKeyboardWillShow; // Another 141 events are inserted here. } // Consumer code would be: NSNotificationCenter.DefaultCenter.UIKeyboardWillShow += handler; </pre> <p>Another option is to partition the notifications based on their natural host, this is my personal favorite, but could be harder to find with the IDE using code completion: <pre> class NSNotificationCenter { public static class Keyboard { public static event EventHandler<KeyboardShowEventArgs> WillShow; } } // Consumer code would be: NSNotificationCenter.Keyboard.WillShow += handler; </pre> <p>All of these proposals have one potential problem: they would all assume that all of these interesting notifications are always posted into the NSNotificationCenter.DefaultCenter. <p>Apple's documentation does not seem to suggest that any of the iOS notifications are posted anywhere but the DefaultCenter. I could not find on GitHub any code that would use anything but the DefaultCenter. <p>On MacOS the InstantMessage framework posts notifications to its own notification center. We could just bind those events to this specific NSNotificationCenter. <p>Thoughts? http://tirania.org/monomac/archive/2012/Apr-12.html miguel@gnome.org (Miguel de Icaza) http://tirania.org/monomac/archive/2012/Apr-12.html Thu, 12 Apr 2012 21:06:00 -0500 MonoMac Updates <p>We have been hard at work at improving the MonoMac API to allow .NET developers to create native Mac applications using C#, F#, IronPython or their favorite .NET language. <p>There are couple of goodies coming on our next release of MonoMac: our Lion support is shapping up and we have been dogfooding this ourselves with our own apps. <p>One of our sample apps, a simple front-end to the Mono Documentation backend is now complete enough that we are going to deprecate the Gtk+ version of it and replace it with the native version of it. <p>MacDoc now has several new features <p><b>Apple documentation integration:</b> MacDoc will now download the Apple docs if they are not available and blend its contents with our documentation and replace the Objective-C samples with C# samples. Amazing! <p><b>Full text indexing:</b> the documentation browser is using Lucene to index all of the contents and allow you to quickly find the materials that you are looking for. <center> <img src="http://tirania.org/s/69c08dbc.png"> </center> <p><b>Conceptual Index:</b> in addition to the full text search, we generate a curated version of the APIs that are useful for performing search-as-you-type in the documentation browser. This is useful to find APIs by class, by method name and also by Objective-C selector. This means that you can now search for Objetive-C selectors in our documentation, and you will get the actual mapping to the C# method. <center> <img src="http://tirania.org/s/1cb8b516.png"> </center> <p>Supports Lion documents, saved state and bookmarks. <p>We extended the ECMA XML format so it now renders images for our docs: <center> <img src="http://tirania.org/s/94a15a6a.png"> </center> <p>The source code is available now in <a href="https://github.com/mono/monomac/tree/master/samples/macdoc">GitHub</a> and will be shipping in the upcoming MonoDevelop 2.8.8 release. http://tirania.org/monomac/archive/2012/Mar-06.html miguel@gnome.org (Miguel de Icaza) http://tirania.org/monomac/archive/2012/Mar-06.html Tue, 06 Mar 2012 21:34:00 -0500 Bubbles <p>Recently one of our customers asked about how to implement a conversation display similar to the iOS SMS/Messages display. You can find the <a href="https://github.com/xamarin/monotouch-samples/tree/master/BubbleCell">BubbleCell sample</a> in our Github repository. <p>This is what the conversation looks like: <center> <img src="http://tirania.org/s/c38c418a.png"> </center> <p>To implement this, I used iOS's UITableView as it already provides a lot of the functionality that we need for this. What I did was to write a custom UITableViewCell that can render bubbles with their text. <p>I wrote both a MonoTouch.Dialog Element that you can host in your DialogViewController as well as a custom UITableCellView which can be reused by those using UITableViews directly. <p>This is how you could populate the initial discussion inside MonoTouch.Dialog: <pre class="code-csharp"> Section chat; var root = new RootElement ("Chat Sample") { (chat = new Section () { new ChatBubble (true, "This is the text on the left, what I find fascinating about this is how many lines can fit!"), new ChatBubble (false, "This is some text on the right"), new ChatBubble (true, "Wow, you are very intense!"), new ChatBubble (false, "oops"), new ChatBubble (true, "yes"), }) };</pre> <p>And this is how you would add a new element to the conversation: <pre class="code-csharp"> chat.Section.Add ( new ChatBubble (false, "I want more cat facts"));</pre> <h2>Implementation</h2> <p>Bubble rendering is implemented in Bubble.cs and contains both the UITableViewCell as well as the element. It follows the pattern for creating UITableViewCells that I <a href="http://tirania.org/monomac/archive/2011/Jan-18.html">documented before</a>. <p>Each cell is made up of two views: one contains a UIImageView that paints the bubble and the other one contains the text to render inside the bubble. <p>This is what the two bubbles images look like: <center> <img src="http://tirania.org/s/dba416d6.png"> </center> <p>We load these using UIImage.FromFile and then use the iOS 5.0 <a href="http://iosapi.xamarin.com/?link=M%3aMonoTouch.UIKit.UIImage.CreateResizableImage(MonoTouch.UIKit.UIEdgeInsets)">UIImage.CreateResizableImage</a> method to create a UIImage that can be stretched on demand. To create the resizable image we need to tell CreateResizableImage the region of the image that can be stretched. Anything outside of the UIEdgeInset will be kept as-is: <pre class="code-csharp"> left = bleft.CreateResizableImage (new UIEdgeInsets (10, 16, 18, 26)); right = bright.CreateResizableImage (new UIEdgeInsets (11, 11, 17, 18));</pre> <p>This will stretch the region highlighted in red, while rendering the external border as-is: <center> <img src="http://tirania.org/s/bstretch.png"> </center> <p>With the above code, the image will be rendered in a variety ways depending on the Frame that is assigned to the UIImageView that hosts our resizable UIImage: <center> <img src="http://tirania.org/s/ff8a3a6c.png"> </center> <p>The only remaining interesting bit in the code is to configure our UILabel properly. We want to set its BackgroundColor to UIColor.Clear to avoid painting the background in a solid color and we also specify that the text should be word-wrapped if it does not fit in a single line: <pre class="code-csharp"> label = new UILabel (rect) { LineBreakMode = UILineBreakMode.WordWrap, Lines = 0, Font = font, BackgroundColor = UIColor.Clear }; </pre> <p>Finally in our LayoutSubViews method we must compute the proper sizes for the bubbles and the text that goes in them. I made it so the bubbles did not take the entire space in a row. Instead they take 70% of the row to give a similar effect to the rendering of the iOS messages UI. The code is pretty straight-forward: <pre class="code-csharp"> public override void LayoutSubviews () { base.LayoutSubviews (); var frame = ContentView.Frame; var size = GetSizeForText (this, label.Text) + BubblePadding; imageView.Frame = new RectangleF (new PointF (isLeft ? 10 : frame.Width-size.Width-10, frame.Y), size); view.SetNeedsDisplay (); frame = imageView.Frame; label.Frame = new RectangleF (new PointF (frame.X + (isLeft ? 12 : 8), frame.Y + 6), size-BubblePadding); } </pre> http://tirania.org/monomac/archive/2012/Jan-30.html miguel@gnome.org (Miguel de Icaza) http://tirania.org/monomac/archive/2012/Jan-30.html Mon, 30 Jan 2012 18:22:00 -0500 Styling your controls in MonoTouch on iOS 5 <p>Starting with iOS 5 it is possible to more easily style your UIViews. Apple did this by exposing a new set of properties on most views that can be tuned. For example, to configure the TintColor of a UISlider, you would write: <pre> var mySlider = new UISlider (rect); mySlider.ThumbTintColor = UIColor.Red; </pre> <p>You can also set the color globally for all instances of UISlider, you do this by assigning the styling attributes on the special property "Appearance" from the class you want to style. <p>The following example shows how to set the Tint color for all UISliders: <pre> UISlider.Appearance.ThumbTintColor = UIColor.Red; </pre> <p>It is of course possible to set this on a per-view way, <p>The first time that you access the "Appearance" static property on a stylable class a UIAppearance proxy will be created to host the style changes that you have requested and will apply to your views. <p>In Objective-C the Appearance property is untyped. With MonoTouch we took a different approach, we created a strongly typed UIXxxxAppearance class for each class that supports styling. Our generated UIXxxxxAppearance class is strongly typed, which allows users to use intellisense to easily discover which properties are avaialble for styling. <p>We also created a hierarchy that reflects the inherited appearance properties, this is the class hierarchy for the <a href="http://docs.go-mono.com/index.aspx?link=T%3aMonoTouch.UIKit.UISlider%2bUISliderAppearance">UISLider.UISliderAppearance</a> class: <center> <img src="http://tirania.org/s/87cf5019.png"> </center> <p>The properties exposed by UISlider for example are: <pre> public class UISliderAppearance { public virtual UIColor BackgroundColor { get; set; } public virtual UIColor MaximumTrackTintColor { get; set; } public virtual UIColor MinimumTrackTintColor { get; set; } public virtual UIColor ThumbTintColor { get; set; } } </pre> <p>MonoTouch also introduced support for styling your controls only when they are hosted in a particular part of the hierarchy. You do this by calling the static method AppearanceWhenContainedIn which takes a variable list of types, it works like this: <pre> var style = UISlider.AppearanceWhenContainedIn (typeof (SalesPane), typeof (ProductDetail)); style.ThumbTintColor = UIColor.Red; </pre> <p>In the above sample the style for the ThumbTintColor will be red, but only for the UISliders contained in ProductDetail view controllers when those view controllers are being hosted by a SalesPane view controller. Other UISliders will not be affected. <p>Both the Appearance static property and the AppearanceWhenContainedIn static method have been surfaced on every UIView that supports configuring its style. Both of them return strongly typed classes that derive from UIAppearance and expose the exact set of properties that can be set. <p>This is different from the weakly typed Objective-C API which makes it hard to discover what can be styled. http://tirania.org/monomac/archive/2011/Oct-14.html miguel@gnome.org (Miguel de Icaza) http://tirania.org/monomac/archive/2011/Oct-14.html Fri, 14 Oct 2011 22:38:00 -0500 MonoTouch 5.0 is out <p>Yesterday <a href="http://blog.xamarin.com/2011/10/12/monotouch-5-with-ios-5-support/">we released MonoTouch 5.0</a>, the companion to Apple's iOS 5.0 release. <p>Apple tends to ship Objective-C APIs that are configured through an NSDictionary instance containing configuration keys. With MonoTouch 5.0, we continued our work to <a href="http://tirania.org/monomac/archive/2010/Dec-01.html">improve over NSDictionary-based bindings</a> by creating strongly-typed versions of those APIs. <p>In the next couple of days, I will be sharing some of the new features in iOS 5.0 and how to take advantage of those using C#. <p>Meanwhile, our documentation team has produced an amazing <a href="http://docs.xamarin.com/ios/tutorials/Introduction_to_iOS_5">Introduction to iOS 5.0 for C# developers</a> and put together some samples showing how to use some of the new features in iOS 5: <ul> <li><a href="http://docs.xamarin.com/@api/deki/files/323/=Storyboard.zip">Storyboard</a>: shows how to use Storyboards from C# and showcases the integration between Xcode 4 and MonoDevelop 2.8 <li><a href="http://docs.xamarin.com/@api/deki/files/320/=CoreImage.zip">CoreImage</a>: shows our bubilicious strongly-typed API for CIFilters, it is in my opinion, a huge usability upgrade over the NSDictionary-based approach. <li><a href="http://docs.xamarin.com/@api/deki/files/321/=iCloud.zip">iCloud</a>: Basic iCloud use. <li><a href="http://docs.xamarin.com/@api/deki/files/324/=Twitter.zip">Twitter</a>: Post new tweets and query twitter for data. <li><a href="http://docs.xamarin.com/@api/deki/files/322/=Newsstand.zip">Newsstand</a>: A complete sample showing how you can integrate with the new Newsstand APIs to publish your own periodicals. We wont be submitting this sample for the Apple Design Awards, but it shows how to use the framework. </ul> http://tirania.org/monomac/archive/2011/Oct-13.html miguel@gnome.org (Miguel de Icaza) http://tirania.org/monomac/archive/2011/Oct-13.html Thu, 13 Oct 2011 16:28:00 -0500 TestFlight support in MonoDevelop <p>We have just released for <a href="http://docs.xamarin.com/ios/tutorials/TestFlight_Support">TestFlight support</a> in MonoDevelop. <p>This makes it simpler for developers to deploy their Ad-Hoc builds directly to Testflight, we added a "Publish to TestFlight" option: <center> <img src="http://docs.xamarin.com/@api/deki/files/271/=testflight-1.png"> </center> <p>The first time you upload to TestFlight you must provide your authentication tokens: <center> <img src="http://docs.xamarin.com/@api/deki/files/268/=testflight-4.png"> </center> <p>And after that, the IDE takes care of the rest: <center> <img src="http://docs.xamarin.com/@api/deki/files/266/=testflight-6.png"> </center> <p>This is built on top of our enhanced <a href="http://docs.xamarin.com/ios/tutorials/IPA_Support_-_Ad_Hoc_and_Enterprise_Deployment">IPA Packaging support</a> in the same release. http://tirania.org/monomac/archive/2011/Sep-29.html miguel@gnome.org (Miguel de Icaza) http://tirania.org/monomac/archive/2011/Sep-29.html Thu, 29 Sep 2011 14:12:00 -0500 Sales Force app built with MonoTouch <p>I do not blog very often about apps built with MonoTouch, but <a href="http://www.justenough.com/NetSuite/MobileSFA/">this application</a> is drop-dead gorgeous. <p>It is a tool designed to be used by the sales force of a company. <p>You can download the app from the Apple AppStore and try it on "demo" mode. What I love about this application is how they took advantage of UIKit and CoreAnimation to create a beautiful enterprise app. It does not stop there, they use everything iOS has to offer: <center> <img src="http://a3.mzstatic.com/us/r1000/063/Purple/f5/ed/83/mzl.gtaqidjm.480x480-75.jpg"> <p> <img src="http://a2.mzstatic.com/us/r1000/061/Purple/37/da/11/mzl.wkykqmxl.480x480-75.jpg"> </center> <p>Enterprise software has a reputation for being hostile to end-users. This shows that you can create great end-user software for users in the enterprise. If you were looking for inspiration for your own enterprise apps, this is the app to look for. http://tirania.org/monomac/archive/2011/Aug-12.html miguel@gnome.org (Miguel de Icaza) http://tirania.org/monomac/archive/2011/Aug-12.html Fri, 12 Aug 2011 20:28:00 -0500 MonoMac add-in for MonoDevelop <p>After a small hiatus we are back. <p>If you have been using MonoMac to build MacOS applications, we have just released an update to the MonoDevelop.MonoMac add-in that should fix the problem with packaging your applications on Lion. <p>This update just contains a critical fix and delivers the add-in to all three MonoDevelop platforms in use today: our stable MonoDevelop 2.4, the 2.6beta3 and for the fearless among you MonoDevelop/master. <p>This was just the first step in maintaining the add-in. I had to sort out the build setup for all three branches and the pipeline to deliver the updates. Now that I got things in place, I will be able to fix some of the other problems that have been reported. <p>If you are running into problems with MonoMac, please file your bugs at the new home for the Mono bug reports at <a href="http://bugzilla.xamarin.com">http://bugzilla.xamarin.com</a>. http://tirania.org/monomac/archive/2011/Aug-03.html miguel@gnome.org (Miguel de Icaza) http://tirania.org/monomac/archive/2011/Aug-03.html Wed, 03 Aug 2011 21:40:00 -0500 Glass button for iPhone <p>Since iOS does not provide a default glossy button implementation I wrote my own, based mostly on looking at a screenshot of Apple's own. <p>Some folks have been using the <a href="http://conceptdev.blogspot.com/2010/08/uiglassbutton-generator-in-monotouch.html">UIGlassButton generator</a>, but I have wanted for a while to have this functionality avaialble at runtime, and not depend on pre-generated images, these are created at runtime, and behave just like a regular button: <center> <img src="http://tirania.org/pictures/mt-glass-button.png"> </center> <p>You can find <a href="https://github.com/migueldeicaza/MonoTouch.Dialog/blob/master/MonoTouch.Dialog/Utilities/GlassButton.cs">my implementation</a> as part of <a href="https://github.com/migueldeicaza/MonoTouch.Dialog/blob/master/MonoTouch.Dialog">MonoTouch.Dialog</a> on github. <p>This is how you would use it, and how you can customize some of its elements: <pre class="code-csharp"> var b = new GlassButton (bounds) { Font = UIFont.BoldSystemFontOfSize (22), NormalColor = UIColor.Green, HighlightedColor = UIColor.Red }; b.SetTitle ("Dismiss", UIControlState.Normal); container.AddSubview (b); </pre> http://tirania.org/monomac/archive/2011/Apr-08.html miguel@gnome.org (Miguel de Icaza) http://tirania.org/monomac/archive/2011/Apr-08.html Fri, 08 Apr 2011 14:19:00 -0500 MonoTouch 4.0 <p><img src="http://tirania.org/pictures/vintage-monotouch-logo.png" align="right">We just released MonoTouch 4.0, a product to build iOS applications using C# and .NET. We also released our new <a href="http://tirania.org/blog/archive/2011/Apr-06.html">Mono for Android</a> product. <h2>New in MonoTouch 4.0</h2> <p>MonoTouch 4.0 is a major upgrade to our product as it upgrades the Mono runtime engine from the old, trusted and friendly Mono 2.6 to the latest and greatest <a href="http://www.mono-project.com/Release_Notes_Mono_2.10">Mono 2.10</a> core, these are some of the new features available as part of this upgrade: <ul> <li>Parallel Frameworks for C#: Great APIs for building multi-threaded software. Not only this is great for iPad 2 users and developers, but it also simplifies just plain multi-threaded programming by exposing Futures, Tasks and Parallel LINQ to the developer. <li>LLVM Compiler Support: In addition to the fast Mono compilation engine, MonoTouch can now also use LLVM to create optimized builds. When you build MonoTouch applications using LLVM your executables will run faster, they will be smaller, and you can optionally opt into generating the nimbler ARMv7 or Thumb code (fat binaries are also supported). <p><b>Example:</b> My own <a href="http://tirania.org/tweetstation/">TweetStation distribution</a> went from 8 megs to 6 megs using Thumb + ARMv7 support. A very significant gain. <li>C# 4.0 and .NET 4.0: This release comes with the latest incarnation of the C# language as well as exposing the new .NET 4.0 APIs (many new functional constructs make for nicer looking code, like all the IEnumerable<Foo> enabled-methods in System.IO). <p>There is one important limitation: C# 4.0 dynamic support is not functional, since it requires dynamic code generation to work. <li>Upgraded WCF stack: We still consider this a preview of the full WCF but has been expanded significantly. <li>All new iOS 4.3 APIs have been exposed. <li>NSDecimal, NSDecimalNumber are now exposed (mostly for the sake of CorePlot :-) <li>Many new convenience APIs have been introduced. </ul> <p>For a full detailed list of changes, see <a href="http://monotouch.net/Releases/MonoTouch_4/MonoTouch_4.0.0">our MonoTouch 4.0 Release Notes</a>. <h2>Resources</h2> <p>The best source of information on parallel programming with Parallel FX is the free <a href="http://www.microsoft.com/downloads/en/details.aspx?FamilyID=86b3d32b-ad26-4bb8-a3ae-c1637026c3ee&displaylang=en">Patterns for Parallel Programming: Understanding and Applying Parallel Patterns with the .NET Framework 4</a>. <p>This is a brilliant document. Whether you use .NET or not, this is a recommended reading for everyone. <p>In addition to the <a href="http://www.amazon.com/dp/047063782X/ref=as_li_ss_til?tag=tiraniaorg-20&camp=213381&creative=390973&linkCode=as4&creativeASIN=047063782X&adid=12Z07C36B4TXR7Y4QRM4&">Programming iPhone with MonoTouch book</a> there are two new books about to hit the shelves <a href="http://www.amazon.com/gp/product/1430231742/ref=as_li_ss_tl?ie=UTF8&tag=tiraniaorg-20&linkCode=as2&camp=1789&creative=390957&creativeASIN=1430231742">Developing C# Apps for iPhone and iPad using MonoTouch: iOS Apps Development for .NET Developers</a>: an incredibly in-depth book from Brian Costanich that I have had the privilege to read in advance. This book will come out in only 3 weeks. <p>If you are more of a hands-on kind of guy, later in the year, Mike Bluestein's <a href="http://www.amazon.com/gp/product/0321719921/ref=as_li_ss_tl?ie=UTF8&tag=tiraniaorg-20&linkCode=as2&camp=1789&creative=390957&creativeASIN=0321719921">Learning MonoTouch: A Hands-On Guide to Building iPhone and iPad Applications with C# and .NET</a> is coming out. <h2>Next Steps</h2> <p>We are currently hard at work to add support to MonoDevelop to work with the new XCode 4. <p>With XCode 4, Apple removed Interface Builder as a standalone tool. We should have a beta in a couple of weeks of the solution we came up with. http://tirania.org/monomac/archive/2011/Apr-06.html miguel@gnome.org (Miguel de Icaza) http://tirania.org/monomac/archive/2011/Apr-06.html Wed, 06 Apr 2011 16:20:00 -0500 MonoMac 1.0 is out <p><a href="http://tirania.org/blog/archive/2010/Apr-19.html">Almost a year ago</a> we started building a set of Mono bindings for building native MacOS X applications. <p>Our original goals were modest: bind enough of AppKit that you could build native desktop applications for OSX using C# or your favorite .NET language. We leveraged a lot of the code that we built for MonoTouch our binding to the CocoaTouch APIs. <p>During the year, the project picked up steam, we got plenty of contributions to MonoMac and grew beyond the original conservative goals. <p>In a year we: <ul> <li>Created a beautiful library that <a href="http://www.mono-project.com/MonoMac#Background">blends the worlds of C# and MacOS X</a> APIs. <li><a href="http://mjhutchinson.com/journal/2010/06/09/monomac_in_monodevelop">Created a MonoDevelop add-in</a> that helps developers get started with Mac development in minutes. <li>Integrated the MonoDoc system into MonoDevelop, to provide developers with documentation on the flight as they type their code. Detailed method information, parameter use and type information is available as you type your code in unobtrusive windows. <li><a href="http://www.mono-project.com/MonoMacPackager">Created a packager</a> that turns your programs into self-contained OSX Packages with no external dependencies on Mono and can be deployed to the Apple App Store. <li>Created a linker that lets you strip out any functionality your application might not need to reduce your executable size. <li>Created a <a href="http://lists.ximian.com/pipermail/mono-osx/">great community of developers</a> that love C#, .NET and MacOS. For some of us, this is a step closer to heaven. <li>Created a <a href="https://github.com/mono/monomac/tree/master/samples">big pool of samples</a> for developers to learn from, and for us to exercise the API and ensure that the resulting library was a delight to use. <li>Created various <a href="http://www.mono-project.com/MonoMac#Tutorials">tutorials</a> on how to build applications with C# on the Mac. <li>Built an <a href="http://mono.ximian.com/monomac-docs/">online documentation mash-up</a> between our API and Apple's web documentation </ul> <p>Some statistics about the MonoMac binding: <ul> <li>1,155 C# classes and 31 C# structures <li>376 enumerations <li>123 C# delegate data types <li>16,556 methods, properties and events exposed </ul> <p>In addition to that, MonoMac <a href="http://mono.ximian.com/monomac-docs/MonoMac.OpenGL">bundles a modified version</a> of the amazing <a href="http://www.opentk.com/">OpenTK 1.0</a>. We took the liberty (and by "we" I mean, the amazing Kenneth Pouncey) of fine-tuning the implementation for MonoMac use. <h2>Getting MonoMac 1.0</h2> <p>If you already have MonoDevelop installed, just update your MonoMac Add-In. If you do not have MonoDevelop installed, follow our <a href="http://www.mono-project.com/MonoMac#Obtaining_MonoMac">friendly instructions</a>. <h2>Contributors</h2> <p>MonoMac would not have been possible without the help of our great contributors, this is the team: <p>Main bindings: <ul> <li>Geoff Norton <li>Miguel de Icaza <li>Jonathan Pryor <li>Michael Hutchinson </ul> <p>Contributors: <ul> <li>Alexander Shulgin (WebKit DOM) <li>James Clancey (AppKit contributions) <li>Kenneth Pouncey (API, samples) <li>Maxi Combina (WebKit events, sample) <li>Regan Sarwas (PdfKit, ImageKit, NSColor, NSGradient, NSBezierPath, samples) <li>Ashok Gelal (CoreWlan) </ul> <h2>Next Steps</h2> <p>What is great about doing a 1.0 release is that you know that there will be a 1.1 release, and a 1.2 release and a 2.0 release. <p>This is our way of saying "thank you for waiting" and giving our users a chance to start building applications, knowing that we have battle tested MonoMac and it is being used in our own products now <a href="#note">[1]</a>. <p>We obviously will continue improving the API, adding more frameworks as time goes by, but we will also be working with other communities to expand MonoDevelop's language support, create more templates for languages like F#, IronRuby, IronPython and UnityScript. <p>Although we have a great start for documentation, we hope that contributors will take advantage of a new web-based wiki and collaboration tool that we are building to improve the docs and help us make MonoMac's documentation the best. <p>Hopefully, we will also get more samples contributed to MonoMac and we will see a new wave of tutorials and we will see fascinating discussions on how to build better software while enjoying every second of it. <p><a name="note">[1]</a> (MonoDevelop 2.6 will be using MonoMac for its native dialog boxes). http://tirania.org/monomac/archive/2011/Mar-17.html miguel@gnome.org (Miguel de Icaza) http://tirania.org/monomac/archive/2011/Mar-17.html Thu, 17 Mar 2011 19:16:00 -0500 MonoMac hotfix <p><a href="https://github.com/hbons">Hylke Bons</a> warned us of a limitation in our MonoMac packager so we are issuing a new MonoMac refresh that fixes various bugs: <ul> <li>Supports using Mono.Posix.dll in packaged bundles. <li>Supports using System.Drawing in packaged bundles. <li>Fixes various BCL P/Invokes problems (we forgot to ship the config file :-) <li>No longer requires Mono 2.8.1, works with any mono 2.8+ </ul> <p>Follow the <a href="http://mono-project.com/MonoMac">standard instructions</a> to update your MonoMac add-in. <p>Hylke then got his <a href="https://github.com/hbons/SparkleShare/tree/master/SparkleShare/Mac/SparkleShare">native Mac client</a> for <a href="http://twitter.com/sparkleshare">SparkleShare</a> (a DropBox-like system, but backed up by Git or any Git hosting sit) working as a bundle on OSX. http://tirania.org/monomac/archive/2011/Feb-06.html miguel@gnome.org (Miguel de Icaza) http://tirania.org/monomac/archive/2011/Feb-06.html Sun, 06 Feb 2011 16:38:00 -0500 MonoMac Refresh! <P>We just pushed a new refresh of <a href="http://www.mono-project.com/MonoMac">MonoMac</a>. This release contains: <ul> <li>New complete bindings: QuartzComposer, CoreWlan, PdfKit, ImageKit and Addresbook. <li>AppKit: new classes: NSBezierPath, NSGradient; convenience methods for NSColor, NSTableView, NSMenuItem, and NSPasteboard. <li>CoreImage's CIVector and support in AppKit for CoreImage. <li>WebKit indexers and support for reporting user decisions. </ul> <p>In our shared core with MOnoTouch, these are the changes: <ul> <li>CoreGraphics: Support for transparency layers. <li>Foundation: API helpers to make NSIndexSet more pleasant to use; new methods to control the NSRunLoop; NSUrlProtocol and NSUrlProtocolClient classes. <li>ObjCRuntime: Exposed the shared library loading code and convenience methods to pull constants from shared libraries. <li>KeyChain: expose new methods for common operations (managing internet passwords) <li>CoreAnimation: bound a few missing constants. <li>OpenGL: new CAOpenGLLayer type. </ul> <p>Many new samples are now included with MonoTouch. Kenneth has contributed various ported samples from the CoreAnimation book exercising the API, fixing it, and providing many samples for developers to get started. We now ship 32 samples covering a wide range of Mac APIs. <p>Contributors to this release include: Geoff Norton, Alexander Shulgin, James Clancey, Maxi Combina, Regan Sarwas, Michael Hutchinson, Ashok Gelal and Miguel de Icaza. <p>Additionally, the first MonoMac app has hit the Mac AppStore! <p>Some small stats: MonoMac 0.4 was installed by 263 developers, MonoMac 0.5 by 369 developers, and MonoMac 0.6 (our last release) by 588. http://tirania.org/monomac/archive/2011/Feb-02.html miguel@gnome.org (Miguel de Icaza) http://tirania.org/monomac/archive/2011/Feb-02.html Wed, 02 Feb 2011 23:55:00 -0500 First MonoMac App Hits the Apple AppStore <p><a href="http://site.yvansoftware.be/">Yvan Janssens</a> (<a href="http://twitter.com/yvanjanssens">@yvanjanssens</a>) just wrote to let me know that <a href="http://itunes.apple.com/us/app/iproxify-plus/id409373452?mt=12">iProxify Plush</a>, his <a href="http://www.mono-project.com/MonoMac">MonoMac-powered</a> application, has been accepted for distribution on Apple's Mac AppStore. <p>This is an important development in the history of MonoMac, as someone has actually published a full executable based on the tools that we built (we have not tested this ourselves as we did not really have anything to publish). <p>It also means that we got all the details right to let people use C# to publish to the Mac AppStore: we do not take external dependencies, we bundle all of the Mono dependencies with the app and we follow all the relevant Apple rules for distribution. <p>Congratulations to Yvan for his published app! http://tirania.org/monomac/archive/2011/Jan-31.html miguel@gnome.org (Miguel de Icaza) http://tirania.org/monomac/archive/2011/Jan-31.html Mon, 31 Jan 2011 22:05:00 -0500 Patterns for Creating UITableViewCells <p>At one point or another, every UITableView programmer will outgrow the <a href="http://developer.apple.com/library/ios/#documentation/UserExperience/Conceptual/TableView_iPhone/TableViewCells/TableViewCells.html#//apple_ref/doc/uid/TP40007451-CH7">default set of UITableViewCells styles</a> available to them, and they will be forced to customize the cells to provide a better user experience. <p>The initial temptation of every lazy programmer like myself is to do the bare minimum amount of work to obtain the desired effect. This path typically starts by realizing that you can add a subview to your cell, like this: <pre class="code-csharp"> static UIImage logo = UIImage.FromFile ("logo.png"); UITableViewCell CreateCell () { var cell = new UITableViewCell (UITableViewCellStyle.Default, "key"); var imageView = new UIImageView (logo) { Frame = new RectangleF (10, 10, 20, 20); }; cell.ContentView.Add (imageView); return cell; } </pre> <p>Although the above code works, and you can get away with it for simple tasks, it does not take into consideration cell reuse. UITableViews like to recycle cells on the screen which is fine as long as you do not need to use a different image on each cell, as all of a sudden, you will need to keep track of the imageView value. <p>In other cases, your GetCell method will get bloated with a lot of inside information about all possible customizations that you might have done in a previous instance, and you will have to undo those. Apple's UICatalog sample is packed with code like that, and so is <a href="https://github.com/migueldeicaza/monotouch-samples/blob/master/monocatalog/textfield.cs#L62">my port</a> of the same code. <p>And that is just not a decent way of living. <p>You are miserable, your users are miserable and everyone around you is miserable. <p>My preferred pattern, which has worked better for me is to create a derived cell class that tracks all of my properties, and centralizes the management of updating the properties of my cell. <p>Assuming that my cell will render the contents of an object called "MyData", this is what my pattern looks like for custom UITableViewCells: <pre class="code-csharp"> // // I create a view that renders my data, as this allows me to reuse // the data rendering outside of a UITableViewCell context as well // public class MyDataView : UIView { MyData myData; public MyDataView (MyData myData) { Update (myData); } // Public method, that allows the code to externally update // what we are rendering. public void Update (MyData myData) { this.myData = myData; SetNeedsDisplay (); } } // // This is the actual UITableViewCell class // public class MyDataCell : UITableViewCell { MyDataView myDataView; public MyDataCell (MyData myData, NSString identKey) : base (UITableViewCellStyle.Default, identKey) { // Configure your cell here: selection style, colors, properties myDataView = new MyDataView (myData); ContentView.Add (myDataView); } public override void LayoutSubviews () { base.LayoutSubviews (); myDataView.Frame = ContentView.Bounds; myDataView.SetNeedsDisplay (); } // Called by our client code when we get new data. public void UpdateCell (MyData newData) { myDataView.Update (newData); } } </pre> <p>With the above pattern implemented, I can now add all of my view specific gadgetry into the MyDataView class, images, helper labels, or other views. <p>Then, the Update method needs to make sure that all of those extra views are updated when this method is invoked. All of the configuration for your cell needs to take place in this method, and nowhere else. <p>The client code that uses these views then looks like this: <pre class="code-csharp"> class MyTableViewDataSource : UITableViewDataSource { public override UITableViewCell GetCell (UITableView tableView, NSIndexPath indexPath) { MyData myData = Lookup (indexPath); var cell = tableView.DequeueReusableCell (key); if (cell == null) cell = new MyDataCell (myData); else cell.UpdateCell (myData); return cell; } </pre> <h3>The Extra UIView</h3> <p>You might be thinking that creating the extra UIView is not really worth the effort, as you likely only need to apply a few customizations, and you got already most of your bang for the buck by creating your custom UITableViewCell. <p>You would be right. <p>The reason for creating a custom UIView, is that there might come a time when you want to do some custom drawing in your cell. Perhaps add a nice gradient on the background, or perhaps draw some shadows, or mix large fonts with small fonts. <p>It might be just a couple of small touch-ups, nothing too complicated, but just a little extra polish. By using a custom UIView, you can now spice up your view just a tiny bit, by overriding the Draw method: <pre class="code-csharp"> public override void Draw (RectangleF rect) { var context = UIGraphics.GetCurrentContext (); UIColor.White.SetColor (); context.FillRect (Bounds); context.DrawLinearGradient (myGradient, start, end, 0); } </pre> <h3>Creating a MonoTouch.Dialog Element</h3> <p>If you are like me, lazy, you would likely not be writing a lot of GetCell methods and large dispatch tables for your UITableViews, and instead you are using <a href="https://github.com/migueldeicaza/MonoTouch.Dialog">MonoTouch.Dialog</a>. <p>MonoTouch.Dialog is an API that takes away the administrivia out of building UITableViews and lets you focus on the content. I <a href="http://tirania.org/blog/archive/2010/Feb-23.html">discussed MonoTouch.Dialog last year</a> on my old blog. <p>Once you have your own UITableViewCell, it is trivial to turn that into a MonoTouch.Dialog Element. You would do it like this: <pre class="code-csharp"> public class MyDataElement : Element { static NSString key = new NSString ("myDataElement"); public MyData MyData; public MyDataElement (MyData myData) : base (null) { MyData = myData; } public override UITableViewCell GetCell (UITableView tv) { var cell = tv.DequeueReusableCell (key) as MyDataCell; if (cell == null) cell = new MyDataCell (MyData, key); else cell.UpdateCell (MyData); return cell; } } </pre> <p>With the code above, you have everything you need to make your custom cell to be used with MonoTouch.Dialog. <p>You can see the entire pattern in action in <a href="https://github.com/migueldeicaza/TweetStation/blob/master/TweetStation/UI/TweetCell.cs">TweetStation's source code</a>. The 300 or so lines of code in that file are responsible for rendering a tweet in TweetStation. http://tirania.org/monomac/archive/2011/Jan-18.html miguel@gnome.org (Miguel de Icaza) http://tirania.org/monomac/archive/2011/Jan-18.html Tue, 18 Jan 2011 19:32:00 -0500 Mono Packager and the Apple AppStore <p>We are happy to announce the <a href="http://www.mono-project.com/MonoMacPackager">Mono Packager for OSX</a>. <center> <img src="http://mono-project.com/files/2/2a/Md-monomac-bundle.png"> <br> MonoDevelop UI for Mac Packages and Installers. </center> <p>The Mono Packager for OSX makes it possible to create self-contained Mono applications that will run on OSX without requiring the Mono.framework to be previously installed on the system. In combination with the <a href="http://www.mono-project.com/MonoMac">MonoMac project</a> you can build fully native MacOS X applications using your favorite .NET technologies. From your choice of Mono/.NET languages, to your choice of Mono/.NET library. <p>The packager can create both signed applications for distribution on the Mac AppStore, as well as creating installers for your software. <h3>Mono on the Mac: Some Background</h3> <p>Mono on OSX has historically been distributed as an image that installed itself in /Library/Frameworks/Mono.framework. Once Mono was installed, users could write code in C# or their favorite .NET/Mono language and run the resulting executable. <p>The problem is that Mono.framework contains an entire development stack: compilers, GUI tools, command line tools, libraries, documentation which is convenient for developers, but most of the time, not very useful for end-users. <p>This meant that developers would typically ask users to first install the Mono.framework (a big download) and then they could install their applications. <p>To work around that problem, some developers have chosen to manually embed Mono in their applications. This has always been possible, but it was error-prone as developers would have to manually assemble Mono into their application bundle, configure Mono properly, and initialize the Mono runtime themselves. Doable, but not pleasant. <p>With today's release, we have taken the burden of creating self-contained Mono applications out of developer's hands and added it as a standard feature for developers to use. <h3>The Mac AppStore</h3> <p>The Mac AppStore requires that applications submitted to it are completely self-contained and they do not depend on third-party frameworks to be installed on the system. It also requires that your application and installer be signed. <p>Both of those features are supported in our MonoMac Packager. Developers can now create Mac AppStore ready applications using MonoDevelop and MonoMac. We have integrated the package creation, installer creation, and signing processes into our MonoDevelop IDE. <p>All that developers have to do is sign up for Apple's Mac developer program, get their distribution certificates, build a fabulous application and upload the application using the Application Loader to Apple.com. <h3>Upcoming: Linking</h3> <p>In this version of the Mac bundler, we include all of the dependencies that your program takes. For example, if you use the System.Xml library, the entire System.Xml library will be bundled with the application. <p>In our next release, we will add support for <a href="http://www.mono-project.com/Linker">Mono's linker</a>, the same technology we used in <a href="http://monotouch.net">MonoTouch</a> to reduce the executable size. <p>When you choose to use use the linker, the granularity of distribute changes from the library-level to the type level. For example, if you only use one type from System.Xml, the linker will strip out all of the unused classes and generate a new System.Xml library that only contains the one type that you used. <p>We did not want to wait for the linker to be ready before shipping our packager, but we should have it ready soon. <h3>MonoMac Refresh</h3> <p>As part of this release we have also issued a refresh to the MonoMac library and templates. <p>From now on, MonoMac binaries will default to be 4.0 profile, allowing users to leverage all of the new features in the C# 4.0 language (like <tt>dynamic</tt>) as well as the new .NET 4.0 APIs that we introduced with Mono 2.8. <p>The updates to the MonoMac API are described in <a href="http://tirania.org/monomac/archive/2011/Jan-10-1.html">my other blog post</a>. http://tirania.org/monomac/archive/2011/Jan-10.html miguel@gnome.org (Miguel de Icaza) http://tirania.org/monomac/archive/2011/Jan-10.html Mon, 10 Jan 2011 17:23:00 -0500 New MonoMac Refresh! <p>As part of today's release of the <a href="http://tirania.org/monomac/archive/2011/Jan-10.html">Mono Packager for OSX</a> we have issued a new MonoMac refresh. <p>As we create more sample code, and start to write real applications with MonoMac, we have updated the API to be simpler, cleaner and more comprehensive. This release is all about small incremental improvements to the MonoMac API. <p>As usual, we have updated the our <a href="http://mono.ximian.com/monomac-docs/">MonoMac API documentation MonoMac API documentation</a>. If you are thinking about getting started with MonoMac, we strongly recommend you read our <a href="http://www.mono-project.com/MonoMac">MonoMac</a> page for some useful links and tutorials. <h3>Statistics</h3> <p>MonoMac 0.4 was installed by 263 developers, MonoMac 0.5 by 369 developers. Interesting considering that the holidays are a slow season: <center> <img src="http://tirania.org/shots/1101091106IHKGeOBC.png"> </center> <h3>Apps!</h3> <p><a href="http://twitter.com/#!/praeclarum">Frank Krueger</a> the creator of <a href="http://icircuitapp.com/">iCircuit</a> a real-time circuit emulator and editor for the iPad/iPhone has started a port of iCircuit to MacOS X using MonoMac: <center> <img src="http://tirania.org/shots/1101091942LfvK58IN.png"> </center> <p>Pretty amazing, considering that Frank only learned to use MonoMac yesterday (although he does have extensive MonoTouch experience). <b>Update:</b> He posted and <a href="http://yfrog.com/h8mx8p">updated screenshot</a> "now in technicolor". <h3>MacDoc Sample</h3> <p>During the holidays, I <a href="https://github.com/mono/monomac/tree/master/samples/macdoc">wrote MacDoc</a> a native front-end for the <a href="http://www.mono-project.com/Monodoc">MonoDoc documentation engine</a>. I also took Jonathan Pobst's <a href="https://github.com/jpobst/Kipunji">fabulous style sheets from Kipunji</a> to spice up the UI a little bit. <p>This is the result: <center> <img src="http://tirania.org/images/monomac-macdoc.png"> </center> <p>I still have to integrate the index and search features of MonoDoc into the UI, and I am struggling as to how to surface them in the UI. <p>The Index is supposed to have an alphabetical listing of classes, method, properties, fields, similar to the index at the end of a book. I always found this to be very useful when developing with .NET. The search functionality on the other hand uses Lucene to search in the documentation body. <p>At this point, I believe that I should add a tabbed widgets, and let the user pick between the tree view on the left and the index (with a new text-entry to do the incremental search). But if the users uses the search on the toolbar, I should replace the tree and the index with a list of results. <p>Does the above make sense, or you think it is a terrible UI idea and completely unacceptable for OSX users? <p>I thought about merging the index and the body search, but it would render the index search a bit useless. Anyways, if you are a Mac expert, please send feedback my way. http://tirania.org/monomac/archive/2011/Jan-10-1.html miguel@gnome.org (Miguel de Icaza) http://tirania.org/monomac/archive/2011/Jan-10-1.html Mon, 10 Jan 2011 16:01:00 -0500 CorePlot Bindings for MonoMac and MonoTouch <p>Happy New Year! <p>Today I wrote the MonoMac and MonoTouch bindings for the open source <a href="http://code.google.com/p/core-plot/">CorePlot</a> library. <p>The bindings are available from the <a href="https://github.com/mono/monotouch-bindings">binding collection module</a> on GitHub and binaries for both MonoTouch and MonoMac are available there. http://tirania.org/monomac/archive/2011/Jan-01.html miguel@gnome.org (Miguel de Icaza) http://tirania.org/monomac/archive/2011/Jan-01.html Sun, 02 Jan 2011 00:39:00 -0500 MonoMac Happy Holidays Edition is out! <p>Just in time for your holidays, we have baked a new version of <a href="http://mono-project.com/MonoMac">MonoMac</a>. As usual, installation is very easy, just <a href="http://www.mono-project.com/MonoMac#Obtaining_MonoMac">follow these steps</a>. <p>As for some stats: 224 developers downloaded the MonoMac 0.4 add-in for MonoDevelop in the previous release, not bad at all! <center> <img src="http://tirania.org/shots/1012150251iPp5lcHM.png"> </center> <p>These are the changes since MonoMac 0.4 <ul> <li><a href="http://mono.ximian.com/monomac-docs/">API Documentation</a>! <ul> <li>Built on JPobst's <a href="https://github.com/jpobst/Kipunji">new documentation engine</a></li> <li>There are mostly automatically generated stubs now</li> </ul> </li> <li>New in CoreAnimation: <ul> <li>Layout managers</li> <li>Constrained layout manager</li> <li>CATextLayer can now set its font with the WeakFont property, or the SetFont () strong types.</li> </ul> </li> <li>CoreGraphics: <ul> <li>CGAffineTransform.Invert () method to obtain the inverse of an affine transformation</li> </ul> </li> <li>Foundation <ul> <li>NSObject.FromObject method automatically boxes .NET types into NSValue, NSString or NSNumber objects.</li> <li>Convenience function to create NSArrays from object [] arrays and using the new NSObject boxing functionality.</li> <li>New NSIndexSet convenience factory method and ToArray method</li> <li>NSDictionary and NSMutableDictionary have convenience methods that take object [] arrays, and do the boxing automatically (using the new NSArray functionality described above)</li> </ul> </li> <li>AppKit: <ul> <li>DoubleClick event on views that have support for double click actions (instead of using the DoubleAction selector + Target)</li> <li>NSTableView has a new Source property that can be used to blend both Delegate and the table view data source into one (NSTableViewSource which combines NSTableViewDataSource and NSTableViewDelegate)</li> <li>New: NSPredicate family of classes</li> <li>New: NSRuleEditor</li> </ul> </li> <li>AddressBook library: <ul> <li>We used to ship the source, but now it is included in the binary library.</li> </ul> </li> <li>CoreImage: <ul> <li>New in this release.</li> </ul> </li> <li>WebKit: <ul> <li>Many events that were previously only exposed as Objective-C style delegates are now proper C# events.</li> </ul> </li> <li>Engine: <ul> <li>Improved methods with return structures <li>Improved string [] marshalling <li>Now with INativeObject marshalling </ul> </li> </ul> <p>We have also updated the <a href="https://github.com/mono/monomac/tree/master/samples">samples</a> and added a few more. http://tirania.org/monomac/archive/2010/Dec-15.html miguel@gnome.org (Miguel de Icaza) http://tirania.org/monomac/archive/2010/Dec-15.html Thu, 16 Dec 2010 00:35:00 -0500 The @" syntax in Objective-C vs C# <p>A common mistake that I see in some of the code from our customers, or from contributed samples to the MonoMac repository is the use of the @"string" syntax in C# programs. <b>Summary:</b> do not use the @"..." when porting Objective-C samples to C#. <h3>String Syntax in Objective-C and C#</h3> <p>Objective-C supports two kinds of string literals in your source code, strings that are surrounded by double-quotes and strings that are prefixed with the at-sign and then double quotes. <p>The first kind are zero-terminated C-style strings. For example the string "foo" is encoded by the compiler as the following four bytes: 'f', 'o', 'o' and zero plus the address of the string at the location where this is referenced. <p>The second kind, for example @"Hello World" is a CoreFoundation string, and the compiler has to encode by creating a static instance of a CoreFoundation CFString object: it has a class identifier, some object flags, a pointer to the data ('foo' in this case) and the length of the string. <p>In both the c-string style and the CFString style, escape sequences are processed by the compiler. This means that if you use "1\n2" or @"1\n2" this will produce in both cases a string consisting of the '1' character followed by a newline (byte value 10) and the '2' character. <p>In C# the syntax @"string" does not mean "encode as a CFString" object. Instead it is a<a href="http://msdn.microsoft.com/en-us/library/362314fe(v=vs.71).aspx"> quoted-string literal</a>, and it instructs the compiler to avoid performing escape sequence processing. In C# the strings "1\n2" and @"1\n2" have respectively lengths 3 and 4. The former one consists of the characters '1', newline (byte value 10) and character '2'. While the latter consists of the characters '1', '\', 'n' and '2'. <h3>System.String vs Foundation.NSString in MonoMac/MonoTouch</h3> <p>In the MonoTouch/MonoMac binding strings are almost always exposed in the binding with the native C# string type (System.String). Any conversions between the C# string and the Objective-C string type are done by the runtime. <p>There are a handful of places where we must expose NSString values instead of strings. This is usually necessary when the underlying Objective-C code performs reference comparisons instead of content comparisons for strings. Some Objective-C APIs for example use these strings as special "tokens" that you must pass verbatim in your application to a method. <p>We typically use those for notification names, keys in dictionaries and a handful of other places. You will notice in those handful of cases that Mono's API's expose an NSString parameter or return value instead of a string parameter or return value. <p>For example if you have a AddNotification (NSString key) API, you would typically use it like this: <pre class="code-csharp"> // example class exposing a notificatin as an NSString: // class SomeClass { // NSString WakeUpNotification { get; } // } AddNotification (SomeClass.WakeUpNotification); </pre> <h3>Conversions</h3> <p>Converting from a C# string to an NSString is easy, all you have to do is call new NSString ("mystring"). <p>To go the other way around from an NSString to a string, you can either use an explicit cast (this is the recommended design pattern from the <a href="http://www.amazon.com/gp/product/0321545613?ie=UTF8&amp;tag=tiraniaorg-20&amp;linkCode=as2&amp;camp=1789&amp;creative=390957&amp;creativeASIN=0321545613">Framework Design Guidelines</a>) or you can use the ToString () method on an NSString. http://tirania.org/monomac/archive/2010/Dec-14.html miguel@gnome.org (Miguel de Icaza) http://tirania.org/monomac/archive/2010/Dec-14.html Wed, 15 Dec 2010 00:33:00 -0500 NSNumbers, NSValues, Dictionaries and Arrays <p>A couple of days ago, <a href="http://jacksonh.tumblr.com/">Jackson</a> pointed out that one of the samples in this blog could be further simplified if we took advantage of C#'s params arguments. The sample in question was this code snippet from QTRecorder, used to return an NSSet: <pre class="code-csharp"> [Export] public static NSSet keyPathsForValuesAffectingHasRecordingDevice () { return new NSSet (new string [] {"SelectedVideoDevice", "SelectedAudioDevice"}); } </pre> <p>In the above example, we have to manually construct the string array, so that we can call the NSSet (string [] args) constructor. Jackson correctly pointed out that the above could be simplified if we introduced an NSSet (params string [] args) constructor. This lets the compiler do the work for us, so the sample above can be written like this instead: <pre class="code-csharp"> [Export] public static NSSet keyPathsForValuesAffectingHasRecordingDevice () { return new NSSet ("SelectedVideoDevice", "SelectedAudioDevice"); } </pre> <p>This got me thinking about another possible improvement to the API. In a number of places in the MonoMac/MonoTouch API it is necessary to pass numbers, strings, booleans, points, rectangles, affine transforms and 3D transforms in NSDictionary and NSArray objects. <p>Since Objective-C does not have language-assisted auto-boxing like C# does, programmers have to manually box those in either NSValue types or NSNumber types. An NSNumber can contain booleans, 16, 32 and 64 bit integers and unsigned integers as well as floats and doubles. NSValues are typically used to box points, rectangles, sizes and 2D and 3D affine transformations. <p>In a few of the current samples we have code like this: <pre class="code-csharp"> var objects = new NSObject [] {&nbsp; new NSNumber (13), new NSNumber ((float) 0.5) }; var keys = new NSObject [] { new NSString ("speed"), new NSString ("volume") }; var dict = NSDictionary.FromObjectsAndKeys (objects, keys); </pre> <p>The above clearly is too verbose for no good reason. We have now introduced methods that take general purpose object [] arrays, containing regular C# data types and will do the automatic boxing: <pre class="code-csharp"> var objects = new NSObject [] {&nbsp;13, 0.5 }; var keys = new NSObject [] {&nbsp;"speed", "volume" }; var dict = NSDictionary.FromObjectsAndKeys (objects, keys); </pre> <p>The method that does the object to NSObject conversion is a convenience static method in the NSObject class, with the following signature: <pre class="code-csharp"> public static NSObject FromObject (object obj); </pre> <p>The function can be used to convert nulls, booleans, numbers, strings, IntPtrs, SizeF, RectangleF, PointF on both MonoMac and MonoTouch and in MonoTouch it additionally supports CGAffineTransform, CATransform3D and UIEdgeInsets. http://tirania.org/monomac/archive/2010/Dec-09.html miguel@gnome.org (Miguel de Icaza) http://tirania.org/monomac/archive/2010/Dec-09.html Fri, 10 Dec 2010 00:33:00 -0500 Using Key-Value Coding with MonoMac <p><a href="http://monomac.files.wordpress.com/2010/12/db-12.png"><img class="alignright size-full wp-image-32" title="db-1" src="http://monomac.files.wordpress.com/2010/12/db-12.png" alt="" width="192" height="381" /></a><a href="http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/KeyValueCoding/Concepts/Overview.html">Key-Value Coding</a> is a set of practices that allow applications to access object properties using strings. This is similar to <a href="http://msdn.microsoft.com/en-us/library/cc189022(VS.95).aspx">Binding Expressions</a> in Silverlight. In both cases the purpose is to allow tooling that does not directly have access to your native code to access properties from your program. <p>You would typically use this from a tool like Interface Builder, where you can use the Binding Inspector (⌘4 from Interface Builder) to explore the possible values of the component that you can data bind. In Interface Builder you would use string to describe the name of the property that you want to access. For example, consider the options available for the NSPopUpButton component show on the right. All of those properties of the NSPopUpButton can be pulled directly from your code from Interface Builder without having to manually hook things up. <p>To use this with MonoMac, all you have to do is make sure that any property that you want to access from Interface Builder is flagged with the [Export] attribute. If you apply the Export attribute without any parameters, it will expose the C# name of your property for Key-Value access. You can change the name that is exposed by passing a parameter, for example: [Export ("myIndex")]. <p>The <a href="https://github.com/mono/monomac/tree/master/samples/QTRecorder">QTRecorder sample</a> shows how various elements in the UI are connected to the code: [caption id="attachment_38" align="alignnone" width="583" caption="MonoMac port of QTRecorder sample"]<a href="http://monomac.files.wordpress.com/2010/12/qtrecorder2.png"><img class="size-full wp-image-38" title="qtrecorder2" src="http://monomac.files.wordpress.com/2010/12/qtrecorder2.png" alt="" width="583" height="547" /></a>[/caption] <p><a href="http://monomac.files.wordpress.com/2010/12/props.png"><img class="alignleft size-medium wp-image-36" title="props" src="http://monomac.files.wordpress.com/2010/12/props.png?w=169" alt="" width="169" height="300" /></a>In that sample, the class QTRecorder, a class derived from NSDocument exposes properties that are hooked up directly to the Enabled properties in various buttons in the UI and properties to populate and control the contents of the various popups in the application. All of these bindings are configured by setting the target of the binding to be the "File's Owner", in this case the QTRecorder class, as it happens to load the QTRecorder.nib file. You can see the complete list of bindings in the screenshot on the left. <p>Let us explore what happens in the application. The Video Devices popup is controlled through three bindings: the Content, the Content Values and the Selected Object. This is what the binding are configured to in Interface Builder for all three properties: <p><a href="http://monomac.files.wordpress.com/2010/12/binding2.png"><img class="alignnone size-full wp-image-41" title="binding" src="http://monomac.files.wordpress.com/2010/12/binding2.png" alt="" width="640" height="105" /></a> <p>The <strong>Content</strong> is bound to the VideoDevices property in the source code, this is a property that merely returns the strongly typed QTCaptureDevice array of the available devices. The <strong>Content Values </strong>is bound to the VideoDevices.localizedDisplayName. The first part of the key matches our VideDevices array, and the second part of the key happens to be the Objective-C name of a property exposed by the QTCaptureDevice that provides the name of the device to display. The actual item selected on the screen is controlled by the <strong>Selected Object</strong> binding. In this case, a new property, the SelectedVideoDevice property. At this point, the NSPopUpButton will populate its contents using these bindings. Additionally, when the user selects a different item, the property bound to the Selected Object will be invoked with the new value. Our code responds by configuring the QTCaptureSession accordingly. <p>Another interesting binding takes place with the recording button. The recording button hasits Enabled property bound to the HasRecordingDevice C# property and has its Value bound to the C# Recording property. When we change the video source in the popup, the button responds by either getting grayed out or becoming active. Although this could have been done programmatically in response to the new value set in the SelectedVideoDevice property, the code takes advantage of the built-in notification system and instead reacts to changes to the SelectedVideoDevice automatically. <p>This is done by using the <a href="http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/KeyValueObserving/Concepts/DependentKeys.html">dependency keys feature</a> a feature of Key-Value Coding. It requires that the code exports a specially-named method which is prefixed with the string "keyPathsForValuesAffecting". These methods are are meant to return an NSSet containing the names of the properties that depend on that particular property. In our case this is: <pre class="code-csharp">[Export] public static NSSet keyPathsForValuesAffectingHasRecordingDevice () { return new NSSet ("SelectedVideoDevice", "SelectedAudioDevice"); }</pre> <p>Once that is done, the runtime knows that when either one of the SelectedVideoDevice or SelectedAudioDevice change, it has to query the value for HasRecordingDevice again. http://tirania.org/monomac/archive/2010/Dec-07.html miguel@gnome.org (Miguel de Icaza) http://tirania.org/monomac/archive/2010/Dec-07.html Wed, 08 Dec 2010 00:33:00 -0500 Error Reporting Pattern in MonoMac <p>When writing code that reports errors to the user, usually when I get back an NSError result, I use the following code snippet to show it: <pre class="code-csharp">if (err != null){ NSAlert.WithError (err).BeginSheet (Window, delegate { // Any needed cleanup code can go here, // it is invoked when the Sheet goes away. }); return; }</pre> <p>The above creates an NSAlert object using the WithError factory method, and then invokes the BeginSheet method to present this modally to the user. Usually you can put any cleanup code in the body of the provided delegate. http://tirania.org/monomac/archive/2010/Dec-05.html miguel@gnome.org (Miguel de Icaza) http://tirania.org/monomac/archive/2010/Dec-05.html Mon, 06 Dec 2010 00:33:00 -0500 MonoMac 0.4 is out <p>We have just released MonoMac 0.4. Installation is very easy, just <a title="MonoMac web site" href="http://www.mono-project.com/MonoMac#Obtaining_MonoMac">follow the steps</a> in our MonoMac site. <div class="alignright" style="width: 310px"> <a href="http://monomac.files.wordpress.com/2010/11/stillmotion.png"> <img class="size-medium wp-image-12" title="stillmotion" src="http://monomac.files.wordpress.com/2010/11/stillmotion.png?w=300&#038;h=172" alt="Still Motion sample in C#" width="300" height="172"/> </a><p class="wp-caption-text">StillMotion C# sample</p> </div> <p>We are very happy with this release as this is the first time that MonoMac can be used to build real applications for OSX. There are three major reasons for this: <ul> <li>Our API coverage for all core Mac APIs is in place.</li> <li>We have completed many of the gaps in the MonoMac engine.</li> <li>With our community, we have been <a title="MonoMac sample source code" href="https://github.com/mono/monomac/tree/master/samples/">porting various samples</a> that have helped us polish the API and make sure that it is natural and usable for C# programmers.</li> </ul> <p>In addition to those fundamental changes, MonoDevelop now also sports a new NSDocument-based template for new projects which will come in very handy for many developers. <p>The major changes in MonoMac 0.4 from version 0.2 released in early November are: <ul> <li>MacCore (code shared with MonoTouch): <ul> <li>Support for Key Value Observing with C#</li> <li>Foundation extensions: <ul> <li>C# friendly NSNotificationCenter APIs</li> <li>NSPredicate support</li> <li>NSMetadata support</li> <li>Strongly typed NSRunLoop</li> <li>Extended NSError, NSMutableData,</li> </ul> </li> <li>CoreVideo framework bindings</li> <li>Security framework bindings</li> <li>CoreGraphics support for PDF files</li> </ul> </li> <li>MonoMac: <ul> <li>QTKit framework bindings</li> <li>NSDocument templates in MonoDevelop</li> <li>WebKit DOM APIs</li> <li>CoreImage APIs</li> <li>ECMA XML Documentation stubs are now in place</li> <li>Lots of <a href="https://github.com/mono/monomac/tree/master/samples/">samples</a>!</li> </ul> </li> </ul> <p>Michael Hutchinson has a <a href="http://mjhutchinson.com/journal/2010/06/09/monomac_in_monodevelop">walk through on using MonoMac with MonoDevelop</a> that will get you up and running quickly. <p>We are still looking for contributors to help us bring more samples to MonoMac, create more <a href="https://github.com/mono/monodevelop/tree/master/extras/MonoDevelop.MonoMac/MonoDevelop.MonoMac/templates/">MonoDevelop templates</a> and help us add more of the system libraries to MonoMac. If you want to join us, check out the <a href="http://mono.1490590.n4.nabble.com/Mono-OSX-f1546893.html;cid=1291392004664-863">mono-osx mailing list</a> and we are on the IRC channel #monomac on the server irc.gnome.org. http://tirania.org/monomac/archive/2010/Dec-03.html miguel@gnome.org (Miguel de Icaza) http://tirania.org/monomac/archive/2010/Dec-03.html Sat, 04 Dec 2010 00:33:00 -0500 MonoTouch for iOS 4.2 is out. <p>We have graduated our <a href="http://monotouch.net/Releases/MonoTouch_3/MonoTouch_3.2.2">MonoTouch 3.2</a> into the stable channel and is now available for all of our MonoTouch users in the stable channel. To update, just go to the Help menu on MonoDevelop, select the stable channel and check for updates. <p>In addition to adding support for the new APIs introduced in iOS 4.2 for the iPhone and the iPad in the core libraries, we have bound many more frameworks that our users have requested, in particular the horrifying Security framework API now has a nice C# binding that regular humans can consume. <p>Enjoy! http://tirania.org/monomac/archive/2010/Dec-02.html miguel@gnome.org (Miguel de Icaza) http://tirania.org/monomac/archive/2010/Dec-02.html Fri, 03 Dec 2010 00:33:00 -0500 Improving upon dictionary bindings. <p>iOS and OSX expose many of their APIs as dictionaries. This means that instead of having properties or methods to configure a particular object, the configuration takes place by creating dictionaries that use some special keys with their associated values. These APIs are not well suited for exploration from the IDE or from our <a href="http://www.mono-project.com/CsharpRepl">REPL</a>. The APIs typically look like this: <pre class="code-csharp"> CFStringRef keys[] = { kCTFontAttributeName, kCTForegroundColorAttributeName }; CFTypeRef bval[] = { cfListLineCTFontRef, CGColorGetConstantColor(kCGColorBlack) }, attr = CFDictionaryCreate (kCFAllocatorDefault, (const void **) &amp;keys, (const void **) &amp;bval, sizeof(keys) / sizeof(keys[0]), &amp;kCFTypeDictionaryKeyCallBacks, &amp;kCFTypeDictionaryValueCallBacks); astr = CFAttributedStringCreate(kCFAllocatorDefault, CFSTR("FFFFFF"), attr);[/sourcecode] </pre> <p>To make the API more easy to discover through intellisense, and our assembly browser, we try to turn all of the Dictionary-based APIs into strongly typed classes that hide the dictionary, the names of the keys, and the types of the keys required. The responsibility for getting the types right, to get the encoding working and to figure out which keys are valid or required in a dictionary are shifted from the programmer to the framework developers (in this case, MonoTouch and MonoMac developers). <p>What we typically do is we create a type that exposes all of the properties that might be supported by the dictionary, and we provide nullable version of them, for example, in the above case, Mono's implementation lives in the CFStringAttributes class, and the properties that we expose are these: <pre class="code-csharp"> public class CFStringAttributes () { public CTFont Font { get; set; } public CGColor ForegroundColor { get; set; } [...] }</pre> <p>Our users then instantiate configuration objects like this: <pre class="code-csharp">var attrs = new CFStringAttributes () { Font = listLineCTFont, ForegroundColor = UIColor.Black.CGColor }; var astr = new NSAttributedString ("FFFFFF"), attrs);</pre> <p>When you create your CFStringAttribute and start a new-line to enter properties, MonoDevelop knows the possible property values that you can initialize in the constructor, avoiding an entire trip to the documentation to figure out what is the key that you should use, the value and its encoding. It is all kept on type-safe datatypes. <p>There is a second class of APIs, mostly in the Audio and Video APIs that exposes a whole set of properties behind fooGetProperty and fooSetProperty APIs. Those APIs tend to be weakly typed, and take both a key, and depending on the key, a pointer to some value. If you feed the wrong values to the program, your application could crash or corrupt its heap. In these cases we have also walked away from exposing the low-level GetProperty/SetProperty methods, and instead exposed strongly-typed C# properties. <p>This is an example from the AudioFile API: <pre class="code-csharp"> AudioFileGetProperty ( audioFileID, kAudioFilePropertyPacketSizeUpperBound, &amp;propertySize, &amp;maxPacketSize );</pre> <p>In the above case, we are probing for the maximum packet size for the file. The user has to find and pass the proper constant (kAudioFilePropertyPacketSizeUpperBound), the size of the object (this is a simple validation system to eliminate a certain class of memory corruption erors), the address of the variable that will get the result back, and then determine the proper type for the property (maxPacketSize). <P>What we have tried to do with MonoTouch/MonoMac is hide all of that complexity, and instead we expose strongly typed properties, in this case the equivalent code is: <pre class="code-csharp"> var maxPacketSize = audioFile.PacketSizeUpperBound; </pre> <p>The property is available as well for intellisense, so it is a lot simpler to see what is going on behind the scenes, shows up on the debugger window, on the interactive shell and means that scripting languages can access it without having to resort to reading C API documentation. http://tirania.org/monomac/archive/2010/Dec-01.html miguel@gnome.org (Miguel de Icaza) http://tirania.org/monomac/archive/2010/Dec-01.html Thu, 02 Dec 2010 00:33:00 -0500 My own iOS and OSX centric blog <p><img class="alignright" title="MacOS Logo" src="http://primates.ximian.com/~miguel/pictures/smile.jpg" alt="MacOS Logo" width="96" height="90" />In the past 18 months I started to spend a lot of time on OSX and iOS due to our MonoTouch project. <a title="MonoTouch" href="http://monotouch.net">MonoTouch</a> was our effort to blend seamlessly the iOS Objective-C APIs with the C# and ECMA CLI APIs. This is probably one of our nicest bindings that we have ever done for a native API, and it build on the experience from previous attempts like CocoaSharp, MonoObjc and other bindings that were created for the Mac in the past seven years. <p>The MonoTouch design and binding patterns proved to be very useful, and Geoff Norton and myself decided to bring the same design and patterns from iOS to OSX. This is how the new <a href="http://mono-project.com/MonoMac">MonoMac</a> bindings were created. While MonoTouch is a proprietary and commercial produce, MonoMac is open source and licensed under the MIT X11 license. <p>Building MonoMac has been a very fun experience, as the scope of the APIs is larger and also as it seems to have filled a void in the space. As the binding bootstraps, we are starting to see both users and contributors that have helped us move MonoMac faster by providing help with the bindings, polishing the API or writing samples that show how to use MonoMac. <p>My <a href="http://tirania.org/blog">personal blog</a> did not feel like the right place to share tips and tricks on iOS and OSX development for two main reasons: (a) tips and tricks were bound to be fairly boring for people interested in other topics that I blog about, and (b) because both of these operating environments are proprietary and I feel that sites that have historically aggregated my blog did so for its open source content. http://tirania.org/monomac/archive/2010/Nov-30.html miguel@gnome.org (Miguel de Icaza) http://tirania.org/monomac/archive/2010/Nov-30.html Wed, 01 Dec 2010 00:33:00 -0500