How handle external api calls when app loads offline?


#1

Hi! Hope i’m not asking any dumb thing, but I’m having real trouble on handling this issue.

I’m using Google Places API in one of my views.

Also I’m handling internet connection detection, so the app can’t be used when there’s no internet.

BUT when I open the app, with no internet, it crashes (blank page) and get the next error:

Uncaught ReferenceError: google is not defined ionic.nightly.bundle.js:

I just need the app not to crash on load, and then I can handle the rest when there is no connection.

Is there a way to avoid external calls when the device loads the app with no internet? Even just for taking the user to another static page.

I’d appreciate any help.
Thanks in advance! This is huge deal and I can’t launch my app until I solve it.


#2

Use the Cordova Network plugin (https://www.npmjs.com/package/cordova-plugin-network-information) to test your connection, Store that in a service that you can reference before making the remote call


#3

Hi Chris, thank you for your answer! I’m afraid i wasn’t clear enough on my question.

But the issue is on the scrip tag to Google Apis, not in the remote call itself. I’m handling the connection with Cordova Network plugin already!

Thank you!!


#4

Oh. You are going to have to manually load that script after you do your network check.

Since you are using Google APIs, I don’t think you can store the js files locally.


#5

Exactly!
I’m going to try that. I was hoping to find a more elegant way, but I asume that is the only way.
Thank you for your time Chris!


#6

Ok, it was a dumb thing, but I will share it just in case anybody else has the same problem.

Just checking if “google” is defined in every place where you use that variable, should work.

It wasn’t so obvious in first place because the error referenced ionic.nightly.bundle,and I thought it was an issue loading the script (that couldn’t be loaded).

  if (typeof(google) !== 'undefined') {
            //magic iniside.
  }

Thank you Chris for yout time and fast help.

Have a great day!