Settings not saving properly

In my app, I allow the user to enter two phone numbers that can be dialed quickly using an autodial feature within the app.

I use native text fields for the items and then at the end, I save the settings.  Only… they aren’t saving.  I don’t understand why.

Here is the code for the settings routine.  The save routine is actually in the globals below.

SETTINGS

--------------------------------------------------------------------------------- -- SCENE NAME -- Scene notes go here --------------------------------------------------------------------------------- local globals = require( "globals" ) local json = require("json") local storyboard = require( "storyboard" ) local scene = storyboard.newScene() local widget = require( "widget" ) -- Clear previous scene storyboard.removeAll() -- local forward references should go here -- local ShowingDoc = false --------------------------------------------------------------------------------- -- BEGINNING OF YOUR IMPLEMENTATION --------------------------------------------------------------------------------- -- Called when the scene's view does not exist: function scene:createScene( event ) local group = self.view -- Background color local bgImage = display.newRect( 0, 0, 380, 570 ) bgImage.strokeWidth = 0 bgImage:setFillColor( 0/255, 84/255, 166/255 ) bgImage:setStrokeColor( 0/255, 84/255, 166/255 ) bgImage.x = display.contentCenterX bgImage.y = display.contentCenterY group:insert(bgImage) -- Header 360 x 50 local bgHeader = display.newRect( 0, 0, 360, 50 ) bgHeader.strokeWidth = 0 bgHeader:setFillColor( 237/255, 237/255, 237/255 ) bgHeader:setStrokeColor( 237/255, 237/255, 237/255 ) bgHeader.x = display.contentCenterX bgHeader.y = 25 group:insert(bgHeader) local topText = display.newText( "Burn Tools", 20, 0, native.systemFontBold, 20 ) topText:setFillColor( 0 ) topText.y = 25 topText.x = display.contentCenterX group:insert(topText) -- back button local backBtn = display.newImage( "back.png" ) backBtn.x = 25 backBtn.y = 25 --backBtn:addEventListener( "tap", function( event ) -- storyboard.gotoScene( "scene\_menu" ) --end ) group:insert( backBtn) -- top Phone button local imgPhone = display.newImage( "callmain.png" ) imgPhone.x = display.contentWidth - 50 imgPhone.y = 25 imgPhone:addEventListener( "tap", function( event ) system.openURL( "tel:610-402-1473" ) end ) group:insert(imgPhone) ----------------------------------------------------- -- -- CONTENT -- ----------------------------------------------------- local Row1 = display.newImageRect( "Hospitalcell.png", display.contentWidth, 55 ) Row1.anchorX = 0 Row1.anchorY = 0 Row1.x = 0 Row1.y = 50 group:insert(Row1) local Row1Text = display.newText( "Quick Call Number", 20, 0, native.systemFontBold, 20 ) Row1Text:setFillColor( 1,1,1 ) Row1Text.anchorX = 0 Row1Text.anchorY = 0 Row1Text.y = 65 Row1Text.x = 10 group:insert(Row1Text) local Row2 = display.newImageRect( "DataEntry.png", display.contentWidth, 40 ) Row2.anchorX = 0 Row2.anchorY = 0 Row2.x = 0 Row2.y = 105 group:insert(Row2) local Row3 = display.newImageRect( "Hospitalcell.png", display.contentWidth, 55 ) Row3.anchorX = 0 Row3.anchorY = 0 Row3.x = 0 Row3.y = 145 group:insert(Row3) local Row3Text = display.newText( "Medical Command Number", 20, 0, native.systemFontBold, 20 ) Row3Text:setFillColor( 1,1,1 ) Row3Text.anchorX = 0 Row3Text.anchorY = 0 Row3Text.y = 165 Row3Text.x = 15 group:insert(Row3Text) local Row4 = display.newImageRect( "DataEntry.png", display.contentWidth, 40 ) Row4.anchorX = 0 Row4.anchorY = 0 Row4.x = 0 Row4.y = 200 group:insert(Row4) local Row5= display.newImageRect( "Hospitalcell.png", display.contentWidth, 55 ) Row5.anchorX = 0 Row5.anchorY = 0 Row5.x = 0 Row5.y =350 group:insert(Row5) local Row5Text = display.newText( "Disclaimer", 20, 0, native.systemFontBold, 20 ) Row5Text:setFillColor( 1,1,1 ) Row5Text.anchorX = 0 Row5Text.anchorY = 0 Row5Text.y = 365 Row5Text.x = 10 group:insert(Row5Text) Row5Arrow = display.newImageRect( "sidearrow.png", 12, 20 ) Row5Arrow.anchorX = 0 Row5Arrow.anchorY = 0 Row5Arrow.x = display.contentWidth - 20 Row5Arrow.y = 372 group:insert(Row5Arrow) Row5:addEventListener( "tap", function( event ) ShowingDoc = true local \_H = 480 local \_W = 320 local SBH = 51 local doc = "HTML/Disclaimer.html" webView = native.newWebView( 0, SBH, \_W, \_H-SBH ) webView.anchorX = 0 webView.anchorY = 0 webView:request( doc, system.ResourceDirectory) end ) -- Create text field Row2TextArea = native.newTextField( 20, 105,280, 36 ) Row2TextArea.anchorX = 0 Row2TextArea.anchorY = 0 Row2TextArea.hasBackground = false Row2TextArea:setTextColor( 1,1,1 ) Row2TextArea.inputType = "phone" Row2TextArea.text = globals.quickCall -- Create text field Row4TextArea = native.newTextField( 20, 200,280, 36 ) Row4TextArea.anchorX = 0 Row4TextArea.anchorY = 0 Row4TextArea.hasBackground = false Row4TextArea:setTextColor( 1,1,1 ) Row4TextArea.inputType = "phone" Row4TextArea.text = globals.MedCommand --Row2TextArea:addEventListener( "userInput", SearchTextAreaListener ) -- Handler that gets notified when the alert closes local function onComplete( event ) if event.action == "clicked" then local i = event.index if i == 1 then -- Save settings elseif i == 2 then -- Open URL if "Learn More" (second button) was clicked --system.openURL( "http://www.coronalabs.com" ) end end end -- Show alert with two buttons --local alert = native.showAlert( "Corona", "Dream. Build. Ship.", { "Yes", "No" }, onComplete ) ------------------------------------------------------ -- -- BACK BUTTON -- ------------------------------------------------------ backBtn:addEventListener( "tap", function( event ) -- Check to see if we're showing a document if (ShowingDoc == false) then local mySettings = {} if Row2TextArea then mySettings.quickCall = Row2TextArea.text else mySettings.quickCall = "6104028000" end if Row4TextArea then mySettings.MedCommand = Row4TextArea.text else mySettings.MedCommand = "6104021473" end print("MedCommand:" ..mySettings.MedCommand) print("QuickCall:"..mySettings.quickCall) saveValue("settings.dat", json.encode(mySettings)) globals.MedCommand = mySettings.MedCommand globals.quickCall = mySettings.quickCall --local alert = native.showAlert( "Corona", "Save Numbers?", { "Yes", "No" }, onComplete ) -- not showing a document so go to menu Row2TextArea:removeSelf() Row2TextArea = nil Row4TextArea:removeSelf() Row4TextArea = nil storyboard.gotoScene( "scene\_menu" ) else -- showing a document so kill it webView:removeSelf() webView = nil ShowingDoc = false Row5Text:toFront() end end ) end -- Called BEFORE scene has moved onscreen: function scene:willEnterScene( event ) local group = self.view end -- Called immediately after scene has moved onscreen: function scene:enterScene( event ) local group = self.view end -- Called when scene is about to move offscreen: function scene:exitScene( event ) local group = self.view end -- Called AFTER scene has finished moving offscreen: function scene:didExitScene( event ) local group = self.view end -- Called prior to the removal of scene's "view" (display view) function scene:destroyScene( event ) local group = self.view end -- Called if/when overlay scene is displayed via storyboard.showOverlay() function scene:overlayBegan( event ) local group = self.view local overlay\_name = event.sceneName -- name of the overlay scene end -- Called if/when overlay scene is hidden/removed via storyboard.hideOverlay() function scene:overlayEnded( event ) local group = self.view local overlay\_name = event.sceneName -- name of the overlay scene end --------------------------------------------------------------------------------- -- END OF YOUR IMPLEMENTATION --------------------------------------------------------------------------------- -- "createScene" event is dispatched if scene's view does not exist scene:addEventListener( "createScene", scene ) -- "willEnterScene" event is dispatched before scene transition begins scene:addEventListener( "willEnterScene", scene ) -- "enterScene" event is dispatched whenever scene transition has finished scene:addEventListener( "enterScene", scene ) -- "exitScene" event is dispatched before next scene's transition begins scene:addEventListener( "exitScene", scene ) -- "didExitScene" event is dispatched after scene has finished transitioning out scene:addEventListener( "didExitScene", scene ) -- "destroyScene" event is dispatched before view is unloaded, which can be -- automatically unloaded in low memory situations, or explicitly via a call to -- storyboard.purgeScene() or storyboard.removeScene(). scene:addEventListener( "destroyScene", scene ) -- "overlayBegan" event is dispatched when an overlay scene is shown scene:addEventListener( "overlayBegan", scene ) -- "overlayEnded" event is dispatched when an overlay scene is hidden/removed scene:addEventListener( "overlayEnded", scene ) --------------------------------------------------------------------------------- return scene

