Downloading zipped folders to your IOS app.

Getting into LiveCode for iOS? Ask your questions here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
FireWorx
Posts: 362
Joined: Wed Sep 07, 2011 9:39 pm

Downloading zipped folders to your IOS app.

Post by FireWorx » Wed Jul 16, 2014 6:39 pm

In the interest of sharing information here are my scripts for checking to see if there are updated files on your web server server and if so downloading them to your IOS app. Hope this helps someone.

When the app opens it checks to see if there is a file "Update.txt" in the docs folder. If not (like the first time it launches) it downloads the zipped folders from the server, un zips them, and places them for use within the IOS app. I also download the latest update file and store it. A progress bar activates to let the users know the files are downloading. If the net connection drops off the procedure is aborted until next time. If the check for update button is pressed the app checks the update file on the server (a text file with a line for each zipped folder on the server with a time stamp of when it was last updated) and compares it line by line to the one in the IOS app. If one of the folders on the server is newer than the one on the IOS app it downloads that folder and replaces the update file with the latest one. Here is the code with my server name redacted.
If anyone can improve on this then please do so and re-post.

PS: The "LoadProtocols" is for a full update of all folders (like the first time the app launches)
The "doTheUpdate" checks to see it there are updates available and only downloads folders that are new.
---------------------Update Scripts ------------------
on doTheUpdate
if the environment is not "mobile" then exit doTheUpdate
put empty into tAgencyItems ## contains an entire list of zipped folder names on the server
libUrlDownloadToFile "http://www.yourservername.on-rev.com/Pe ... folderpath ("documents") & "/LastUpdate2.txt"
put specialfolderpath ("documents") & "/LastUpdate.txt" into tPath ## download the compare file
if there is a file tPath then
open file tPath
read from file tPath until EOF
close file tPath
put it into tPrefs
else ## not yet saved to docs folder so go to card 1. Card script should try and do a full update.
answer "Please relaunch the Pedi Fast Pack application with adequate wi fi to download the files."
clearTheUpdateFile
exit doTheUpdate
end if
-------------------- compare line by line file on IOS vs file on the server
put specialfolderpath ("documents") & "/LastUpdate2.txt" into tPath
if there is a file tPath then
open file tPath
read from file tPath until EOF
close file tPath
put it into tPrefs2
if tPrefs2 is empty then
answer "Could not evaluate the update status. Please check your internet connection."
clearTheUpdateFile
exit doTheUpdate
end if
else
answer "Could not locate the server file neccesary to do the update."
clearTheUpdateFile
exit doTheUpdate
end if
--------ok now compare line by line tPrefs2 to tPrefs1
repeat with x = 1 to the number of lines of tPrefs
put item 2 to 3 of line x of tPrefs into tCurrent
convert tCurrent to seconds
put item 2 to 3 of line x of tPrefs2 into tLatest
convert tLatest to seconds
if tLatest >tCurrent then
put item 1 of line x of tPrefs & "," after tAgencyItems
end if
end repeat
if last char of tAgencyItems is "," then delete last char of tAgencyItems
if tAgencyItems is empty then
answer "Your files apear to be up to date. Would you like me to force an update?" with "Force Update" or "No"
if it is "Force Update" then
go to card 1
LoadProtocols
exit doTheUpdate
else
clearTheUpdateFile
exit doTheUpdate
end if
end if
if tAgencyItems is not empty then -- holds just the folders that need to be updated
go to card 1
downloadFiles
downloadTheUpdateLogFile
end if
if it is "Not Now" then
--clearTheUpdateFile
end if
end doTheUpdate

on clearTheUpdateFile
put specialfolderpath ("documents") & "/LastUpdate2.txt" into OldFilePath
delete file OldFilePath ## there was no update to do so delete the compare file from the net
end clearTheUpdateFile

on LoadProtocols
go to card 1 ## don't get confused here the zipped folders are named Card_1, Card_2 etc... nothing to do with LC Cards !!!
put "Card_1,Card_2,Card_3,Card_4,Card_5,Card_6,Card_7,Card_8,Card_9,Card_10,Card_11,Card_12,Card_13,Card_14,Card_15,Index_TextFiles" into tAgencyItems
downloadFiles
downloadTheUpdateLogFile
end LoadProtocols

