Chapter 8. Mobile App Development with NativeScript for Vue.js

Mobile app development with JavaScript in the past has been a bit underperforming, literally. Nothing against the early frameworks (the early 2010’s), but those frameworks like PhoneGap just took your mobile website, wrapped it with a native “wrapper”, and packaged it into an “app” (hybrid-app) and shipped it to the Apple AppStore or Google Play Store. Like jQuery, PhoneGap and the others had their time. They really brought the mobile development world within reach of front-end web developers across the world. Back in those days, a user could really tell the difference between a hybrid and a native app; one was glitchy with performance problems and the other was polished and performant. That all changed however when React Native joined the scene.

React Native is a first party framework created by Facebook that lets users who are familiar with React to build real native mobile applications. All of the React Native code is translated into real native components like UILabal, UIImage and UIStackView in Apple’s UIKit framework. A user or a bot would not be able to tell the difference between an app built in Swift or one built in React Native because they’re both native. You can as a developer even write some native Swift or Java code for your native iOS and Android apps, respectively.

With that said, as PhoneGap pioneered the hybrid app, React Native pioneered the native app built with JavaScript. For a while, since Vue.js was the new kid of the block, it did not have a “native option.” That was the case until 2016.

The year 2016 was a huge year for Vue.js in general, but also for its native app framework needs. There is no first-party solution, but there are third party solutions. Most notably there is Weex, created by the Chinese giant, the Alibaba Group, and NativeScript for Vue, an open source project. This chapter will be focusing on NativeScript for Vue (not to be confused with NativeScript for Angular) as it’s the most polished and accepted of the two.

It’s safe to say that NativeScript for Vue is the unofficial recommendation of the Vue.js Core Team. Jen Looper of NativeScript is often invited to speak at large scale Vue conferences around the world and has since started Vue Vixens; a group for people who identify as women who want to learn Vue.js. NativeScript has an interactive playground that you can explore and work with NativeScript without setting up an environment.

Installation

We’re going to be using the NativeScript Playgrounds so you can get right to the point versus setting up a development environment. If you wish to set up a development environment, below are some instructions. Unfortunately, the Node.js live server that Vue.js developers have been accustom to, does not apply when developing native mobile applications. The reason is, you need SDK’s or Software Development Kits for each operating system; iOS and Android.

The installation is different for the two main desktop operating systems: macOS and Windows 10.

Prerequisites for macOS and Windows

With iPhones, you can only develop iOS applications on an Apple Macintosh computer. With that said, if you intend to launch your application for both platforms, you need to have a Mac with macOS installed at some point during this journey. Anyway, in order to use NativeScript for Vue on your computer, you will need some sort of system to build your application, like Node.js. You should have Vue.js already installed if you have been following along with the book. These instructions can also be found on the official NativeScript documentation website.

The first thing that you will need, is the NativeScript CLI.

$ npm install -g nativescript

To verify that the package was downloaded correctly, use the command tns. If installed, you should see a list of commands.

macOS Installation

To use on macOS you will need macOS Mavericks or later. You will also need a software called, “Homebrew” installed on your machine. This lets you download additional software that Apple has not included into the base operating system.

Installing iOS SDK

1. Install Homebrew

$ ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

2. Update to the Latest Node Version

$ brew update
$ brew install node@8

3. Install iOS SDK Dependencies

You will need to have Xcode installed on your machine in order to build applications for iOS. You can find Xcode in the App Store. The only official release of Xcode comes from Apple in the Mac App Store. Do not download it anywhere else. When you’ve done that, log in to (or create) your Developer’s account and download the Command Line Tools for Xcode.

# Install XcodeProj
$ sudo gem install xcodeproj

# Install CocoaPods
$ sudo gem install cocoapods

Installing Android SDK

1. Install JDK 8

$ brew tap caskroom/versions
$ brew cask install java8

2. Set Java in your Environment Variable

$ export JAVA_HOME=$(/usr/libexec/java_home)

3. Install the Android SDK

$ brew cask install android-sdk

4. Set Android SDK in your Environment Variable

$ export ANDROID_HOME=/usr/local/share/android-sdk

5. Install remaining Android Packages

