Creating a Mobile App
Last updated
Last updated
Time to have some fun and create a mobile app that consumes our REST API. The fact that our API, for now, does not require authentication makes the mobile app relatively straightforward. The mobile app will be created with Ionic Framework, and we will also use the fantastic Ionic Creator to quickly build the user interface and write some code. Ionic Creator has a free version that allows you to create one project and export the code to continue building the application on your development machine. The screenshot below shows the Ionic Creator experience:
When you create a new project in Ionic Creator, use the settings below:
We start from a blank template and we use Ionic 1 instead of Ionic 3. Creator support for Ionic 3 is still in the alpha stage. Note that Ionic 1 is very different from Ionic 3 because Ionic 1 uses AngularJS where Ionic 3 uses Angular 3. Or was it Angular 4, or 5, or... I don't know about you but I tend to lose track of Angular versions these days.
Anyway, when you see the empty iPhone picture, you can start building the user interface. Select the single page from the Pages box and, in the properties of the page, do the following:
set the title of the page; I used Influx Measurements
enable the Right Header Button and use the ion-ios-refresh-empty icon
add an Angular directive to the page called ng-click; next to ng-click type submit(); use the <-> icon to place the directive in the right header button (button HTML element)
Obviously, we still have to write the submit() function. That function will do the actual call to the REST API and populate the rest of the page with the results.
Under the header, drag a List and, inside the List, drag a Container List Item. In the properties of the Container List Item, add an ng-repeat directive and in the text box next to it, type measurement in data as shown below:
The variable data is an array that will hold the return values of a call to the Influx REST API. Using ng-repeat here just repeats the Container List Item for each item in the array. Each item can be referenced with measurement like in measurement.device.
It is not very useful to repeat the Container List Item without displaying something in each item. To do so add four markdown components in the Container List Item and use the text below:
We are using measurement which comes from measurement in data of our ng-repeat directive.
Click Code at the bottom left of the screen to open the code editor:
write an AngularJS service that calls the API at http(s)://host/measurements/last/number where number is a parameter we can set when we call the service. In the screenshot above, we use https:// with a fully qualified domain name but in our example, use the IP address of the influx-rest Kubernetes service and http
in the controller of the page, above Influx Measurements, define our $scope and methods to call from the page like submit()
The service is shown below (services.js):
The service Influx is defined and has the $http service injected. We use $http to interact with HTTP resources. In our case, we need to do an HTTP GET to our service. The methods of our service are encapsulated in an object called ret and returned. I have chosen to define one method, last, to retrieve the last measurements. The amount of measurements to retrieve is set with a parameter to the last method. If this is new to you, I recommend taking a look at https://www.w3schools.com/angular/angular_http.asp.
Let's see how to use this service (controllers.js, in the Creator Interface named Influx Measurements):
Because we want to use our Influx service, you have to inject it by specifying it as a parameter to the controller function. The AngularJS $scope variable is used and $scope.data is set to an empty object. When we click the refresh icon, the $scope.submit function is called and that function calls the last method of the Influx service. The JSON response is stored in $scope.data. The other function is just a simple helper function to display the time and data correctly.
If you use http and the IP address of your Kubernetes service, and you try to run the app in Creator by clicking the Eye icon, you will notice that the refresh button does not work. If you open the browser's developer console, you will notice an error as well. In Google Chrome:
Because Ionic Creator is using https, you cannot make a call to an insecure endpoint. To be able to test this application, we will continue development on our local workstation
Ionic Framework uses HTML5, CSS and JavaScript to build so-called hybrid mobile apps. This means we can also test and run the application in the browser. To bring a Creator app to your desktop, first install Ionic. Go to https://ionicframework.com/getting-started to find out more about installation. You will need NodeJS as well!
After installation, you can use the ionic executable to create a new project. From the command prompt, in a folder where you want to create the Ionic application,, type the following command:
When the command finishes, you end up with a folder called Influx. Open that folder in your favorite code editor:
To obtain the zip archive, in Ionic Creator, click the Export Your App button in the top right section of the screen. You will also find that option on the dashboard.
When you have replaced the files, open a command prompt and type ionic serve. A development web server will be started and your app will be shown:
Retrieving the measurements over http now works because the development server also uses http!
You can also test the application on your mobile device. With recent versions of Ionic, you see the following output when your run ionic serve:
Notice the last line that begins with DevApp? If you download Ionic DevApp from the iOS or Android App Store you will see this project in the app if your mobile device is on the same network as your development workstation. Pretty cool no?
In this section, you saw how to create a mobile app prototype quickly and easily using Ionic Creator. It takes much more work to create a fully functioning app with more configuration options, more screens and graphics. For now though, we will leave the app in this state and create enhancements as we go along.
We will do two things:
Your www folder will look a bit different because it contains the starter code for a blank project. We need to export the Ionic Creator application to a zip archive and replace the files in the www folder with the files from the zip archive.