Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

HelloWorld with Java, iOS and RoboVM

DZone's Guide to

HelloWorld with Java, iOS and RoboVM

· Java Zone
Free Resource

Learn how to troubleshoot and diagnose some of the most common performance issues in Java today. Brought to you in partnership with AppDynamics.

Ever since native iOS applications have been developed with Objective C, there has been a surprising emergence of alternatives to using Objective C as the language to develop these applications. For example, just to name a few:

The latest one that I have come across is RoboVM, that allows for iOS applications to be written and deployed using the Java language. This post will go through the steps of getting a very simple application up and running with RoboVM and Java.

Here are the pre-requiestes:

Firstly, within Eclipse, install the RoboVM plugin, under Help, Install New Software, http://download.robovm.org/eclipse/. This will need to restart the IDE.

With the plugin installed, you can create a new project. Select the'RoboVM iOS Project' option.

Just use 'HelloWorld' as the project nameMain classApp name and App id. It should look like the screenshot below.

For some reason, when I use the wizard, the Main Class does not get created, so this needs to be done manually. So create a class calledHelloWorld.

Put the following contents in the HelloWorld class.

import org.robovm.apple.foundation.NSAutoreleasePool;  
import org.robovm.apple.foundation.NSDictionary;  
import org.robovm.apple.foundation.NSString;  
import org.robovm.apple.uikit.UIApplication;  
import org.robovm.apple.uikit.UIApplicationDelegateAdapter;

public class HelloWorld extends UIApplicationDelegateAdapter {

    @Override
    public boolean didFinishLaunching(UIApplication application,
            NSDictionary<NSString, ?> launchOptions) {
        return true;
    }

    public static void main(String[] args) {
        NSAutoreleasePool pool = new NSAutoreleasePool();
        UIApplication.main(args, null, HelloWorld.class);
        pool.close();
    }

}

Here we are extending the UIApplicationDelegateAdapter which is RoboVM version of the UIApplicationDelegate. We have a main method in the class which launches the application.

What we will do now is override the didFinishLaunching method to create an UIWindow and an UIlabel. To achieve this, add the following details to the method.

   @Override
    public boolean didFinishLaunching(UIApplication application,
            NSDictionary<NSString, ?> launchOptions) {
        UIWindow window = new UIWindow(UIScreen.getMainScreen().getBounds());
        UILabel label = new UILabel(new CGRect(50, 50, 100, 50));
        label.setText("Hello World");
        window.addSubview(label);
        window.setBackgroundColor(UIColor.colorGreen());
        window.makeKeyAndVisible();
        return true;
    }

Most of the code above should be pretty self explanatory. The arguments into the construction of the CGRect class are the x and y co-ordinate followed by the label's width and height.

If you right click the project and select Run as/iOS Simulator App (iPhone). You should be presented with an iOS simulator with a green screen with the text "Hello World". Something like the screenshot below.

If you have reached this point, it means your environment has been setup correctly and you should be able to start developing iOS applications with Java.

Here are some references for more information. I hope this post has been useful in showing you the first steps to iOS development using Java.

Understand the needs and benefits around implementing the right monitoring solution for a growing containerized market. Brought to you in partnership with AppDynamics.

Topics:

Published at DZone with permission of Ricky Yim, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}