I would like my Swift app to be able to write a file (myDebug.log) that is viewable by a user on their iOS device.
The code I am using is below. While the code can be cleaner and more efficient, right now my issue is being able to access/view the file from the iOS device.
I can see the file if it is created in Xcode's (13.4.1) simulator.
I can see the file when I look at my iPhone's container for the app using Xcode. Although I cannot access it there, if that has any bearing.
However, I cannot find the file using iPhone's File app. Which is the bottom line, I would like the log to be accessible from iOS devices outside of at the development setting.
My first guess was that I am choosing the incorrect directory? However, I can see it on the "Documents" folder of my simulator. So am still unsure as to where my error is.
In the code below I have also tried using the allDomainsMask but that made no difference.
Would appreciate any advice on what I am doing incorrectly.
//setup on viewDidLoad
if debugLog {
debugFileURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0].appendingPathComponent("myDebug.log")
}
//record an event
func debugLog (msg: [String]) {
...exit if not logging, or no valid URL...
...prep text...
if let handle = try? FileHandle(forWritingTo: dbClass.debugFileURL!) {
handle.seekToEndOfFile()
handle.write(text.data(using: .utf8)!)
handle.closeFile()
} else {
try! text.write(to: dbClass.debugFileURL!, atomically: false, encoding: .utf8)
}
}
CodePudding user response:
In the Info section you need to add an entry for Supports Document Browser
and set its value to YES
.
From documentation:
To allow other apps to open and edit the files stored in your app’s Documents folder, set this key to YES. This key also lets users set the app’s default save location in Settings.
After all the Files
app is just another app. But be aware this files could be read from all apps.