+

Search Tips | Advanced Search

For up-to-date product documentation, see the IBM MobileFirst Foundation Developer Center.


Some initial WebView code for connecting to the server

Most of your Cordova app is developed using the web resources. After creating your Cordova app and adding the MobileFirst plug-in we can add MobileFirst functionality.


WebView files

In this example a simple index.html file creates the initial screen containing a single button. This button is defined in the imported index.js file.

Note: When running the Cordova CLI build or prepare command, the web resources in main [projectname]\www folder are copied to each platform's www folder ([projectname]\platforms\[platform]\assets\www). Therefore in this case you need to edit the main www copy if you are going to build with the Cordova command. If you are going to build with platform IDE such as Android Studio or Xcode, you need to edit the local platform copy.

After creating your Cordova app using the Apache tools and adding the MobileFirst plug-in, the index.html and JavaScript files do not include any examples of the MobileFirst API. Here is an example of accessing a server resource (adapter).

Note: You need to register your app before we can access the server. For more information, see Register Cordova applications to MobileFirst Server.


The index.html file

The app starts up with the initial index.html The button is included in the html:

The resourceRequestBTN button is defined in the index.js file.


The index.js file

The index.js adds the listener to the resourceRequestBTN button which calls the resourceRequestGetBalance function.

The resourceRequestGetBalance function requests the server resource (an adapter) and returns either the response or an error.

For information on creating and deploying the adapter see Develop the server side of a MobileFirst application.


Testing the connection to the server without deploying an adapter

If we want to test the app registration and connection to the server without the need for deploying an adapter, we can test a request for an access token. If no security checks have been added to our app the access token will succeed once the registration and connectivity to the server are working.

To test the security token access replace the resource button with this button in the file:

In the index.js file replace the resource button listener with the access token button listener for invoking getToken:

and add the getToken function

If the connection succeeds you will see the contents of the token. If not the error is displayed.


The sample index.js file for the resource request check:

The entire index.js file for testing the resource request appears below.

Parent topic: Cordova WebView