Since the use of Objective C to develop Native iOS applications, many alternative iOS applications have been developed in other languages. For example, the following lists some of them:
- PhoneGap, developed using HTML/JS
- RubyMotion, using Ruby
- Ionic, developed using AngularJS
I recently discovered RoboVM, which allows you to develop iOS programs in Java. This article describes how to use RoboVM and Java to develop a simple application.
Below are some preparations:
- OSX 10.9.3
- JDK 1.7
- Eclipse kepl 4.3
- Xcode 5.1.1
First, install a RoboVM plug-in for eclipse. Run http://download.robovm.org/eclipse/under the help -- install the new software /. Restart the IDE.
After the plug-in is installed, you can create a new project. Select the "RoboVM iOS Project" option.
Enter the project name, main class, application name, and Application ID as HelloWorld. As shown below.
I don't know why. When I use the wizard, the main class is not successfully generated and can only be done manually. Create a HelloWorld class.
Paste the following content into 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 inherit the UIApplicationDelegateAdapter class, which is the UIApplicationDelegate of RoboVM. Class contains a main method, which is used to start the application.
Now we need to rewrite the didFinishLaunching method to generate a UIWindow and UILabel. Put the following code in 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 is self-explanatory. The CGRect construction parameters are the x, y coordinates and the length and width of labels.
If you right-click the project and choose to run it as an iOS simulator application. You will see an iOS simulator with a green screen and text displayed, "Hello World ". As shown in the following figure.
If you have reached this step, it means that your environment has been set up, you can start to use Java to develop iOS programs.
The following are some references. I hope this article will help you use Java for iOS development.
- Source code of this Article
- RoboVM
- RoboVM on Twitter
- RoboVM on google groups
- PhoneGap
- RubyMotion
- Ionic
Original address.