$ $ANDROID_HOME/tools/bin/sdkmanager "tools" "platform-tools" "platforms;android-25" "build-tools;27.0.3" "extras;android;m2repository" "extras;google;m2repository"

6. Setup Android Emulators (AVD)

The NativeScript documentation has a great walkthrough to follow. It’s recommended to follow through that.

7. Install NativeScript CLI and Check Configuration

$ npm i -g nativescript
$ tns doctor

If you see, No issues were detected, you’re up running.

Windows Installation

With Windows, you can only develop for Android; due to Apple’s limitations, you will need a Mac to package and launch the iOS version of your application.

1. Install Chocolatey

Run the Command Prompt as Administrator and install Chocolatey for simpler installation and configuration.

@powershell -NoProfile -ExecutionPolicy unrestricted -Command "iex ((new-object net.webclient).DownloadString('https://chocolatey.org/install.ps1'))" && SET PATH=%PATH%;%ALLUSERSPROFILE%chocolateyin

Be sure to restart the Command Prompt.

2. Install Google Chrome

You will need to install Google Chrome to debug NativeScript apps for Android on Windows.

choco install googlechrome -y

3. Install the Latest Node.js LTS

choco install nodejs-lts -y

4. Install JDK 8

choco install jdk8 -y

5. Install Android SDK

choco install android-sdk -y

6. Install Remaining Android SDK Packages

"%ANDROID_HOME%	oolsinsdkmanager" "platform-tools" "platforms;android-25" "build-tools;27.0.3" "extras;android;m2repository" "extras;google;m2repository"

7. Install Android Virtual Devices (AVD)

Be sure to run the Command Prompt as Administrator.

@powershell -NoProfile -ExecutionPolicy Bypass -Command "iex ((new-object net.webclient).DownloadString('https://nativescript.org/setup/win-avd'))"

Be sure to restart the command prompt.

8. Install Android Studio

choco install androidstudio -y

Follow the official steps to create and manage all the virtual devices from AVD.

9. Install NativeScript CLI

$ npm i -g nativescript
$ tns doctor # test to see if installed properly

Getting Started with Playgrounds

Keep in mind, when developing a real production application, you will need to configure a development environment, which you can walk through with their official documentation.

  1. Download the “NativeScript Playground” and “NativeScript Preview” apps for the App Store or Google Play Store.
  2. Visit the NativeScript Playgrounds website.
  3. Scan the QR code on your phone.
  4. Follow along.

First of all, NativeScript Playgrounds are amazing. Just type your code in the web browser, click “Preview” and watch your phone update over the network. If you are all set, let’s explore NativeScript for Vue together.

Differences Between Vue.js and NativeScript

Before you get too far down into NativeScript, let’s point out a few differences between traditional Vue.js development and NativeScript development. For starters, you are not writing HTML. You are writing these HTML-like tags that are really components that convert into native components. For instance, if you want a <p>, in NativeScript you are actually going to write <Label /> instead. The reason being is that <p> does not convert well into the native UILabel and Label components for iOS and Android, respectively. Another caveat is instead of using v-on:click for click events, you are using the @tap="" event for tap events.

Lastly, all of the template markdown in a traditional Vue.js application is actually using string literals instead of the <template /> component in the single-file Vue component format. All of your methods and logic will live above the template still in the form of methods, computed, etc.

Playing With the Code

So, to begin. Open up the app.js file in Playgrounds and delete everything but the <ActionBar /> component. NativeScript needs a base “view” just like Vue.js needs a root element. We’re going to use the <StackLayout /> component, so when you keep adding components inside, they just stack on top of each other. Let’s add a few things before building an interface.

app.js

const Vue = require("nativescript-vue");

new Vue({

  template: `
    <Page class="page">
      <ActionBar title="Home" class="action-bar" />
      <StackLayout backgroundColor="#3c495e">

      </StackLayout>
    </Page>
  `,

}).$start();

First, let’s add a <WebView />. This WebView view is like an <iframe> in the front-end web developer world; it’s a window into a website.

