Newer posts are loading.
You are at the newest post.
Click here to check if anything new just came in.

June 05 2012

Developing cross-platform mobile apps with C#

Building a mobile app that runs on more than one platform, with minimal code changes, is a technical Holy Grail. The HTML5 stack (using CSS and JavaScript, among other standards) and Java are two solutions. Another is Microsoft's .NET plus C#, a combo that's been overlooked outside the Windows Phone 7 developer community despite its potential to create apps that can run natively on Android and iOS.

In the following interview, software engineer Greg Shackles (@gshackles) aims to expand the potential of this platform. Shackles is the author of "Mobile Development with C#" and maintains a blog focusing mainly on .NET and its related technologies.

We hear a lot about using C++ to build mobile apps, so why use C# — and the associated .NET?

Greg Shackles: There are various ways to share code across different platforms. Unfortunately, many approaches will abstract away the user interface from the developer in order to achieve a "write once, run anywhere" solution, making it easier to release an application quickly on many platforms. This sounds great, but often it will lead to a degraded user experience since the app won't look and feel native to that platform. The user experience is the most important thing to consider when designing an application.

Using C# and the Mono Tools allows the developer to share a large subset of an application's code across multiple platforms while still building a completely native user interface on top of it for each platform. Applications created with this approach will look and feel native because they're using the exact same APIs and toolkits exposed by the platform. In some cases, the Mono tools even help to clean up the platform APIs to make them easier to work with than those exposed by the native languages.

This approach allows developers to concentrate on solving business problems rather than having to manage multiple languages and reinvent the wheel every time they want to expand to a new platform. Going even further, the code that is shared across platforms isn't limited to mobile applications. It can go pretty much anywhere that C# and .NET are supported, such as ASP.NET, Silverlight, or WPF. Developers already familiar with these technologies can easily hit the ground running and start targeting these new platforms while reusing the skills they already have.

What else makes the .NET Framework well suited for mobile development?

Greg Shackles: C# and .NET are both very mature and powerful technologies. They have evolved over the years to provide support for things like asynchronous programming and memory management, and features like LINQ help make them great to work with as a developer.

For example, there is no garbage collector when writing iOS apps with Objective-C. That's a feature .NET developers are used to having. MonoTouch actually brings a garbage collector along with it, making it much easier to work with, without having to worry about manual memory management.

What are a few of the technical weaknesses of C# or .NET?

Greg Shackles: There aren't too many technical limitations, but whenever you place another layer between you and the native platform, some problems are unavoidable.

One example is that on iOS, you are not allowed to dynamically execute code at runtime, meaning that the standard .NET style of just-in-time compilation is not permitted and that aspects of .NET that rely on runtime code compilation are not possible, such as Reflection.Emit and the Dynamic Language Runtime. To get around this, MonoTouch compiles the application down to static code ahead of time. This particular limitation does not apply on Android, which does allow for just-in-time compilation.

For those who are already developing native apps for Android or iOS, what benefits would they gain from using C#?

Greg Shackles: For developers who have already built their apps in Java in Objective-C, the case for switching to a new set of tools definitely becomes more difficult to make. The benefits they would get from making such a move would largely be in the ability to share code across all of the platforms rather than have to rewrite it in a different language every time. Both MonoTouch and Mono for Android offer the ability to interact with code written in Objective-C and Java, so code already written in those languages could still be leveraged.

What kind of cross-platform mobile apps are easy or best to make under C#?

Greg Shackles: I don't think there's any particular category of app that's obviously more difficult to write in C#. For extremely simple applications that don't have much logic, it becomes more of a decision of preference for the developer rather than a strategic advantage. In reality, not many applications fall into this category. A majority of applications will need to perform tasks like accessing the Internet or saving to a database, and that is where it becomes beneficial to be able to write that code once and share it across all platforms. Personally, I find C# to be a much nicer language to work with than Objective-C and Java, so that alone becomes an advantage of using it.

.NET is native on Windows Phone 7, but it's not on Android or iOS without the use of MonoTouch or Mono. What are the performance issues or differences across these mobile platforms when you're developing for all three at once using C# through .NET and its unofficial variants?

