How to close application using button ionic

Hi… How can i close application using button ionic 2.

Try this:

Html:

<button ng-click="exitApp()">Close application<button>

js:

$scope.exitApp = function() {
  ionic.Platform.exitApp();
})
2 Likes

The question was on Ionic 2 so you can’t use $scope

3 Likes

I cant use $scope in Ionic2

1 Like

This is the correct way in ionic 2

Html:

<button (click)="exitApp()">Close application<button>

TS:

import {Platform} from 'ionic-angular';

@Page({ /*...*/ })
export MyPage {
  constructor(platform: Platform) {
    this.platform = platform;
  }
  exitApp(){
     this.platform.exitApp();
  }
}
9 Likes

I just want to add a few notes to the following answer:

  • An additional Cordova plugin might be needed:

navigator.app.exitApp() does not work on iOS, only Android. On iOS, Apple does not allow apps to programmatically exit. It can be done through iOS Objective-C side but there’s a good chance this app will be rejected in Apple app store.

  • Therefore, I would not recommend you to do it and to rethink your UI instead:
3 Likes

Hi Tushar!

This is the way it work for me on Android

Best regards.

navigator[‘app’].exitApp();

import 'es6-shim';
import {Component, Inject, ViewChild} from '@angular/core';
import {Platform, NavController,ionicBootstrap} from 'ionic-angular';
import {StatusBar} from 'ionic-native';
import {TabsPage} from './pages/tabs/tabs';
import {LoginPage} from './pages/login/login';

import {DataService, fb} from './lib/data.service';

@Component({
  template: `
               <ion-nav #rootNavController [root]="rootPage"></ion-nav>`,
  providers:[DataService]
})
export class MyApp {
  @ViewChild('rootNavController') nav:NavController;
  private rootPage:any;

  constructor(public _data:DataService, private platform:Platform) {
    this.rootPage = TabsPage;

    platform.ready().then(() => {
      // Okay, so the platform is ready and our plugins are available.
      // Here you can do any higher level native things you might need.

      StatusBar.styleDefault();

      document.addEventListener('backbutton', () => {
               let activeVC = this.nav.getActive();
               let page = activeVC.instance;

               if (!(page instanceof TabsPage)) {
                 if (!this.nav.canGoBack()) {
                   console.log('Exiting app due to back button press at the root view');
                   return navigator['app'].exitApp();
                 } else if (page instanceof LoginPage) {
                   console.log('Saliendo de la aplicacion para evitar ingresar sin registrarse');
                   return navigator['app'].exitApp();
                 }
                 console.log('Detected a back button press outside of tabs page - popping a view from the root navigation stack');
                 return this.nav.pop();
               }

               let tabs = page.tabs;
               let activeNav = tabs.getSelected();

               if (!activeNav.canGoBack()) {
                 console.log('Exiting app due to back button press at the bottom of current tab\'s navigation stack');
                 return navigator['app'].exitApp();
               }

               console.log('Detected a back button press - popping a view from the current tab\'s navigation stack');
               return activeNav.pop();

      }, false);

    });
  }
}

ionicBootstrap(MyApp)

The plugin describe here, is not longer available, another way to do it on Android?

this.platform.exitApp() still works for me

this.platform.exitApp() doesn’t work :frowning:

What happens? Any errors?

Never mind I got it to work. my apk was not up to date. Thanks @andrewgy8.
BTW Do you know a method to hide the application ?

but not on ios i was unable to do that on ios emulator

this.platform.exitApp(); Is not working anymore since Ionic V4. What is the new way to do this ?

1 Like

@MrWhite : try

navigator[‘app’].exitApp();

4 Likes

Thanks It worked like a charm

Hi this is giving run time error to me in Ionic 4 "core.js:15724 ERROR TypeError: Cannot read property ‘exitApp’ of undefined’. Anything wrong with below code?

ionViewWillLeave() {
alert(‘ionViewWillLeave’);
if (this.commonService.checkLogin) {
if (this.router.url === ‘/app-landing’) {
navigator[‘app’].exitApp();
}
if (this.router.url === ‘/login’) {
return navigator[‘app’].exitApp();
}
}

It is also working. i just tested in my android device.

this.platform.exitApp()

Hope you know:
import { Platform } "ionic-angular";
and all

Have you tested in ionic 4?. Because in ionc 4 this.platform.exitApp() giving below error “Property ‘exitApp’ does not exist on type 'Platform”

Swap with navigator[‘app’].exitApp() as suggested many times before

1 Like