Access to NSUserDefaults set in old version of app

Hi everyone,

I have an app which was written in objective-C using Xcode a while back.

I want to do a new version of the app using Corona.

But - all my users currently have their progress / app data stored on their devices in NSUserDefaults.

So - I need a way within Corona to access these NSUserDefaults and retrieve the values so I can import them into the new version of the app’s database. When users install the new Corona version I don’t want them losing any data.

I know the roadmap says “Access to set NSUserDefaults” but I need to GET the old values back, not set them. Is this likely to be included?

Does anyone know if this is going to possible at any point or if there is any way of doing it currently. It’s kind of crucial to my entire development process because if I can’t do this I can’t use Corona realistically for this app or I have to seriously annoy all my users by basically wiping all their data.

Any help / advice on this would be much appreciated.

Thanks,

Ian [import]uid: 123492 topic_id: 33046 reply_id: 333046[/import]

I don’t think we have any access to that yet, I could be wrong though.

It’s not pretty but could you make a transitional update in obj-c that accesses the data and then saves it out into the documents directory and then when you update again with the Corona version you can then access the data fine? [import]uid: 119420 topic_id: 33046 reply_id: 131215[/import]

Hi Glitch Games - thanks for the response.

I quite like this as a solution but of course anyone who hasn’t updated to the transitional version when the main update is released would still lose their data. I’d either have to accept that was the case or delay the main update sufficiently that nearly everyone who was actively using the app would have definitely updated. Obviously I can track numbers of updates so I could ensure the majority had updated but it still could mean quite a delay to my main release.

If anyone from Corona could shed some light on whether a solution that doesn’t require a transitional release may be forthcoming it would be very helpful. I can’t imagine it should be particularly difficult to incorporate into the build but I appreciate I may be wrong on that.

It’s basically going to be the difference for me between either being able to smoothly release an improved version of this app to my customers or seriously annoy a lot of them by losing their data or delaying the release long enough to ensure they’ve all updated via an interim version which does what Glitch Games suggested. Obviously I would love to be able to do the first option and it would save me and my customers a lot of trouble.

Thanks in advance for any help,

Ian [import]uid: 123492 topic_id: 33046 reply_id: 131243[/import]

NSUserDefaults is an XML file(Disguised under the extension .plist) stored in Library/Preferences. You can open it up using a Lua file handle, and get a Lua-based XML parser (http://lua-users.org/wiki/LuaXml) to convert it into a Tables. I don’t recall the actual file name, but you can do:

local lfs = require("lfs")  
local tmpDir = system.pathForFile("", system.CachesDirectory) .. "/../Preferences" --This works since the Caches directory on iOS is Library/Caches  
local iter, dir = lfs.dir(tmpDir)  
while true do  
 local file = iter(dir)  
 if not file then --Done Iterating  
 break  
 end  
 print("FILENAME", file)  
end  

This snippet should print out all the names of the files in that folder. One of them is going to be your XML User defaults. The next step would be to load files with the extension .plist as xml files and test out which ones contain the data you expect them to. [import]uid: 134101 topic_id: 33046 reply_id: 131246[/import]

I don’t think we have any access to that yet, I could be wrong though.

It’s not pretty but could you make a transitional update in obj-c that accesses the data and then saves it out into the documents directory and then when you update again with the Corona version you can then access the data fine? [import]uid: 119420 topic_id: 33046 reply_id: 131215[/import]

Hi Glitch Games - thanks for the response.

I quite like this as a solution but of course anyone who hasn’t updated to the transitional version when the main update is released would still lose their data. I’d either have to accept that was the case or delay the main update sufficiently that nearly everyone who was actively using the app would have definitely updated. Obviously I can track numbers of updates so I could ensure the majority had updated but it still could mean quite a delay to my main release.

If anyone from Corona could shed some light on whether a solution that doesn’t require a transitional release may be forthcoming it would be very helpful. I can’t imagine it should be particularly difficult to incorporate into the build but I appreciate I may be wrong on that.

It’s basically going to be the difference for me between either being able to smoothly release an improved version of this app to my customers or seriously annoy a lot of them by losing their data or delaying the release long enough to ensure they’ve all updated via an interim version which does what Glitch Games suggested. Obviously I would love to be able to do the first option and it would save me and my customers a lot of trouble.

Thanks in advance for any help,

Ian [import]uid: 123492 topic_id: 33046 reply_id: 131243[/import]

NSUserDefaults is an XML file(Disguised under the extension .plist) stored in Library/Preferences. You can open it up using a Lua file handle, and get a Lua-based XML parser (http://lua-users.org/wiki/LuaXml) to convert it into a Tables. I don’t recall the actual file name, but you can do:

local lfs = require("lfs")  
local tmpDir = system.pathForFile("", system.CachesDirectory) .. "/../Preferences" --This works since the Caches directory on iOS is Library/Caches  
local iter, dir = lfs.dir(tmpDir)  
while true do  
 local file = iter(dir)  
 if not file then --Done Iterating  
 break  
 end  
 print("FILENAME", file)  
end  

This snippet should print out all the names of the files in that folder. One of them is going to be your XML User defaults. The next step would be to load files with the extension .plist as xml files and test out which ones contain the data you expect them to. [import]uid: 134101 topic_id: 33046 reply_id: 131246[/import]

Thanks for the reply - I’ll try this out.

I briefly tried the code in the simulator but it caused an error because that folder doesn’t exist on my Mac so I’ll try running it on a device with a version of my app on already.

Corona team - it would be awesome if you could include a way to directly access NSUserDefaults via Corona. If anyone could write back and let us know whether this is something you’re planning on that would be great.

Thanks,
Ian [import]uid: 123492 topic_id: 33046 reply_id: 131549[/import]

Thanks for the reply - I’ll try this out.

I briefly tried the code in the simulator but it caused an error because that folder doesn’t exist on my Mac so I’ll try running it on a device with a version of my app on already.

Corona team - it would be awesome if you could include a way to directly access NSUserDefaults via Corona. If anyone could write back and let us know whether this is something you’re planning on that would be great.

Thanks,
Ian [import]uid: 123492 topic_id: 33046 reply_id: 131549[/import]