<WebView height="1600px" src="http://bleedingedgepress.com/" />
`

app.js

const Vue = require("nativescript-vue");

new Vue({
  template: `
    <Page class="page">
      <ActionBar title="NativeScript Demo App" class="action-bar" />
      <StackLayout backgroundColor="#3c495e">
        <WebView height="1600px" src="http://bleedingedgepress.com/" />
      </StackLayout>
    </Page>
  `,
}).$start();

You should see the mobile version of the Bleeding Edge Press website on your phone...in a native app in Vue.js! Let’s add to this. Let’s add a heading and a button that performs an action. In app.js write a heading. Since we are not worried about web standards or SEO, we can get away with a few things. In the traditional sense, this would be a <h1> however, this is a <Label />. In the StackLayout add the following.

<Label text="Bleeding EdgePress" class="heading" />

You’ll notice that there is a class associated with this Label because you can use CSS in NativeScript! In the playground, all of the CSS for this app is in the app.css file. Open that up and add the following:

.heading {
  padding: 50px 30px;
  color: white;
  font-size: 25px;
}

Let’s move on and add a <Button /> next. Under the WebView, add the following:

<Button text="Follow Bleeding Edge Press" />

Bleeding Edge example in NativeScript

This is going to render a button on the screen. This button won’t do anything until you give it an action or a function to run when it’s pressed. This is going to be a follow button so you can visit Bleeding Edge Press on Twitter. For now, add the @tap event with a string value equal to onButtonTap.

<Button text="Follow Bleeding Edge Press" @tap="onButtonTap" />

Up in the Vue Instance, add a function in the methods property.

new Vue({
  methods: {
    onButtonTap() {
      alert('Follow @edgepress on Twitter!);
    },
  },
  template: `
    <Page>
      <ActionBar title="NativeScript Demo App" class="action-bar" />
      <StackLayout backgroundColor="#3c495e">
        <Label text="Bleeding EdgePress" class="heading" />
        <WebView height="1600px" src="https://bleedingedgepress.com/" />
        <Button text="Follow Bleeding Edge Press" @tap="onButtonTap" class="follow" />
      </StackLayout>
    </Page>
  `,
}).$start();

When tapped, a native alert will be trigger with the text, “Follow @edgepress on Twitter!” But we can make this a little more functional. We want to open up a URL when the user taps this button. To do that, you need to import a package called tns-core-modules/utils/utils. Above the Vue Instance, add const below the Vue library.

const utilsModule = require("tns-core-modules/utils/utils");

In the onButtonTap method, reference the imported library and pass in a URL for the button to open.

utilsModule.openUrl("https://twitter.com/edgepress")

Your app.js should look something like this:

const utilsModule = require("tns-core-modules/utils/utils");

new Vue({
  methods: {
    onButtonTap() {
      utilsModule.openUrl("https://twitter.com/edgepress")
    },
  },
  template: `
    <Page>
      <ActionBar title="NativeScript Demo App" class="action-bar" />
      <StackLayout backgroundColor="#3c495e">
        <Label text="Bleeding EdgePress" class="heading" />
        <WebView height="1600px" src="https://bleedingedgepress.com/" />
        <Button text="Follow Bleeding Edge Press" @tap="onButtonTap" />
      </StackLayout>
    </Page>
  `,
}).$start();

After the Playgrounds refreshes, tap on the button. If you have the Twitter app already installed on your phone, the @edgepress Twitter page will show up in the app. If not, the URL will open in your default browser. If you haven’t already noticed, you can drag elements from the bottom right into the editor to generate the coded needed for those elements. It’s very Xcode-esque.

Conclusion

This was a very quick look at NativeScript for Vue, but I hope you can see just how influential this can be to the Vue.js community. If you have had experience with native mobile app development before, some of these concepts may look familiar to you. These components can be Label and ScrollView, as they directly correspond to native components like UILabel, UIScrollView for iOS and Label and ScrollView for Android.

NativeScript is very different, yet very familiar at the same time. The benefit of NativeScript is that you can develop real native mobile applications with existing front-end technologies like Vue.js or Angular. To deploy these applications, you will need to build them with their respective IDE’s and submit them to their respective App Stores. Native mobile development is an exciting and rewarding experience that has just made its way to Vue.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.220.136.165