Skip to content

[Capacitorjs | Angular] Back Swipe Support for iOS and Android

Georg Höller
Georg Höller
2 min read
[Capacitorjs | Angular] Back Swipe Support for iOS and Android
Photo by Tyler Lastovich / Unsplash

Recently I recognized that capacitorjs apps does not have a native back swipe support. Little bit weird I thought but the solution is quite simple!

iOS - Native

To enable the iOS back swipe we need to write a capacitor plugin. I tried it without a plugin but inside the AppDelegate - it didn't work.

First add the backswipe folder to a plugins folder, add a new object-c and swift file.

import Foundation
import Capacitor

@objc(BackswipePlugin)
public class BackswipePlugin: CAPPlugin {
   
    override public func load() {
        bridge?.webView?.allowsBackForwardNavigationGestures = true;
    }
}
BackswipePlugin.swift
#import <Foundation/Foundation.h>
#import <Capacitor/Capacitor.h>

CAP_PLUGIN(BackswipePlugin, "BackswipePlugin",)
Backswipe.m

With the object-c file we register the new plugin. As we don't need any externally callable functions we can ignore the third parameter of CAP_PLUGIN.

Inside the swift file we override the load method to use it as a initialize function. To enable the backswipe we have to set allowsBackForwardNavigationGestureson capacitors webview.

Hit run and test it - the iOS simulator supports the back swipe feature!

Android - Angular

For android it is even easier! I added this code to the app.component.tsngOnInit function. You will need the App Capacitor Plugin:

npm install @capacitor/app
  ngOnInit() {
    if (Capacitor.getPlatform() === 'android') registerAndroidListener();
  }

  registerAndroidListern() {
    App.addListener('backButton', (data) => {
      if (data.canGoBack) window.history.back();
      else App.exitApp();
    });
  }

I think this code should be clear ;-)

If you have further questions about this topic or need help with a problem in general, please write a comment or simply contact me at yesreply@georghoeller.dev :)
CapacitorAngular

Comments


Related Posts

Members Public

[Angular | Storybook] Tailwind, Directives, Content Projection, Icons and i18n

Install packages npm i -D @nx/storybook npm i -D @storybook/angular Create config nx g @nx/storybook:configuration <project-name> I created the config inside the main app to share some configs/modules. My stories are located at libs/shared/ui. That's why I had to

[Angular | Storybook] Tailwind, Directives, Content Projection, Icons and i18n
Members Public

[Angular | RxJS] BehaviorSubject with custom states

Whenever I call an api, often my frontend has to go through some different visualization steps. Imagine you have some kind of search input and a list which displays the results but you want to show an loading indicator, an error hint and something different when the result is empty.

[Angular | RxJS] BehaviorSubject with custom states
Members Public

[Angular] Dynamic App Config and Translations in a Submodule

When I'm setting up a new angular application, I always start with translation support and an external app config json loader. You should never start a new app without an internationalization (i18n) system - even if you only support one language! We are going to use the package

[Angular] Dynamic App Config and Translations in a Submodule