on downloadFiles
CheckTheNet
wait 40 with messages
if gNetStatus is "True" then ## The net is up and the server accessible according to the stack script
show group "MyProgress" of group "landscape"
hide field "directions" of card 1
show group "MyProgress" ## show the progress bar and download the zipped map folders
repeat with x = 1 to the number of items of tAgencyItems
showProgressLoop
show field "download status" of card 1
put item x of tAgencyItems into tAgency
put "Downloading" && tAgency && " Protocols. Please wait..." into field "download status" of card 1
libUrlDownloadToFile "http://www.yourservername.on-rev.com/Pedi/EMSIA/" & tAgency & ".zip", specialfolderpath ("documents") & "/" & tAgency & x & ".zip"
end repeat
showProgressLoop
hide group "MyProgress"
send unZip to me in 0 milliseconds ## script located in the stack script
hide field "download status" of card 1
show field "directions" of card 1
answer "The file update apears to have went smoothly."
else
answer "You will need internet access to download those files. We will try again next time you launch the program."
end if
end downloadFiles

command unZip --un zipps the folders and files to the documents folder
repeat with x = 1 to the number of items of tAgencyItems
put item x of tAgencyItems into tAgency
put specialFolderPath("documents") & "/" & tAgency & x & ".zip" into TempFile
put specialFolderPath("documents") & "/" into TargetFile
revZipOpenArchive TempFile, "read"
put RevZipEnumerateItems(TempFile) into tList

repeat for each line i in tList
if i = empty then
next repeat
end if
--HERE IS THE FOLDER CODE
if the last char of i ="/" then
create folder (TargetFile & i)
next repeat
end if
--END
revZipExtractItemToFile TempFile, i, (TargetFile & i)
end repeat
revZipCloseArchive TempFile
delete file TempFile
put specialFolderPath("documents") & "/" & tAgency into tMyFile
iphoneSetDoNotBackupFile tMyFile, true -- set the do not back up attribute to the folder
end repeat
end unZip

on downloadTheUpdateLogFile ## after the download load the latest updatefile from the server to the iPad
libUrlDownloadToFile "http://www.yourservername.on-rev.com/Pe ... pdate2.txt", specialfolderpath ("documents") & "/LastUpdate2.txt"
put specialfolderpath ("documents") & "/LastUpdate2.txt" into OldFilePath
put specialfolderpath ("documents") & "/LastUpdate.txt" into NewFilePath
put URL ("binfile:/" & OldFilePath) into URL ("binfile:/" & NewFilePath)
delete file OldFilePath
end downloadTheUpdateLogFile
------------------------------Progress Bar and Internet checking below -----
on ShowProgress pEndp, pPos
set the tmEndValue of control "myProgress" to pEndp
  set the tmThumbPos of control "myProgress" to pPos
wait 0 millisecs with messages
end ShowProgress

on showProgressLoop ## just some quick progress activity if there is no repeat loop to jump into
repeat 1 ## testing to see if repeating twice is uneccessary
put 50 into pEndp
repeat with x = 1 to 50
put x into pPos
ShowProgress pEndp, pPos
end repeat
end repeat
end showProgressLoop

on CheckTheNet
put "http://www.yourservername.on-rev.com" into tMyServer
iphoneSetReachabilityTarget tMyServer, reachabilityChanged
end CheckTheNet

on reachabilityChanged tMyServer, tReach
if "connection required" is among the items of tReach then put "False" into gNetStatus
if tReach is "reachable" then put "True" into gNetStatus
if tReach is empty then put "True" into gNetStatus
end reachabilityChanged
on urlProgress pUrl, pMessage,pbytesR,pbytesT
   if pMessage contains "loading" then
      set the TmThumbPos of grp "MyProgress" of card 1 to ((pbytesR/pbytesT)*100)
end if
 if pMessage contains "error" then
put specialfolderpath ("documents") & "/LastUpdate.txt" into NewFilePath
set the lockMessages to true
answer "At least one of the files failed to download properly Indicating an Internet failure or a file request failure" with "Ok"
hide group "My Progress"
hide field "download status" of card 1
exit to top
end if
end urlProgress

FireWorx
Posts: 362
Joined: Wed Sep 07, 2011 9:39 pm

Re: Downloading zipped folders to your IOS app.

Post by FireWorx » Sat Jan 03, 2015 4:25 am

PS. This script above works in LiveCode 6.7 but fails in LC 7.0 & 7.0.1. In 7.0 it fails because the libUrlDownloadToFile does not work. In LiveCode 7.0.1 it fails because revZipExtractItemToFile TempFile command doesn't work.
Dave

Post Reply