MatiMenich/cordova-plugin-nativeClickSound

Is this plugin working on Ionic 3 project?

Opened this issue · 8 comments

LHLK commented

Hello there,
I installed this plugin on my Ionic 3 project but it didn't work out.

Please advice.
Thanks.
LH

LHLK commented
Lx commented

I can confirm that this plug-in works on an Ionic 3 project.

Be aware that the watch method expects an array of classes. Ionic 3 does not appear to apply a specific class to all Ionic buttons. Therefore, you need to assign your own custom class to the elements that you wish to click when tapped.

I've done similar to this:

nativeclick.watch(['clicky']);
<button ion-button menuToggle class="clicky"> ... </button>

with good results.

Lx commented

I found assigning the clicky class to every necessary component to be excessively tedious, so I now run the following code in my app's root component constructor to target elements by tag name:

constructor(
  platform: Platform,
  statusBar: StatusBar,
  splashScreen: SplashScreen,
) {
  platform.ready().then(() => {
    // ↓↓↓ BEGIN ADDITION
    const nativeClickListener = (event: Event) => {
      // Traverse through the clicked element and all ancestors.
      for (
        let curElement = <Element> event.target;
        curElement != null;
        curElement = curElement.parentElement
      ) {
        // If a BUTTON element is encountered, trigger a click and stop.
        if (curElement.tagName === 'BUTTON') {
          // ‘nativeclick’ doesn't exist outside Cordova's environment.
          typeof nativeclick !== 'undefined' && nativeclick.trigger();
          break;
        }
      }
    };
    // Call the above listener whenever anything is clicked, ensuring that it
    // is called before more specific EventTargets (or else clicks won't be
    // heard on e.g. <ion-datetime> components).
    document.addEventListener('click', nativeClickListener, true);
    // ↑↑↑ END ADDITION
    statusBar.styleDefault();
    splashScreen.hide();
  });
}

It might be appropriate to watch for more tag names. Adjust as necessary.

Lx commented

See this answer on Stack Overflow for more details.

LHLK commented

Hi Lx, It works. Thanks very much !

Not working. I tried all solutions above.

Lx commented

It is working for others. Maybe post your code on Stack Overflow and someone can help you there.