hello. need help about txt file downloaded and rewrite in app. I am mad already.

dear Sirs,

as I added some new function in my app , there are more troubles found.


first one is solve. just for share here.
in codea in pad, this not happened. when I test on true phone / iPhone se , I cannot download file as url is blocked which is shown in debug area.

way to solve : in the biggest info.plist, add ket/dict as following :

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSAllowsArbitraryLoads</key>
    <true/>
</dict>

you can get more information on this page :
http://www.timekl.com/blog/2015/08/21/shipping-an-app-with-app-transport-security/


the next has trouble me for 2 days. I did many test. cannot pass.

I hv 1 txt files on internet named ver.txt. there are only 3 numbers in the file. such as 101 .
I hv 1 ver.txt in app and 1 versus.txt in app. same or different number as above.

at beginning of my app, I download the ver.txt on net into local and write it as versub.txt.

when I want to do is: down ver.txt to local and save as versub first, I get the number in versub and compare with the number in ver.txt in local .
in test, the number on web is 108. in local ver txt is 105 , in local versub is 106.

if number is same, the button is gray. we need not download other new files.
if number is diff, the button is active. we can press to download all new files. ( many other txt files. the last one is the ver.txt , down it into local as ver.txt which cover the old , then button could be gray)

the code can work well in codea in pad. but cannot work well in simulated. and true phone.

app can download , open or made new file, write and close file ( as I print something in function) , but finally the number is still the old. it means the versub is not rewrite well.

I also os.remove the versub.txt in local before write new file. remove can work well. I can see this by test.
but after writing the new, the versub.txt still has 106 in file. not 108 .

also I found other txt files cannot be changed once it is already in app(after the first building) . even though I changed text in local file and build app again ( also cleaned) , the text in txt file never changed.

it looks apple has done something for protection. I don’t know more.
so what can I do next? really headache in these days. pls help . thanks a lot.

@dave1707 @Simeon

ps my download versub code :
after url request , then

function downVer(data,status,headers)
  --  print("connected")
      if file_exists(generalPath.."versub.txt") then
          os.remove(generalPath.."versub.txt")
      end
    writeVer("versub.txt",data)
end

function writeVer(fileName,data)

    local file = generalPath..fileName
    wFd, err = io.open(file, "w")
    if wFd == nil then
    error("\
\
Error creating/editing file " .. fileName .. "\
\
Error: " .. err)
    end
    wFd:write(data)
    wFd:close()
   print("downed")
  print(generalPath..fileName)

@totorofat You say the code works well on the iPad, but not on the simulator or phone. I only write code on the iPad and do nothing with XCode for apps, so I can’t give you any suggestions.

@dave1707

sorry that the final “end” is missing. but it cannot work without full code.

so I made a simple edition to show that the txt downed cannot be written
in xcode simulator and true phone. with photo also.

in my following code. the ver.txt on my web address has the number 108.
in local codea, you should made 2 txt files in documents folder.
I put 105 in ver.txt and put 106 in versub.txt. pls don’t forget to have the 2 txts in your project.

in codea in pad, code works well. at first 105/106 could flash then 108 shown.

in xcode, as the photo shot shown, it is always 105/106, even the ver.txt is downed in local.

pls see the 2 orange line parts in right side of photo. I add the 2 lines part in info.plist. then simulator can download. this is for newer’s ref. :smiley:

you can copy paste code to test. it can work well.

thanks dave. you are our god.



-- Ver

-- Use this function to perform your initial setup
function setup()
    
    fontSize(20)
    fill(228, 255, 0, 255)
    textMode(CENTER)
    displayMode(FULLSCREEN)
    
    downing = false
    
    http.request("http://eng-1255561905.cossh.myqcloud.com/3a/ver.txt",downVersub)
    
    generalPath = os.getenv("HOME") .. "/Documents/"
    
    
end




-- This function gets called once every frame
function draw()
    -- This sets a dark background color 
    background(40, 40, 50)
    strokeWidth(5)

    if file_exists(generalPath.."ver.txt") then
        verInApp = string.sub( readText("Documents:ver"),1,3)
    else
        verInApp = "no ver"
    end
    
    if file_exists(generalPath.."versub.txt") then
        verDowned = string.sub( readText("Documents:versub"),1,3)
    else
        verDowned = " no verdowned"
    end
    
    text("ver is "..verInApp,WIDTH*0.3,HEIGHT*0.8)
    text("versub is "..verDowned,WIDTH*0.6,HEIGHT*0.8)
     
    if verInApp ~= verDowned then
        text("ver is not equ to versub",WIDTH/2,HEIGHT*0.7)
        if downing == false then
         http.request("http://eng-1255561905.cossh.myqcloud.com/3a/ver.txt",downVer)
        end
    else
        text("they are equ",WIDTH/2,HEIGHT/2)
    end
    
    
end

function downVersub(data,status,headers)
    print("versub connected")

    writeVer("versub.txt",data)

end

function downVer(data,status,headers)
    downing = true
    print("ver connected")
    writeVer("ver.txt",data)

end

function writeVer(fileName,data)

    local file = generalPath..fileName
    wFd, err = io.open(file, "w")
    if wFd == nil then
    error("\
\
Error creating/editing file " .. fileName .. "\
\
Error: " .. err)
    end
    wFd:write(data)
    wFd:close()

    print(fileName.."  downed")
    
end



function file_exists(path)
  local file = io.open(path, "rb")
  if file then file:close() end
  return file ~= nil
end



if tested in codea, after working, the 2 txts will be rewrite to 108… if compile then, it must be 108 in simulator.
pls correct 2 txts back to 105/106 then compile (don’t run in codea) .

