Custom-URL-scheme icon indicating copy to clipboard operation
Custom-URL-scheme copied to clipboard

A better way to call openHandleURL !!!

Open rolinger opened this issue 3 years ago • 1 comments

the openHandleURL fires way before the app fully initializes EVEN when set in the deviceReady section of app.js or main.js. This is particularly worse when an app launches for the very first time. Example scenario: User installs the app BUT doesn't open it - the app has never initialized on the users phone before. Then the user clicks on an external URL Scheme (or QR Code) that redirects them into the app. This will cause window.handleOpenURL to fire well before the first time app initialization is complete and the redirected URL won't work.

For years, everyone has been using the following:

    window.handleOpenURL = function(url) {
        setTimeout(_ => {
          .... do stuff here
       },0) ;

But even beyond the first time app initialization scenario above, working off of timers can bite you in the ass because sometimes some things take just hair longer than your timeout timer - and then your redirected URL will fail within your app.

To manage this I changed to the following method:

    window.handleOpenURL = function(url) {
      var doURL = $interval(function() { 
        if (getDB("app_initialized") !== null) {    // <----- specific app local storage value is set
          $interval.cancel(doURL) ;
          openURL(url) ;
        }
      },100) ;
      function openURL(url) {
        setTimeout(_ => {
           ... do stuff here
       },0) ;
    }

Change your interval trigger to whatever you want....but this ensures you can control WHEN handleOpenURL will fire. In my case, I set a bunch of localStorage variables upon first time install....app_initialized is the last one to be set. When its set, then my environment is ready to go.

rolinger avatar Aug 06 '21 21:08 rolinger

On which platform did you experience this?

EddyVerbruggen avatar Feb 23 '15 19:02 EddyVerbruggen

I get this warning during launch of my app on iOS (tried iphone 5s and iphone 4): THREAD WARNING: ['Flashlight'] took 36.587158' ms. Plugin should use a background thread.

crissi avatar May 05 '15 11:05 crissi

Same issue here : it happens only on Samsung Galaxy S5 in cordova release mode and the toggle method is called when the user taps a button. The result is : the camera of the device is not working anymore, I have to manually kill the app to access the camera again. Here is the code :

var isOn = false;
....
if (window.plugins && window.plugins.flashlight) {

  window.plugins.flashlight.available(function (isAvailable) {

    if (isAvailable) {

      window.plugins.flashlight.toggle(function () {

          isOn = !isOn;

        });

      }

  });

}

ElodieC avatar Jun 26 '15 17:06 ElodieC

Android takes too long the turn its camera off and on again. I implemented a third parameter to switchOff(null, null, true) to avoid turning the camera off completely and changing only the flashlight parameter.

https://github.com/lucasdupin/Flashlight-PhoneGap-Plugin

lucasdupin avatar Oct 26 '15 21:10 lucasdupin

Interesting addition. You'll only use that if you need to blink it repeatedly, right?

What happens if you use the soft switch off and open the camera app? Does it have trouble accessing the camera?

EddyVerbruggen avatar Oct 26 '15 21:10 EddyVerbruggen

Right, only if you need to blink it repeatedly. You must turn it off with switchOff(); to release the camera before quitting your app/activity. But either way, this is something you are probably already doing.

lucasdupin avatar Oct 26 '15 22:10 lucasdupin

FYI On Android the plugin now executes everything on a background thread (v 3.1.0)

EddyVerbruggen avatar Jun 14 '16 08:06 EddyVerbruggen