NSUserDefaults

Hi,
Is there a possibility of adding NSUserDefault functionality to Corona? This shall save me from creating my own persistent propertyBag and file read/writes to the documents directory every time I read/write a value. Plus it will be consistent for all projects and users.

I hope I have not been under a rock for so long, that this is already available… :wink:

cheers,

Jayant C Varma [import]uid: 3826 topic_id: 7651 reply_id: 307651[/import]

I have created a class called Property (shall be available soon to download) this class allows for creating properties like NSUserDefaults in Memory and also being able to persist them to the disk.

So the way it will work is

local prop = require("property")  
  
local propertyBag = prop:init()  
  
--Properties created in memory  
propertyBag:setProperty("MyName", "Jayant C Varma")  
propertyBag:setProperty("MyCompany", "OZ Apps")  
propertyBag:setProperty("myURL", "www.oz-apps.com")  
  
--Now let us save this to the disk  
propertyBag:SaveToFile()  
  
--Let us change the URL to ReviewMe  
propertyBag:setProperty ( "myURL", "reviewme.oz-apps.com" )  
  
--NOTE that the url is changed but it is only in memory, not saved to disk as yet  
print ( propertyBag:getProperty ("myURL" )  
  
--Let us revert back to the saved data from Disk  
propertyBag:GetFromDisk()  
  
print ( propertyBag:getProperty ("myURL" )  
  

That is how easy it will be to use, so you can save anything you want. Scores, items, filenames or any data that you can and want to refer to with a name.

I shall release it once I work on it a bit more, I am trying to set the Default values, like in NSUserDefaults, so you might call the getProperty as

[code]
print ( propertyBag:getProperty( “nothing” , “not as yet defined” ) )

–This shall create the property and set the value of nothing to the default value provided and this shall be an optional value
[/code] [import]uid: 3826 topic_id: 7651 reply_id: 27167[/import]

Hi, I have included a Property.lua file on Git at https://github.com/JayantVarma/Rating-Dialog-CoronaSDK, this does more or less all that I’ve mentioned above. It is quite helpful for saving and using KeyValues that you might want in your game and persisted between two classes or forms.

It is a small file and the code is easy to understand, so there is nothing very complicated to explain, however if you still have questions, do feel free to ask.

cheers, [import]uid: 3826 topic_id: 7651 reply_id: 27619[/import]

This is very good and thanks for making the code available. Currently there is no support in Corona for this feature so this is very useful.

Please submit this to the Code Exchange (if you haven’t already). You could do a quick write-up of the code and link to your Github account.

Thanks again.
Tom [import]uid: 7559 topic_id: 7651 reply_id: 27791[/import]

thanks jay this will be very useful [import]uid: 7911 topic_id: 7651 reply_id: 27822[/import]

@jay
does this work in the simulator or on device only

never mind i got it working [import]uid: 7911 topic_id: 7651 reply_id: 29891[/import]

new problem
when i reload the values they load as strings instead of numbers and tonumber() isnt converting them
any ideas

edit:
it seems that it is converting it to a number but i still get an error saying that im trying to compare a string to a number so maybe it a corona bug idk
anyway i found a work around
just add 0 to the value and it works [import]uid: 7911 topic_id: 7651 reply_id: 29901[/import]

would you want to post the additions/changes that you made to the code for others, I can update the code with that and attribute you for those changes. [import]uid: 3826 topic_id: 7651 reply_id: 29906[/import]

i didnt make any changes to your class
what i did was in my app as i loaded the values i added 0 like this

[blockcode]
varScore = propertyBag:getProperty(“varScore”) + 0
[/blockcode] [import]uid: 7911 topic_id: 7651 reply_id: 29908[/import]

Hello, the github link isn’t working. Any chance you could give me a link to your class?
This seems to be what I’m looking for, does it allow you to add a settings tab for your app in the ios settings?
Thanks in advance. [import]uid: 7059 topic_id: 7651 reply_id: 114097[/import]