@totorofat I ran your latest code and it works OK. One thing you can try is to write a simple program that all it does is writes something to a file and then reads it back and see if that works in the simulator and phone. If you can write and read a file, then something is wrong with your existing code. As I said, I don’t do anything with Xcode or apps so I can’t help you with that. Is this the actual code you’re using or is this just for an example. You’re constantly reading these txt files 60 times per second in draw.

@dave1707 thanks dave. this code is not my app code. I write it here just to show the problem straightly.

I searched on web. find a explanation that : the documents folder is specially for storage of something , such as photo, sound etc which are special for app use. file downed fm web will not be saved in this folder . which will be saved in Caches folder.

but obviously my code can work well in codea /documents folder.
maybe cannot pass apple’s true phone test.

just tested in /Library/Caches/ folder. situation is same… I always get the old number in 2 txt files. even the new is downed from web. how to do then ???

@totorofat Have you tried searching this forum for anything about file access using XCode.

@dave1707 hi dave, I searched. but nobody met similar problem.

I search web and find apple office explanation as following, maybe it concerned. but I tested in Caches folder. results is same … don’t know way.


  1. Only documents and other data that is user-generated, or that
    cannot otherwise be recreated by your application, should be stored in
    the <Application_Home>/Documents directory and will be automatically backed up by iCloud.2. Data that can be downloaded again or regenerated should be stored in the <Application_Home>/Library/Caches
    directory. Examples of files you should put in the Caches directory
    include database cache files and downloadable content, such as that used
    by magazine, newspaper, and map applications.3. Data that is used only temporarily should be stored in the <Application_Home>/tmp
    directory. Although these files are not backed up to iCloud, remember
    to delete those files when you are done with them so that they do not
    continue to consume space on the user’s device.

but for test in cache folder, I just copy the 2 txt files in documents folder to cache folder. then download to caches folder. result is same.

is it possible to have the liberty/caches folder in codea ? as codea only hv documents folder and drop box (assets) folder .

@dave1707 @Simeon@yojimbo2000

due to study in these days, I found this issue was found before. pls take ref for this post:
https://codea.io/talk/discussion/7282/loadtext-and-savetext#latest

it looks that this is not solved yet. so, it trouble too much.


I did many tests, and dig into the actual library of simulator. finically find something but not very clear ( as did too many tests and change in code). but I want to say something sured in advance.

it looks like that there are many location for one txt file.

when we code in Codea, we put txt in the material library in pad,
if we use readText() in Codea in pad, the Documents: or Dropbox: are the folders. let’s call it location-1.

if we use os.getenv(“HOME”) … “/Documents/” or os.getenv(“HOME”) … “/Documents/Dropbox.assets/” in codea in pad, let’s call it location-2.

in Codea, location 1,2 are same in pad. so , there is not problem to run code in pad…


after exported, there is the zip file. we open it , there will be a filebox- assets-Dropbox.assets or Documents.assets folder - then the txt file. they are in Mac. let’s call it location-3

after compile by Xcode, the matrerial files , include the txt, will be stored in a assets folder in simulator. I dig into true the simulator route and check many folders and find where it is in Mac. let’s call it location-4 .

if we use os.getenv(“HOME”) … “/Documents/” or os.getenv(“HOME”) … “/Documents/Dropbox.assets/” in Xcode in Mac, let’s call it location-5.

pls note , even for the same name of “Documents”, the route of location-4 and 5 is different.

in my code, I download txtfile by io.open–write file by os.getenv , so the file is stored in location-5.

if we use readText() in Xcode in Mac, I am reading the txt in the Documents: or Dropbox: folder in location-3 or location-4. , not location-5. ( in one of my test, it is location 3. in another, it is location-4 but not very sure as cannot remember clearly. for all tests, 100% sure it is not location 5,or I will get correct result.) .

So I alway got the wrong result.

Could I say it is a bug of Codea ?

I cannot help you directly, I have no experience with xcode. But I had an experience with web download that drove me mad some years ago. It may not be your problem but i’ll share it below just in case.

When you download a file from the internet it is cached by the web for some time (typically 1 day). So the first time you read a file from your site, it ok. But then you change the file content on your site, you download again on your ipad, but the content doesnt change. You work for hours not understanding what’s going on. And the next day… the file has changed! But new changes will again not apply for some time. Drove me crasy. The solution of this problem is to put a special command in the .htaccess file of your site, that forces the files to be always updated, not cached (i dont remember the exact command, but it is easy to google it when you have nailed it).

That’s it. Good luck!

@Jmv38 thanks you so much for your note. for my situation, the situation is clear. in xcode, I code to download to “ducuments” folder (by io.open, io.write) . when I enter the actural folder in simulater, I surprise to find the file downed is there. (before, I thought the file is downed by did not cover the old file ) . so I hv idea to find where is the file read by “readText()” . then I found the txt files in assets folder which is not the same “Documents” folder.

in another side, my server on web is ok. bcz I am always using pad to test first. then export to xcode.

your trouble can really really make us mad. but finally you find the reason. that is the fun of coding , right ?

for my problem. pls Codea team to test whether it is a bug and do something. we use Codea a to code . for most users, the final target is put app in appstore. we cannot move round xcode. right ?

@Jmv38 thanks you so much for your note. I met this now…haha…

all are same as what you said. I am using object store service now.
I already used it in 2 services fm 2 company. all have this problem.

at first using, ( register, get new service ) , in the first day, all can work well. the data is update immediately .

after some days, all are same you said. also, for 1 service, I already wait for 3 days. data is not the new yet.

which kind of key I can use to search in google ? can your find your exact command ? thanks so much.