RoboVM: use Java to develop iOS applications

Source: Internet
Author: User

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.

 
 
  1. import org.robovm.apple.foundation.NSAutoreleasePool;   
  2. import org.robovm.apple.foundation.NSDictionary;   
  3. import org.robovm.apple.foundation.NSString;   
  4. import org.robovm.apple.uikit.UIApplication;   
  5. import org.robovm.apple.uikit.UIApplicationDelegateAdapter; 
  6.  
  7. public class HelloWorld extends UIApplicationDelegateAdapter { 
  8.  
  9.     @Override 
  10.     public boolean didFinishLaunching(UIApplication application, 
  11.             NSDictionary<NSString, ?> launchOptions) { 
  12.         return true; 
  13.     } 
  14.  
  15.     public static void main(String[] args) { 
  16.         NSAutoreleasePool pool = new NSAutoreleasePool(); 
  17.         UIApplication.main(args, null, HelloWorld.class); 
  18.         pool.close(); 
  19.     } 
  20.  

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.

 
 
  1. @Override 
  2.    public boolean didFinishLaunching(UIApplication application, 
  3.            NSDictionary<NSString, ?> launchOptions) { 
  4.        UIWindow window = new UIWindow(UIScreen.getMainScreen().getBounds()); 
  5.        UILabel label = new UILabel(new CGRect(50, 50, 100, 50)); 
  6.        label.setText("Hello World"); 
  7.        window.addSubview(label); 
  8.        window.setBackgroundColor(UIColor.colorGreen()); 
  9.        window.makeKeyAndVisible(); 
  10.        return true; 
  11.    } 

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.