And here is the globals, which is where the save routine is.  I have a print statement, confirming that the save routine is running and I get no error.

-- globals.lua local globals = {} local json = require("json") globals.font = { regular = native.systemFont, bold = native.systemFontBold, } globals.bigScreen = 0 globals.myTotal = 0 globals.document = 0 globals.documentToShow = "none" globals.returnScene = "none" globals.quickCall = "" globals.MedCommand = "" globals.Registered = "" globals.theHospital = "" -- load the settings function loadTable(filename) local path = system.pathForFile( filename, system.DocumentsDirectory) local contents = "" local myTable = {} local file = io.open( path, "r" ) if file then -- read all contents of file into a string local contents = file:read( "\*a" ) myTable = json.decode(contents); io.close( file ) print("File loaded") return myTable end return nil end -- Save specified value to specified encrypted file function saveValue(strFilename, strValue) local theFile = strFilename local theValue = strValue local path = system.pathForFile( theFile, system.DocumentsDirectory ) local file = io.open( path, "w+" ) if file then -- If the file exists, then continue. Another way to read this line is 'if file == true then'. file:write(theValue) -- This line will write the contents of the table to the .json file. io.close(file) -- After we are done with the file, we close the file. print("File written") return true -- If everything was successful, then return true end end local tmpSettings tmpSettings = {} tmpSettings = loadTable("settings.dat") if tmpSettings then print("File found") globals.quickCall = tmpSettings.quickCall globals.MedCommand = tmpSettings.MedCommand else print("File not found") globals.quickCall = "6104028000" globals.MedCommand = "6104021473" globals.Registered = "N" tmpSettings = {} tmpSettings.quickCall = "6105551212" tmpSettings.MedCommand = "6105552121" saveValue("settings.dat", json.encode(tmpSettings)) end return globals

Anyone have any ideas?

So I think I found the problem.  I think it’s the “w+” when using the save.  If I’m correct, that means to APPEND to the file, rather than overwrite it.  I changed that to a “w” and now it looks like it’s saving it.

So I think I found the problem.  I think it’s the “w+” when using the save.  If I’m correct, that means to APPEND to the file, rather than overwrite it.  I changed that to a “w” and now it looks like it’s saving it.