A first look at Silver: how to write a crossplatform iOS / Android app in Swift


RemObjects opened in private beta their Silver language that provides a way to develop both iOS, Android and Windows Phone mobile applications by using Apple Swift language. Since the 18th March, this project has officially passed 1500 beta testers showing that Silver arouses keen interest among mobile developers community.

Last february, InfoQ released a nice interview of RemObjects about this new cross-platform solution, untitled:

Silver Brings Apple’s Swift Language to .NET and Android

You can read it here as an introduction of the following lines.

Silver is a free implementation of Apple Swift v1.2 specifications and compiles Swift source code to Java byte code and .NET/IL byte code, in order to respectively build Android and Windows Phone apps.

As it looks very promising and made by well-known cross-compilation specialists, I decided to get a first look at Silver that puts Swift as a center of a multi-platform way to develop native apps, like Xamarin does on its side with C#.

In addition, it’s important here to underline that Silver’s technical approach is different to Phonegap aka Apache Cordova one’s in order to produce iOS / Android / Windows Phone application. In fact, Silver produces cross-compiled native apps, e.g generating native byte codes, while Phonegap encapsulates JavaScript/HTML5 source code into a native envelope that provides access to low-level OS features.

Silver dev environment setup

Get ready to rummmble!

Let’s start with a simple Calculator app (please don’t sigh …) that will run the same logic on both iOS and Android.

Firstly, you have to register to the private beta program here. As soon as you received your confirmation mail, you will be able to download Silver OSX and Windows SDK.

As I am a decerebrate Apple fanboy, I will continue this tutorial on an OSX environment, sorry for Windows addicts! ;)

Nahhh, I am more open minded than you are obviously thinking ^^, as my technical mobile background since 2004 is J2ME / DoJa (double RIP combo) then Java Android.

However, since 4 years now, I mostly use JavaScript & HTML5 in order to build crossplatform apps using Phonegap/Cordova. In fact, we (at Ubidreams) released our own open source JavaScript framework named Nanoko that provides a way to architecture your hybrid application with modular & dynamic services and to industrialize your mobile developments thanks to Apache Maven .

Despite the fact that RemObjects is providing a way to use Silver in Xcode, I chose to use their own Fire IDE, that looks really nice and fully integrated with Silver, Mono and Android SDK.

Yes I am a lazy man that don’t want to lose time to setup and to switch between IDEs ;) All you need to setup Fire is explained on their great docs section.

Below, Fire IDE Preferences show the integration of all SDKs that are needed in order to cross-compile any Silver application source code:

Mono, Android, Java and Silver SDKs integration in Fire IDE

SilverCalc solution architecture

Here, the architecture I chose in order to build a cross-platform mobile calculator application:

  • common Swift code that implements all calculation operators (+, -, x, /, =, ., +/-) … yes that calculator rocks! ^^
  • iOS UI implemented in Swift
  • Android UI implemented in Java

Later, I will study if Silver provides a way to generate Android UI from iOS one’s, even if RemObjects says in their InfoQ interview that cross-platform apps should implement a different UI on each OS. Well, perhaps … but I don’t think that Angry Birds UI (or my Calculator app one’s) needs to be adapted specifically to each OS.

In order to fit this crossplatform project architecture, I created a Fire solution (packed as a .sln file) named SilverCalc that contains:

  • a shared project named SilverCalc-shrd
  • an iOS universal application project named SilverCalc-iOS
  • and finally an Android application project named SilverCalc-android (package name: org.rubiq.silver.calc in the following picture).
A Fire solution configured with shared, iOS and Android projects

Now it’s time for you to download this SilverCalc solution source code on GitHub and try it on your own! ;)

PS: please check this README.txt before building and running Android calc, it’s a fix to use until the next Silver release.

Swift (e.g Sugar) calculator shared code

SilverCalc-shrd shared project is intended to host all common code between iOS and Android apps. I wrote only one class file named Calculator.swift in order to implement all calculation features: number building, math operators, in-memory computation.

I started by writing some classical Swift code but as soon as I was writing some Int32, List and String related code I thought this:

Mannn (yes I talk a lot to myself) … this code needs to be crossplatform and compatible with different kinds of software and hardware architectures … what’s about little Endian and Big Endian issues, data structure translation etc etc…”

This conducted me to discover that Silver provides Sugar library which ensures to write bulletproof crossplatform instructions by translating Sugar code to machine (native) code.

For exemple to handle crossplatform lists, Sugar provides its own Sugar.Collections.List library. I used it in order to manage each digits, entered “tap by tap” by the user on the calculator keyboard, as a first member of a double:

var firstMbrs : Sugar.Collections.List<Double> = Sugar.Collections.List<Double>()

Another example is the way Sugar enabled me to convert a Swift Double to a Sugar.String:

var result: Sugar.String = Sugar.Convert.ToString(getDoubleValue())

Sugar provides a complete set of features like types (String, Binary etc.), data structures, XML parsing, I/O etc … that are mandatory to write a real crossplatform source code. However, a lot of standard Swift types are kept like Double, Int32 etc.

So for me, in a Silver way of life, in order to write some shared code:

“ Silver is more about writing Sugar code with a Swift syntax than writing pure Apple Swift code ”

In fact, if you look at Calculator.swift, there’s no import of Foundation!

But this is not bad at all, as building cross-platform apps requires to write cross-platform code (yes yes trust me ^^), Sugar prevents developers to enter in this abyssal and scorching hell!


iOS calculator implementation