Greg Shackles: The addition of another layer between you and the platform will have its consequences, but by and large, it's not something you'll notice or need to worry about as a developer. Since MonoTouch applications are run through its ahead-of-time compiler, their performance is already highly optimized. Mono for Android applications include their own instance of the Mono runtime that .NET code is run against and includes an intelligent garbage collector that is optimized for managing objects across the different runtimes. In general, you won't be able to see any difference in performance between an app written in C# and one that is not.

One other common concern is the size of the application, since the .NET Framework is not known for being minimal. Both Mono for Android and MonoTouch ship with a tool called a linker that is included as part of the build process. The linker is a static analysis tool that scans the compiled assemblies in the application and actually strips out any pieces of the framework that are not referenced. As a result, your application will only ship with precisely the pieces of the .NET Framework that you actually use, which drastically cuts down the size of the application. With each release, the Mono team seems to find new ways to optimize the linking process, so this size overhead continues to dwindle down further, even though it is already rather minimal.

This interview was edited and condensed.

Mobile Development with C# — This hands-on guide shows you how to reuse one codebase across iOS, Android, and Windows Phone by combining the business logic layer of your C# app with separate, fully native UIs.

Related:

May 18 2011

Developer Week in Review: Buying a lawsuit with an in-app purchase

Hello, and welcome to another fun-filled week of frolic and mayhem in the software industry. We'll get right to the news, but first this short commercial message.

Do you suffer from the heartbreak of buffer overruns? Has your SQL been injected? Do you stay awake at night because of cross-site scripting attacks? If so, try new Hackitol Plus, now available in convenient 8-hour strength. Don't let poorly secured applications keep you from leading the life you want to have. Note: Side effects may include nausea, heart palpitations, and the inability to use Flash or Facebook. Consult your doctor if you are currently developing in JavaScript.

And now, back to our program.

iPhone developers ask for whom the suit trolls

The continued three-ring-circus that is software intellectual property continued to roll right along last week, with a group of iPhone app developers the latest to feel the sting. Lodsys sent legal nasty-grams to a number of developers who were taking advantage of the evidently patented idea of doing in-app purchases. This has evidently led Apple to put some new iPhone apps, which use the feature, on hold.

Interestingly, Lodsys claims that Apple, among others (including Microsoft and Google) already licenses the patent, but that it doesn't extend to developers using Apple's in-app function. That's going to be an interesting argument to watch play out. Does that mean if Apple licensed a technology to render an iOS control, and developers use that control in their applications, they'd need to get a license as well?

Apart from being a headache for both Apple and the developer community, there could be other far-reaching ramifications. For example, would Steam's in-game purchasing of weapons and clothing be subject to the same patent? Until Congress or the courts step in and stop the madness, it's anyone's guess.

OSCON 2011 — Join today's open source innovators, builders, and pioneers July 25-29 as they gather at the Oregon Convention Center in Portland, Ore.

Save 20% on registration with the code OS11RAD

Mono strikes out on its own

MonoAs previously reported, Novell's new overlords (that would be Attachmate, which still sounds like some kind of "As Seen On TV" product to me) gave the Mono developers their walking papers last week. Now Mono guru Miguel De Icaza has formed a new company to pick up the pieces. The company, called Xamarin (which sounds like a prescription sleeping aid to me), will offer commercial Mono support, as well as .NET tools for Android and iOS.

Knit One, perl 5.14

Perl 6 may be languishing out there with "Duke Nukem Forever," but there's still new perl to be had. This week, perl 5.14 hit the streets. Improved Unicode support seems to be a major thrust of the release (click here for all the gripping details.)

For those of us who grew up (professionally, at least) with perl in our toolbag, it's good to see continued active development on the language. While I may not pull that particular tool out as often as I used to, I still find myself writing the occasional script to grovel over a file and pull out the golden nuggets I need.

Got news?

Please send tips and leads here.



Related:


Older posts are this way If this message doesn't go away, click anywhere on the page to continue loading posts.
Could not load more posts
Maybe Soup is currently being updated? I'll try again automatically in a few seconds...
Just a second, loading more posts...
You've reached the end.

Don't be the product, buy the product!

Schweinderl