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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
//
//  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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
//
//  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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
//
//  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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
//
//  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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
//
//  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