The iOS file system
The sandbox
Subfolders of the sandbox
Writing and reading to the file system
“sandbox”: where the app keeps all of its “stuff”.
Sandbox
Bundle Container – MyApp.app
-> executable code, resources
Data Container – Documents, Library, Temp
-> user data(Documents/Inbox), non-user data(Library/Applications Support, Library/Caches)
iCloud Container – …
Library/Preferences/info.myapp.mobile.plist
*Documents: important stuff
*Caches:
*Library:
1. Find where the sandbox is.
2. Write to a file
-NSFileManager to get the path to the sandbox
-String to write or read text files
-NSData to write or read binary files
func sandboxPlayground(){
let fm = FileManager.default
let urls = fm.urls(for:.documentDirectory, in: .userDomainMask)
let url = urls.last?.appendingPathComponent("file.txt")
do {
try "Hi There!".write(to: url!, atomically: true, encoding: String.Encoding.utf8)
} catch {
print("Error while writing")
}
do {
let content = try String(contentsOf: url!, encoding: String.utf8)
if content == "Hi There!"{
print("yay")
} else {
print("oops")
}
} catch {
print("Something went wrong")
}
}
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
sandboxPlayground()
return true
})