2

I have some problems in my code. My web application is running on AngularJs 1.5. I have got an internet shop with a shopping cart. The problem is that data which should be saved in sessionStorage and restored after updating the browser is properly working on desktop browsers, but totally not working on iOS (tested Chrome and Safari). Android (Chrome) is working properly, too. While inspecting code in MacOS Safari from iPhone, the console is not throwing any errors. Here is the code:

(function(angular, loc) {
    var app = angular.module('myModule', ['ui.bootstrap','ui.mask'])    

    app.run(function($rootScope) {
       window.onbeforeunload = function(event) {
          $rootScope.$broadcast('savestate');
       };
    });

    app.factory('basket', ['$rootScope', function($rootScope) {

        var basketData = {
            content: []
        };

        function saveState() {
            sessionStorage.basket = angular.toJson(basketData.content);
        }
        function restoreState() {
            basketData.content = angular.fromJson(sessionStorage.basket);
        }
        $rootScope.$on("savestate", saveState);

        if (sessionStorage.basket) restoreState();

        return basketData;

    }]);

    ...

})(angular, window.location);
Vemonus
  • 868
  • 1
  • 16
  • 28
fakie
  • 334
  • 3
  • 12
  • why sessionStorage? session storage will go away, why not use localStorage – 1Mayur Jan 23 '17 at 22:37
  • I have never used storage before. I have just searched for ready to use script. – fakie Jan 23 '17 at 22:44
  • I have changed sessionStorage to localStorage. It works properly. If I close tab and open website in another tab, storage is restored. But iOS still no effect. – fakie Jan 23 '17 at 23:17
  • in ios can you debud and see if the storage has the data present? – 1Mayur Jan 23 '17 at 23:18
  • MacOS Safari localStorage gets basket [...]. If I inspect iOS Safari, localStorage gets nothing. Empty. – fakie Jan 23 '17 at 23:26
  • 1. check if the local storage is available. http://stackoverflow.com/questions/14555347/html5-localstorage-error-with-safari-quota-exceeded-err-dom-exception-22-an – 1Mayur Jan 23 '17 at 23:32
  • 2. if yes then try passing window.localStorage how you are passing window.location – 1Mayur Jan 23 '17 at 23:34
  • 1. localStorage is available. It gets test 1; 2. window.localStorage does not change anything. Any other ideas? – fakie Jan 24 '17 at 00:29
  • Seems that `window.onbeforeunload = function(event) { $rootScope.$broadcast('savestate'); console.log('onbeforeunload') };` is not working in iOS Safari. Nothing shows in console. I have found this: http://stackoverflow.com/questions/3239834/window-onbeforeunload-not-working-on-the-ipad – fakie Jan 24 '17 at 00:41
  • `$(window).on('beforeunload pagehide', function(event) { $rootScope.$broadcast('savestate'); });` found this working in iOS Safari. Tomorrow I will turn this code in native JS from JQuery. Problem is that iOS Safari does not support beforeunload. Pagehide solves this problem. Mayur, big thanks for your help – fakie Jan 24 '17 at 01:00

1 Answers1

4

Problem is that iOS Safari does not support beforeunload. Pagehide solves this problem.

fakie
  • 334
  • 3
  • 12