1

I'm coding an Android app with Angularjs and cordova, but I have a problem: For an oauth call I need to open a new window, go to the withings site and when I come on my callback url close the window.

I have tried to use setInterval (or $interval) to check every 2 seconds if I reveived the infos by a call to my API. It works fine on a computer in chrome but when I compile the app with Cordova I never enter in the loop.

So I tried to catch an event or $watch on the url of my new window but I execute only once my function at the first url change even if my call to the API result an error.

This is my last code but I tried a lot of different tests :

var newWindow = $window.open(result.res);
var unwatch = $scope.$watch(window.location, function() {
    $http.get(url)
    .success(function(result) {
        newWindow.close();
        unwatch();
        $location.path('/tab/dash');
    })
    .error(function(error) {
        console.log(error);
    });
});

If you have any idea to make this work, would be very happy.

Thanks by advance !

theonlygusti
  • 11,032
  • 11
  • 64
  • 119
Jowy
  • 93
  • 2
  • 9
  • instead of watch you can listen `$locationChangeStart` or $locationChangeSuccess` – Pankaj Parkar Feb 26 '15 at 20:48
  • I also tried this but that's not working. – Jowy Feb 26 '15 at 20:50
  • take a look at this http://stackoverflow.com/questions/19974097/angularjs-and-phonegap-location-path-causes-subsequent-tempateurl-lookup-to-fa – Pankaj Parkar Feb 26 '15 at 20:52
  • I think it can't apply to my app. – Jowy Feb 26 '15 at 21:02
  • your app does have html5mode enable? – Pankaj Parkar Feb 26 '15 at 21:04
  • yes I have $locationProvider.html5Mode(true); and in my index.html. But even in my laptop browser $locationChangeStart doesn't work. – Jowy Feb 26 '15 at 21:13
  • try setting `true` option inside you $watch to watch object deep copy OR use `$window.on('hashchange', function() { //.. work .. });` don't forget to add $window dependency – Pankaj Parkar Feb 27 '15 at 08:55
  • I tested exactly what you say for html5mode, and for you last comment but nothing work... I also try to make a loop function who work fine in chrome but only exec once on android... `var loopVerifWithings = function() { $http.get(url) .success(function(result) { console.log("loop"); }) .error(function(error) { console.log(error); loopVerifWithings(); }); };` – Jowy Feb 27 '15 at 10:45

1 Answers1

0

Finally I found a solution:

I install the cordova plugin inAppBrowser and use this code

var windowWithings = $window.open(result.res, '_blank', 'location=no');
    windowWithings.addEventListener('loadstart', function(event) {
        $http.get(url)
        .success(function(result) {
            windowWithings.close();
            $location.path('/tab/dash');
        })
        .error(function(error) {
            console.log(error);
        });
    });

Be careful if you don't add 'location=no' in open it won't work.

Jowy
  • 93
  • 2
  • 9
  • Hello @Jowy . i am having similar type of case , but what i want is to close window automatically at some specific url and get back to application screen based on success or failure, so is there any way to achieve this. – sid Dec 22 '15 at 12:52