I am working on progressive web app with ionic 3 and angular 6 framework.
Every thing works fine but when I press my browser back button or mobile back button my application get closed.
I tried to search how to do this, I am able to disable browser back button with java script but on mobile app it's not working.
<script>
// window.onbeforeunload = function() { return "Your work will be lost.";
};
$ionicPlatform.registerBackButtonAction(function (event) {
event.preventDefault();
}, 100);
</script>
I tried to register the back button event and lot's more but none of that is worked fine for me.
Can anyone have same issue then please tell me how to resolve this.
Thanks,
Dattatray
You can use Ionic's Platform to override the physical back button on Android.
Add it into app.component.ts file as such :
import { Platform } from 'ionic-angular';
constructor(public plt: Platform) {}
platform.registerBackButtonAction(() => {
//Add your code here
},100);
Ionic Platform Service.
Related
I've tried the two following approaches to close my ionic app but non has worked.
1.
closeApp(){
this.platform.backButton.subscribeWithPriority(999999, () => {
navigator['app'].exitApp();
})
}
In this approach, when I click on the back button, the app is disappeared, but it's in the list of the background running apps. This doesn't help me, because I need the app to be completely closed and the user be forced to reopen the app by clicking on its icon.
2.
closeApp(){
const { App } = Plugins;
App['exitApp'];
}
in this case, nothing happens, i.e., I stay on the page.
Is there any other approach or plugin which I can use?
I am working with capacitor: 3 and testing on android 12.
import { App } from '#capacitor/app';
ExitApp() {
App.exitApp();
}
try this
How to close the mobile app programmatically using IONIC 5? Use case: Upon app start, I am displaying a Terms of Use popover and if the user does not accept, I need to close the app. I am using IONIC5+Angular.
Thank you.
V
I figured it out.
While navigator['app'].exitApp() works for Android, it does not work for iOS. Also, according to Apple, the apps should not terminate themselves. For my app, I will just display the "Accept" but. The user will need to close the app manually if they don't want to agree to the Terms of Use.
navigator['app'].exitApp();
**that's work for me **
you also use
#capacitor/app
App.exitApp();
Force exit the app. This should only be used in conjunction with the backButton handler for Android to exit the app when navigation is complete.
Ionic handles this itself so you shouldn’t need to call this if using Ionic.
HTML :-
<ion-button color="danger" (click)="close()" fill="clear" expand="block">Close App </ion-button>
TS :-
import { Platform } from '#ionic/angular';
constructor(private platform: Platform) { }
close(){
this.platform.backButton.subscribe( () => {
navigator['app'].exitApp();
})
}
I am using a website in the ionic-5 web-view app. When I go back to the previous page in the Ionic app, Android device back button is not responding. I am unable to go back to previous page in the Ionic app. Ionic web-view app events are not in sync with Android device back button. Please help me to solve this issue.
You can handle hardware back button using platform
import { Platform } from '#ionic/angular';
this.platform.backButton.subscribeWithPriority(10, () => {
// route to previous page or component
});
I am using the new webcomponent <pwa-install >Install </pwa-install> to install my PWA app build using PWA Builder. The button works well but the only problem I have is that I cant make it to disappear if the app has already been installed.
I have tried to hide the install button and only show it when the beforeinstallprompt is activate but I failed with that:
self.addEventListener('beforeinstallprompt', (e) => {
e.preventDefault();
this.deferredPrompt = e;
$("#installbanner").show();
});
I also tried the appinstalled method, that did not work as well.
self.addEventListener('appinstalled', (evt) => {
app.logEvent("App is installed");
$("#installbanner").show();
});
I also tried displaying all the available methods that are contained within the <pwa-install >Install </pwa-install> but it just showed jibrish.
I am really sorry to bug everyone, may you please kindly assist
you can try using media display query, for example you can try one of the following properties:
#media all and (display-mode: standalone) {
pwa-install{
display: none;
opacity: 0;
visibility: hidden;
}
}
I have a use case where I need to detect current view being shown to the user to report to Google Analytics. I am aware of Ionic 2's View Lifecycle hooks but that is not what I am after.
What would like to know if there is a way to 'hook' into ionic's navigation system to subscribe to navigation events and get route or view object every time user navigates the app. Since Ionic 2 is not using Angular2's Router I can't use following:
export class AppComponent {
private currentRoute:string;
constructor(_router:Router,
_location:Location) {
_router.events.subscribe((event:Event) => {
if (event instanceof NavigationEnd) {
// When the route is '/', location.path actually returns ''.
let newRoute = this._location.path() || '/';
// If the route has changed, send the new route to analytics.
if (this.currentRoute != newRoute) {
ga('send', 'pageview', newRoute);
this.currentRoute = newRoute;
}
}
});
}
}
Does anyone know how to subscribe to navigation changes in Ionic 2 app?
My current setup as of time of wrighting is:
Ionic Framework Version: 2.0.0-rc.4
Ionic CLI Version: 2.1.13
Ionic App Lib Version: 2.1.7
Ionic App Scripts Version: 0.0.48
You can subscribe to App Lifecycle events which are described in the docs
To be more specific, you need to inject App from ionic-angular and then add code like this where needed:
this.app.viewWillEnter.subscribe(viewCtrl => {
console.log('Entering new view')
console.log(viewCtrl)
})