In Swift, you can protect sensitive files by utilizing file protection attributes that are part of the iOS File Protection feature. This feature ensures that files are only accessible when the device is unlocked, adding an extra layer of security for user data.
Here's how to implement file protection attributes in Swift:
let fileURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first?.appendingPathComponent("sensitiveFile.txt")
if let fileURL = fileURL {
// Write data to the file
let data = "This is sensitive data.".data(using: .utf8)
do {
try data?.write(to: fileURL, options: [.completeFileProtection])
print("File saved with complete file protection.")
} catch {
print("Error saving file: \(error)")
}
// To read the file when the device is unlocked
do {
let storedData = try Data(contentsOf: fileURL)
let content = String(data: storedData, encoding: .utf8)
print("File content: \(content ?? "")")
} catch {
print("Error reading file: \(error)")
}
}
How do I avoid rehashing overhead with std::set in multithreaded code?
How do I find elements with custom comparators with std::set for embedded targets?
How do I erase elements while iterating with std::set for embedded targets?
How do I provide stable iteration order with std::unordered_map for large datasets?
How do I reserve capacity ahead of time with std::unordered_map for large datasets?
How do I erase elements while iterating with std::unordered_map in multithreaded code?
How do I provide stable iteration order with std::map for embedded targets?
How do I provide stable iteration order with std::map in multithreaded code?
How do I avoid rehashing overhead with std::map in performance-sensitive code?
How do I merge two containers efficiently with std::map for embedded targets?