article

Monday, May 24, 2021

Local storage UserDefaults CRUD Create Read Update Delete

A simple app Swift UI that allow to create, read,upate and delete value in local storage using UserDefaults

ContentView.swift
 
//
//  ContentView.swift
//  Testapp
//
//  Created by Cairocoders 
//

import SwiftUI


struct ContentView: View {

    @State var showAlert: Bool = false
    
    var body: some View {
        NavigationView {
            VStack {
                
                Text("Local storage UserDefaults CRUD Create Read Update Delete")
                    .font(.largeTitle)
                    .bold()
                    .padding(.bottom, 50)
                
                HStack {
                    
                    NavigationLink (destination: AddView(), label: {
                        Text("Add")
                            .font(.headline)
                            .foregroundColor(.white)
                            .padding()
                            .background(Color.green)
                    })
                    
                    NavigationLink (destination: DataView(), label: {
                        Text("View")
                            .font(.headline)
                            .foregroundColor(.white)
                            .padding()
                            .background(Color.green)
                    })
                    
                    NavigationLink (destination: EditView(), label: {
                        Text("Edit")
                            .font(.headline)
                            .foregroundColor(.white)
                            .padding()
                            .background(Color.green)
                    })
                    
                    Button(action: {
                        LocalStorage.removeValue() //Delete a value from local storage
                        self.showAlert = true
                    }, label: {
                        Text("Delete")
                            .font(.headline)
                            .foregroundColor(.white)
                            .padding()
                            .background(Color.green)
                    })
                }
            }
            .alert(isPresented: $showAlert, content: {
                Alert(title: Text("Data has been removed"))
            })
        }
    }
    

}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}
LocalStorage.swift

This file will handle the creating, fetching, updating and deleting the data from local storage.

 
//
//  LocalStorage.swift
//  Testapp
//
//  Created by Cairocoders
//

import Foundation

class LocalStorage {
    
    private static let myKey: String = "myKey" //static constant named
    
    public static var myValue: String {
        set {
            UserDefaults.standard.set(newValue, forKey: myKey)
        }
        get {
            return UserDefaults.standard.string(forKey: myKey) ?? ""
        }
    }
    
    public static func removeValue() {
        UserDefaults.standard.removeObject(forKey: myKey) //removeValue() remove that value from UserDefaults.
    }
    
}
AddView.swift
 
//
//  AddView.swift
//  Testapp
//
//  Created by Cairocoders
//

import SwiftUI

struct AddView: View {
    @State var value: String = ""
    @State var showAlert: Bool = false
    
    var body: some View {
        VStack {
            TextField("Enter value", text: $value)
                .padding(10)
                .background(Color(.systemGray6))
                .cornerRadius(5)
                .disableAutocorrection(true)
            Button(action: {
                LocalStorage.myValue = self.value //Add value to local storage
                self.showAlert = true
            }, label: {
                Text("Save")
            })
        }
        .padding()
        .alert(isPresented: $showAlert, content: {
            Alert(title: Text("Data has been saved"))
        })
    }
}

struct AddView_Previews: PreviewProvider {
    static var previews: some View {
        AddView()
    }
}
DataView..swift
 
//
//  DataView..swift
//  Testapp
//
//  Created by Cairocoders
//

import SwiftUI

struct DataView: View {
    var body: some View {
        Text(LocalStorage.myValue) //Get value from local storage
    }
}

struct DataView_Previews: PreviewProvider {
    static var previews: some View {
        DataView()
    }
}
EditView.swift
 
//
//  EditView.swift
//  Testapp
//
//  Created by Cairocoders
//

import SwiftUI

struct EditView: View {
    @State var value: String = ""
    @State var showAlert: Bool = false
    
    var body: some View {
        VStack {
            TextField("Enter value", text: $value)
                .padding(10)
                .background(Color(.systemGray6))
                .cornerRadius(5)
                .disableAutocorrection(true)
            Button(action: {
                LocalStorage.myValue = self.value
                self.showAlert = true
            }, label: {
                Text("Update")
            })
        }
        .padding()
        .onAppear(perform: {
            self.value = LocalStorage.myValue //Edit value in local storage
        })
        .alert(isPresented: $showAlert, content: {
            Alert(title: Text("Data has been updated"))
        })
    }
}

struct EditView_Previews: PreviewProvider {
    static var previews: some View {
        EditView()
    }
}

Related Post