2

We have a mobile application which has been developed using Salesforce Mobile SDK 2.1 and Backbone.JS (Smartstore + smartsync) on iOS platform and we recently, move it to Salesforce Mobile SDK 3.0.

We en-counted some issues, specially with addEventListener event where it didn't work which works fine with SDK 2.1.

document.addEventListener("offline", function () {}

document.addEventListener("online", function () {}

But somehow, once we changed to window from document as below it triggers the listeners but both firing at the same time and when the application comes online it fires twice each

window.addEventListener("offline", function () {}

window.addEventListener("online", function () {}

Offline Tracker Code :

        app.models.OfflineTracker = Backbone.Model.extend({
              initialize: function () { 
                  var that = this;
                  this.set("isOnline", navigator.onLine);
                  //start here
                  document.addEventListener("offline", function () {
                      debugLog("Received OFFLINE event");

                      that.set("isOnline", false);

                      app.router.navigate('#getUserLanguage', true);
                  }, false);
                  document.addEventListener("online", function () {
                      debugLog("Received ONLINE event");

                      that.set("isOnline", true);

                      app.router.navigate('#home', true);

                  }, false);
              }
          });

Is there any solution for this?

  • Do you see this issue on iOS/ Android or both? – Gaurav Kheterpal Feb 23 '15 at 9:51
  • It's a in-house application runs on iOS platform. – Virath Feb 23 '15 at 9:53
  • You definitely shouldn't be using window.addEventListener, I'm curious why you upgraded to 3.0 and not 3.1? Do you mind your sharing your code snippet? – Gaurav Kheterpal Feb 23 '15 at 10:21
  • Well this upgrade was done couple of month earlier before 3.1 launch. I'll update the offline tracker code snippet along with the main post. – Virath Feb 23 '15 at 10:31
1

I presume you're refering to this hybrid example from Mobile SDK. This looks like a scenario where network detection is not working because of a missed plugin/ missing step. If you are upgrading your app from Mobile SDK 2.x to 3.0, you should be following these commands

npm install cordova -g

cordova create TestApp
cd TestApp
cordova plugin add https://github.com/forcedotcom/SalesforceMobileSDK-CordovaPlugin
cordova platform add android
cordova platform add ios
cordova prepare

If this does not help, try moving the listeners to ondeviceready() as shown below

onDeviceReady: function() {

    app.receivedEvent('deviceready');

    document.addEventListener("offline", app.onOffline, false);
    document.addEventListener("online", app.onOnline, false);
},

onOnline:function(){
    console.log("Online");
}
onOffline: function(){
    console.log("Offline");
}
0

Thanks to Akhilesh Gupta: Found the solution for this issue where we need to add the org.apache.cordova.network-information cordova plugin to listen for online/offline events.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.