Skip to main content
Participant
December 2, 2012
Answered

sharedObject not working on iphone?

  • December 2, 2012
  • 7 replies
  • 1183 views

I've got a shared object that for some reason is working perfectly fine on the desktop but won't work on an iphone, the data is just not saving/loading. Does anyone have any ideas as to what the issue could be? Publishing with air 3.4.0.2710 to an iphone 5 running the latest version of iOS 6 if this makes a difference? Some code samples below...

The code I've got for saving is:

so = SharedObject.getLocal("pGame");

so.data['levelData'] = levelStats

so.data['numCoins'] = totalCoins

so.flush();

And on load it just reads the data back as below:

so = SharedObject.getLocal("pGame");

levelStats = so.data['levelData']

totalCoins = so.data['numCoins']

I'm saving the state on exit, and also on major events. First time I ran this code on the phone the app just froze because it couldn't find the shared object at all. When I wrap the shared object in an if statement to check that it exists before attempting to load data, it runs on the phone but no data loads. Any ideas?

This topic has been closed for replies.
Correct answer Mark.fromOP

So I tried creating a blank file from scratch to get a clean testing environment. I put a text area input called "txtBox" on stage with the following code:

NativeApplication.nativeApplication.addEventListener(Event.EXITING, onExit)

var so:SharedObject = SharedObject.getLocal("testSO");

if(so){

    if(so.data.textData){

        txtBox.text = so.data.textData

    }

}

function onExit(evt:Event):void{

so.data.textData = txtBox.text

so.flush();

}

Same results. Works on desktop, does not work on phone. What am I doing wrong???


Perhaps the EXITING listener is not firing properly on your deivce. Add a manual button that saves to the SO, then close the app and relaunch it should now load from the SO.

Also the way you are checking for the SO is concerning to me, I am sure its fine but when I was researching this everyone seems to write it the way I posted above.

var so:SharedObject = SharedObject.getLocal("testSO"");

txtBox.text = so.data..textData;

if (! so.data..textData)

{

    txtBox.text = "no data found";

}

Also I am not sure its a good idea to load text directly from the SO into the textbox you might want to pass it through a variable

7 replies

Mark.fromOP
Inspiring
December 2, 2012

I am sure there is a reason for the "['levelData']" but try getting rid of it.

try this.

//only call the shared object instance once when you first launch the app then load your stuff, use the if function to see if a value exists otherwise assign your variable a value

var so:SharedObject = SharedObject.getLocal("pGame"");

//load your stuff

levelStats = so.data.levelStats;

if (! so.data.levelStats)

{

    levelStats = 0;

}

numCoins = so.data.numCoins;

if (! so.data.numCoins)

{

    numCoins = 0;

}

//then save as  you see fit

so.data.levelStats = levelStats;

so.data.numCoins = totalCoins;

so.flush();

I use shared object extensively for all my data needs and they work flawlessly on all version of air thus far.

Looks like you might be calling the SO class more than once which cause it to crash. Surprised you are not getting an error on the flash simulator.

green_pezAuthor
Participant
December 3, 2012

Thanks for the reply Mark, I tried both your tips (calling the shared object once, and getting rid of the ['variable names'] kind of referencing) with no result

Is it possible that this is a localised issue with the new iphone model / OS version, or is it most likely something wrong with my code? Just don't understand why it works 100% without fail on the desktop but not on the phone.

Another thing, the levelStats variable is an array - this shouldn't break things should it? Or can you only store simple numbers/strings in an SO?

Mark.fromOP
Inspiring
December 3, 2012

This might be the reason, I have never tried storing an array in SO I tend to split up my arrays and store each variable separately, its probably not the easiest way to do it but it works.

I can confirm that SO work on iOS5 and iOS6 and on the iPhones 3gs through 5 as well as all iPads from gen one to gen 3 and I assume the mini and gen 4.

Try changing your arrays to just one element and see if it works. Or lets google.

https://www.mochimedia.com/community/forum/topic/sharedobject-and-arrays-as3