import SwiftUI
struct OfflineNotesView: View {
@State private var noteText: String = ""
private let noteKey = "savedNote"
var body: some View {
VStack {
TextEditor(text: $noteText)
.padding()
.border(Color.gray, width: 1)
.frame(height: 300)
Button("Save") {
UserDefaults.standard.set(noteText, forKey: noteKey)
}
.padding()
}
.padding()
.onAppear {
if let savedNote = UserDefaults.standard.string(forKey: noteKey) {
noteText = savedNote
}
}
}
}
struct OfflineNotesView_Previews: PreviewProvider {
static var previews: some View {
OfflineNotesView()
}
}This app uses UserDefaults to save and load the note text locally on the device. When the user taps the Save button, the current text is stored under a key. When the view appears, it checks if a saved note exists and loads it into the text editor. This local storage means the note is available even without internet, enabling offline functionality.
UserDefaults is a simple way to store small pieces of data persistently on iOS devices. It works like a small local database that the app can read and write anytime.