diff --git a/application-management.md b/application-management.md index 456c2a324..0e0a3c46e 100644 --- a/application-management.md +++ b/application-management.md @@ -1,55 +1,80 @@ --- -nav-title: "Application Management" -title: "Application Management" -description: "Application Management" +nav-title: Application Management +title: Application Management +description: Learn how to manage the life cycle of NativeScript applications from application start to storing user-defined settings. position: 4 - --- -# Application Start -You are required to call the **start** method of the application module once you are ready with its initialization. This method doesn't do anything for an Android application at the moment but may do in the future. In an iOS application this call will start the UIApplication and will trigger its UI message loop.: + +# Application Management + +* [Start Application](#start-application) +* [Use Application Callbacks](#use-application-callbacks) +* [Persist and Restore Application Settings](#persist-and-restore-application-settings) + +## Start Application + +You must call the **start** method of the application module after the module initialization. + +This method is required for iOS application. + +### Example + ``` JavaScript +/* +iOS calls UIApplication and triggers the application main event loop. +*/ + var application = require("application"); application.mainModule = "app/template-settings/main-page"; application.start(); ``` ``` TypeScript +/* +iOS calls UIApplication and triggers the application main event loop. +*/ + import application = require("application"); application.mainModule = "app/main-page"; application.start(); ``` -# Using Application Callbacks -Each NativeScript application has several important lifecycle events. You can use those events to perform all kinds of needed maintenance and housekeeping: -+ onLaunch(context) - method called when application launch. -+ onSuspend() - method called when the application is suspended. -+ onResume() - method called when the application is resumed after it has been suspended. -+ onExit() - method called when the application is about to exit. -+ onLowMemory() - method called when there is low memory on the target device. -+ onUncaughtError(error) - method called when there is an uncaught application error. -Here is a code example that demonstrates how too use those callback functions: + +## Use Application Callbacks + +NativeScript applications have the following life cycle events. + ++ `onLaunch(context)`: This method is called when application launch. ++ `onSuspend()`: This method is called when the application is suspended. ++ `onResume()`: This method is called when the application is resumed after it has been suspended. ++ `onExit()`: This method is called when the application is about to exit. ++ `onLowMemory()`: This method is called when the memory on the target device is low. ++ `onUncaughtError(error)`: This method is called when an uncaught application error is present. + +### Example + ``` JavaScript var application = require("application"); application.mainModule = "app/template-settings/main-page"; application.onLaunch = function (context) { - // For Android applications the context is an android.content.Intent. - // For iOS applications the context is undefined, i.e. there is no available context. + // For Android applications, the context is an android.content.Intent class. + // For iOS applications, the context is undefined. if (application.android) { - console.log("Launched Android application with intent: " + context); + console.log("Launched Android application with the following intent: " + context + "."); } else if (application.ios) { - console.log("Launched iOS application"); + console.log("Launched iOS application."); } }; application.onSuspend = function () { - console.log("Application suspended"); + console.log("Application suspended."); }; application.onResume = function () { - console.log("Application resumed"); + console.log("Application resumed."); }; application.onExit = function () { - console.log("Application exit"); + console.log("Application will exit."); }; application.onLowMemory = function () { - console.log("Application low memory"); + console.log("Memory is low."); }; application.onUncaughtError = function (error) { console.log("Application error: " + error.name + "; " + error.message + "; " + error.nativeError); @@ -60,63 +85,71 @@ application.start(); import application = require("application"); application.mainModule = "app/main-page"; application.onLaunch = function (context: any) { - // For Android applications the context is an android.content.Intent. - // For iOS applications the context is undefined, i.e. there is no available context. + // For Android applications, the context is an android.content.Intent class. + // For iOS applications, the context is undefined. if (application.android) { - console.log("Launched Android application with intent: " + context); + console.log("Launched Android application with the following intent: " + context + "."); } else if (application.ios) { - console.log("Launched iOS application"); + console.log("Launched iOS application."); } } application.onSuspend = function () { - console.log("Application suspended"); + console.log("Application suspended."); } application.onResume = function () { - console.log("Application resumed"); + console.log("Application resumed."); } application.onExit = function () { - console.log("Application exit"); + console.log("Application will exit."); } application.onLowMemory = function () { - console.log("Application low memory"); + console.log("Memory is low."); } application.onUncaughtError = function (error: application.NativeScriptError) { console.log("Application error: " + error.name + "; " + error.message + "; " + error.nativeError); } application.start(); ``` -# Persist and Restore Application Settings -To persist settings that the user has defined you have to use the local-settings module. The local-settings module is a static singleton hash table that stores key-value pairs for the application. The getter methods have two parameters -- a key and an optional default value to return if the specified key does not exist. The setter methods also have two parameters -- key and value. Here is an example of using the local settings-module and all of its available methods: + +## Persist and Restore Application Settings + +To persist user-defined settings, you need to use the `local-settings` module. The `local-settings` module is a static singleton hash table that stores key-value pairs for the application. + +The getter methods have two parameters: a key and an optional default value to return if the specified key does not exist. +The setter methods have two required parameters: a key and value. + +### Example + ``` JavaScript var localSettings = require("local-settings"); -// Event handler for Page "loaded" event attached in main-page.xml +// Event handler for Page "loaded" event attached in main-page.xml. function pageLoaded(args) { localSettings.setString("Name", "John Doe"); - console.log(localSettings.getString("Name")); // Prints "John Doe" + console.log(localSettings.getString("Name")); // Prints "John Doe". localSettings.setBoolean("Married", false); - console.log(localSettings.getBoolean("Married")); // Prints false + console.log(localSettings.getBoolean("Married")); // Prints false. localSettings.setNumber("Age", 42); - console.log(localSettings.getNumber("Age")); // Prints 42 - console.log(localSettings.hasKey("Name")); // Prints true + console.log(localSettings.getNumber("Age")); // Prints 42. + console.log(localSettings.hasKey("Name")); // Prints true. localSettings.remove("Name"); // Removes the Name entry. - console.log(localSettings.hasKey("Name")); // Prints false + console.log(localSettings.hasKey("Name")); // Prints false. } exports.pageLoaded = pageLoaded; ``` ``` TypeScript import observable = require("data/observable"); import localSettings = require("local-settings"); -// Event handler for Page "loaded" event attached in main-page.xml +// Event handler for Page "loaded" event attached in main-page.xml. export function pageLoaded(args: observable.EventData) { localSettings.setString("Name", "John Doe"); - console.log(localSettings.getString("Name"));// Prints "John Doe" + console.log(localSettings.getString("Name"));// Prints "John Doe". localSettings.setBoolean("Married", false); - console.log(localSettings.getBoolean("Married"));// Prints false + console.log(localSettings.getBoolean("Married"));// Prints false. localSettings.setNumber("Age", 42); - console.log(localSettings.getNumber("Age"));// Prints 42 - console.log(localSettings.hasKey("Name"));// Prints true + console.log(localSettings.getNumber("Age"));// Prints 42. + console.log(localSettings.hasKey("Name"));// Prints true. localSettings.remove("Name");// Removes the Name entry. - console.log(localSettings.hasKey("Name"));// Prints false + console.log(localSettings.hasKey("Name"));// Prints false. } -``` +``` \ No newline at end of file diff --git a/cli.md b/cli.md deleted file mode 100644 index 2ab691d6e..000000000 --- a/cli.md +++ /dev/null @@ -1,76 +0,0 @@ ---- -nav-title: "NativeScript CLI" -title: "CLI" -description: "NativeScript Documentation: Command-line Interface" -position: 16 ---- - -# Command-line Interface - -Using a command line interface (CLI) is a very productive and simple way to use a developer framework. With the NativeScript CLI, it is possible to IDE to edit the application code and use the CLI to build and deploy the application to an emulator or on a real device. - -One of the goals with NativeScript is having tools that are very similar to existing ones to lower the learning curve. If you are familiar with the [Cordova Command-line interface (Cordova CLI)](http://cordova.apache.org/docs/en/3.6.0/guide_cli_index.md.html), you're ready to build your cross-platform application with the NativeScript CLI. - -#Prerequisites - -It is important to note that NativeScript is all about building applications for devices. Thus, to test and run our product, we need the respective Software Development Kits (SDKs) and build tools installed in accordance to their license terms. For example, on a MAC computer one can install Node.js, Xcode, Java, Android SDK and Apache Ant and then test their NativeScript project on both an iOS simulator and device(s) and Android emulator and device(s). On the other hand, there is no Xcode for Windows PC, hence only Android applications can be tested there, provided we have Java, the Android SDK and Apache Ant. The full requirements can be found in the [System Requirements section of the official NativeScript package readme file](https://www.npmjs.org/package/nativescript#system-requirements). - -#Installing the NativeScript CLI - -The [NativeScript CLI](https://www.npmjs.org/package/nativescript) is distributed as a Node.js NPM package. Thus, its installation is as simple as installing any other NPM package. It is recommended that you install it globally so that you can access it from any folder. - -The command `npm install -g nativescript` will add the `nativescript` (or its synonym, `tns`, used further for shortness) program available to your console. - -#Creating a project using the default template - -Navigate to a preferred 'projects' folder of yours and type `tns create HelloWorld` - -![tns create *nix](img/cli/tns-create-unix.png "tns create *nix") -![tns create windows](img/cli/tns-create-windows.png "tns create windows") - -A new folder will get created, named `HelloWorld`. It contains some sample code, available at [github](https://github.com/NativeScript/template-hello-world). - -![finder app created](img/cli/finder-app-created.png "finder app created") -If you check the folder in Finder or Windows Explorer, you will notice it contains two folders: `app` and `platforms`. - -The latter is empty at this stage of the app creation. Once we start "enabling" our project with specific platforms (iOS and Android supported for the time being), the `platforms` folder will contain platform-specific versions of our project. Please note that this folder is automatically re-generated based on the cross-platform code changes, so please do not alter its content, because it will be replaced next time the app is build. Use this folder for debugging purposes only. - -The app folder contains the actual cross-platform code, the resources of our application and some pre-packed JavaScript modules, representing the NativeScript framework. The most important content in it are the: - -- `app` subfolder, which is our application code. Generally, any edits of the application logic go here. Please, check [the documentation](https://github.com/nativescript/docs) for the available APIs (cross-platform, android and iOS). - -- `App_Resources` subfolder, which contains some platform-specific resources. The structure and the usage of resources is still to be finalized, hence for the time being do not delete, move or rename any files from that folder. Modification of the images to fit your brand is perfectly fine. - -- `tns_modules` subfolder, containing cross-platform modules, pre-packed by Telerik. In the future, the CLI will allow modules to be added/updated via a special command in a plugin manner. - -#"Enabling" the project to a specific platform - -To run the already created project on an emulator or a physical device, first navigate to the project-folder you just created (i.e. `cd HelloWorld`). - -On a mac/windows/linux computer, type the command `tns platform add android`. Alternatively, on a mac computer type the command `tns platform add ios`. - -![tns platform add ios *nix](img/cli/tns-platform-add-ios-unix.png "tns platform add ios *nix") -![tns platform add android windows](img/cli/tns-platform-add-android-windows.png "tns platform add android windows") - -These will download the android or ios runtimes for NativeScript and will unfold them using the contained platform-specific project to the `platforms` folder. A runtime package contains a platform-specific (Eclipse for Android or XCode for iOS) template project and a set of binaries, allowing the NativeScript javascript code to be executed on the target device. - -#Populating the cross-platform code to platform-specific folders - -The next step is running the command `tns prepare android` (alternatively, `tns prepare ios`). - -In essence, this command copies the files in the `app/app`, `app/App_Resources` and `app/tns_modules` folders to the `platforms/[SpecifiedPlatform]` folder, following some logic. - -![tns prepare android windows](img/cli/tns-prepare-android-windows.png "tns prepare android windows") - -The `prepare` command is not run automatically during an `emulate` command call on purpose. The reason is that while debugging we might change the javascript in the platform-specific project (under the `platforms` folder) and we want to debug that code directly by running the emulate or run commands. If the prepare command was called, it would overwrite our changes. - -#Running the application - -- `tns emulate ios` - call this command on a Mac computer to get your project deployed and started on the iOS simulator -- `tns emulate android` - call this command on a Mac/Windows/Linux computer to get your project deployed and started on the Android emulator -- `tns run ios` - call this command on a Mac computer to get your project deployed and started on a device attached to your computer -- `tns run android` - call this command on a Mac/Windows/Linux computer to get your project deployed and started on an Android device, attached to your computer. - -![tns emulate ios *nix](img/cli/tns-emulate-ios-unix.png "tns emulate ios *nix") -![tns emulate android windows](img/cli/tns-emulate-android-windows.png "tns emulate android windows") - diff --git a/getting-started.md b/getting-started.md index 2be24d5de..15ca09062 100644 --- a/getting-started.md +++ b/getting-started.md @@ -1,213 +1,61 @@ --- -nav-title: "Getting Started With NativeScript" -title: "Getting Started With NativeScript" -description: "NativeScript Documentation: Getting Started" +nav-title: Getting Started +title: Getting Started +description: Learn the basics of of how NativeScript work, how to set up your system and how to create your first Hello World app. position: 2 --- -# Getting Started With NativeScript -NativeScript framework enable developers to use pure JavaScript language to build native mobile applications running on the major mobile platforms - Apple iOS, Google Android. The applications UI stack is built using native UI components and because of that no compromises with the User Experience of the applications are done. - -## How It Works -The native script architectures can be generally explained with this diagram: -![architecture diagram]( img/architecture.png "architecture diagram") - -* **Native OS** - At the bottom level is the native OS (Android, iOS and soon Windows). -* **NativeScript runtime** runs the JavaScript code of your application. The runtime also provides a way to call all the native APIs of the platform the app is running on. This means that you have access to all the native capabilities of the platform. -* **NativeScript Modules** are a set of platform-agnostic libraries that are build on top of the runtime. These modules are wrap the platform specific code, providing a common API. -* **Application Code** - your application's code. Building an application on top of the NativeScript modules means that you will not have write platform-specific code. This should be the case most of the time. However, you still have the option to reach the native API trough the NativeSctipt runtime. - -## Requirements -Currently NativeScript can run on the following platforms: - -* Android 4.2+ (equivalent to Android API level 17+) -* iOS 7.1+ - -For NativeScript development you have the following options: - -* Using the [NativeScript Command-Line Interface](https://github.com/NativeScript/nativescript-cli) -with a IDE or text editor of your choice. -* Using [AppBuilder](http://docs.telerik.com/platform/appbuilder/nativescript/index) where you have all the features of [Telerik Platform](http://www.telerik.com/platform) at your disposal. - -## Example -In the following example we will start with a empty NativeScript project and build a simple hello word sample application. - -### Creating Blank Project -Let's start by creating a blank project. As we mentioned you can either use the [NativeScript CLI](https://github.com/NativeScript/nativescript-cli) or NativeScript Blank project template in AppBuilder(available for JavaScript or TypeScript). -Form here on we will be working in the `App` folder inside the project. - -### Adding UI -The project we just created has a single empty page. The UI of the page is defined declaratively in the 'main-page.xml' file. In the project there are also `main-page.js` (or `main-page.ts`) and `main-page.css` files that will hold applications code and styles for this page. - -Let's add some UI in `main-page.xml`: -```XML - - - -