Works in Sim, Will It Work Live?

The three routines to create a text file, write to it, and read from it work when testing on the iOS simulator. Is there any reason to think that they won’t work on an actual iPhone? Is the SpecialFolder location reliable to use in production?

Sub CreateFile()
Var f As FolderItem = SpecialFolder.Documents.Child(“cpc_user_data.txt”)
If f.Exists Then
lblMsg.text = “file found”
else
lblMsg.text = “file not found…creating…”
//create the file with default data
Dim output As TextOutputStream
output = TextOutputStream.Create(f)
End If
End Sub

Sub WriteFile()
Var file As FolderItem = SpecialFolder.Documents.Child(“cpc_user_data.txt”)
If file <> Nil Then
Var output As TextOutputStream
output = TextOutputStream.Create(file)
output.WriteLine(txtInput.text)
output.Close
End If
End Sub

Sub ReadFile()
Var f As FolderItem = SpecialFolder.Documents.Child(“cpc_user_data.txt”)
If f.Exists Then
lblMsg.text = “found file…trying to read…”
Var input As TextInputStream
input = TextInputStream.Open(f)
txtOutput.Text = input.ReadAll
input.Close
else
lblMsg.text = “file not found”
End If
End Sub

I see no reason why it should not work on an actual device. Why don’t you test it on a device?

Your code will work on 99.99% of devices but can fail on devices where free storage is low.

Public Sub CreateFile()
  Var docs As FolderItem = SpecialFolder.Documents
  
  if docs.exists = false then
    docs.CreateFolder
  End if
  
  Var logMsg As String
  
  Var f As FolderItem = docs.Child("cpc_user_data.txt")
  
  If f.Exists Then
    logMsg = "file found"
  else
    logMsg = "file not found…creating…"
    //create the file with default data
    try
      Dim output As TextOutputStream
      output = TextOutputStream.Create(f)
    catch err as IOException
      logMsg = "file not created. " + err.reason
    end try
  End If
  
  lblMsg.text = logMsg
End Sub
Public Sub WriteFile()
  Var docs As FolderItem = SpecialFolder.Documents
  
  if docs.exists = false then
    docs.CreateFolder
  End if
  
  Var file As FolderItem = docs.Child("cpc_user_data.txt")
  
  If file <> Nil Then
    
    try
      Var output As TextOutputStream
      output = TextOutputStream.Create(file)
      output.WriteLine(txtInput.text)
      output.Close
    Catch err as IOException
      //Do something with the error
      
    end try
    
  End If
End Sub

Thanks. I’m going to try and test it on my phone today.

Forum for Xojo Programming Language and IDE. Copyright © 2021 Xojo, Inc.