So as my objective is not to teach anyone to make a calculator app (yes I have limited skills ^^), I want to show you very fast how both Silver & Fire enable the developer to use a shared project from an iOS universal application project.

It’s really simple, you just have to drag’n’drop the shared project into the References section of your iOS project (SilverCalc into SilverCalc-iOS > References in the following picture).

Drag’n’drop a reference between two projects in Fire solution

To finish the setup of my iOS project, I needed to reference libSugar.fx / libSugar.Data.fx as my shared code contains Sugar crossplatform code, in addition to libSwift.fx / libNougat.fx as both my shared code and my iOS app are written in Swift.

To create my iOS Calc app, I just implemented the UI controller in RootViewController.swift and the UI view in Main.storyboard. In order to edit this storyboard, you can right-click the SilverCalc-iOS project in Fire and select Edit User Interfaces Files in Xcode option.

RootViewController instanciates the shared Calculator features (no needs to import Calculator) and uses it in each UIButton action targets (e.g listeners):

// shared Silver/Sugar class
var calc : Calculator = Calculator()
... 
func equal(sender:UIButton!){
...
calc.process()
}
// this piece of code is just ... (sigh)

Like Xcode, Fire provides a complete section, Settings, to manage your iOS application build and distribution settings. In order to build and run your app, you have to select your project in Project popup, set your configuration to Debug or Release mode, and choose an iPhone simulator from settings or run it on your device through Crossbox Server popup.

iOS application settings panel for signing, packaging, building, compiling

So here is the result in iPhone Retina (4-inch) simulator / iOS 7.1:

iOS Calculator powered by Fire & Silver - beautiful UI, isn’t it? :p

If you are seeking for an UI designer in order to contribute to your awesome stylish project … damn, yes I’m in! ^^

Android calculator implementation

One thing that surprised me at first, when I created the Android application project in Fire, was the MainActivity.swift file. In this file, your Java Android code is written in Swift!

import java.*
import android.*
public class MainActivity: Activity {
public override func onCreate(savedInstanceState: Bundle!) {
super.onCreate(savedInstanceState)
ContentView = R.layout.main
}
}

This exercise is quite tricky at start as a Silver developer has to:

“ think Java Android , write Swift

NB: Fire provides a way to convert Java Android source file to Swift via File > Add & Convert but it seems to not work on my environment (RemObjects team will surely fix it in the next release).

I used Android Studio to create the calculator UI, e.g the main.layout-xml file. Fire offers a shortcut to edit your layouts in Android Studio, just right-click your Android project and select Edit User Interfaces Files in Android Studio.

As an aside, Android Studio works as simple as Xcode Storyboard Editor, king congrats to Google Android & Apple Xcode dev teams for these efficient UX/UI tools! Whhaaat?! I give me the right ^^

Silver is still in beta, so I faced some small difficulties and bugs, like inner class definition that does not pass compilation step:

// in Silver for Android do
bt.setOnClickListener { self.myCallback() }
// instead of 
bt.setOnClickListener(
View.OnClickListener(){
public func onClick(v:View) {
self.myCallback()
}
}
}

However RemObjects dev team and community are really active! I was surprised by their celerity & efficiency to answer my questions in their talk forum (that is very well designed too!).

Finally, the Android Silver source code is really close to the iOS one’s, as you can see in the following code blocks comparison:

// Android UI controller: set listeners to each buttons
for bt in buttons {
if bt is Button {
if (bt as! Button).getText() == “x" {
bt.setOnClickListener{self.multiply(bt)}
}
...
}
...
}
// iOS UI controller: set targets to each buttons
for bt in buttons {
if bt is UIButton {
if bt.titleLabel.text == “x" {
bt.addTarget(self,
action: “multiply:”,
forControlEvents:UIControlEvents.TouchUpInside)
}
...
}
...
}

Therefore, porting the UI controller from iOS to Android was incredibly fast!

And here, we start to see the potential of Silver approach by using Swift as a central language to write both shared and controllers code ”

In order to test the application, you just have to launch an Android emulator through Fire’s Tools > Java > Launch Android Virtual Devices Manager menu, select the emulator in Crossbox Server popup (don’t worry if the popup does not show the emulator name after your selection, it works, and I am sure it will be fixed soon), then build & run your app!

Android version of SilverCalc powered by Fire & Silver

Conclusion

With Silver, I was able in less than 2 days to discover the tools, the workflow and to make a first simple crossplatform app. The learning curve is impressively low, helped by a great documentation and a “swift” + proactive RemObjects team (which is really friendly too! ^^).

As I said at the start of this story, I was not a supporter of cross-compilation to build cross-platform mobile apps. But Silver is starting to convince me to use it for certain kind of projects where UI needs to be specific to the platform or requires to be very reactive (like 3D mobile games for example).

As a conclusion, I am sure that Silver will have a great future, may be even greater if it stays in a free licensing model. Furthermore, despite the fact it is still in beta version (and therefore requires some enhancements), it already enables developers to make compelling cross-platform apps in a simple way!

A last thought about Silver in the perspective of this first tutorial:

Today it’s really simple to make a really simple app … and by now … it’s really simple to make a really simple crossplatform app thanks to Remobjects Silver!

This was my first tutorial on Silver, I will release soon another story by going deeply into crossplatform development with complex I/O based shared code.

Did you like this story?

If yes, share it and follow me on Twitter, you will be informed about my next shot ;) You can recommend this article below too.

Otherwise, I just created a Google+ Silver community, feel free to join it and share your own experiments.

If not, skip it! :p

Cheers!