Skip to content

A drop-in replacement for the iOS 14 @AppStorage property wrapper compatible with iOS 13

License

Notifications You must be signed in to change notification settings

xavierLowmiller/AppStorage

Repository files navigation

AppStorage

A drop-in replacement for the iOS 14 @AppStorage property wrapper.

Build Status CocoaPods Compatible Carthage Compatible Platform

Features

  • 100% identical API as SwiftUI's @AppStorage property wrapper
  • Fully compatible with iOS 13, macOS Catalina, watchOS 6, tvOS 13
  • Automatically updates when the underlying UserDefaults changes
  • Well tested

Usage

// iOS 14
@AppStorage("text") var text = "Default Text"
@AppStorage("magic_number", store: .customUserDefaults) var magicNumber = 42

// iOS 13
@AppStorageCompat("text") var text = "Default Text"
@AppStorageCompat("magic_number", store: .customUserDefaults) var magicNumber = 42
More complete example

import SwiftUI
import AppStorage

enum StringEnum: String, Identifiable {
    case a, b, c
    var id: String { rawValue }
}

enum IntEnum: Int, Identifiable {
    case this, that, theOther
    var id: Int { rawValue }
}

struct ContentView: View {
    @AppStorageCompat("text", store: .standard) var text = "Default Text"
    @AppStorageCompat("string_enum") var selectionString: StringEnum = .a
    @AppStorageCompat("int_enum") var selectionInt: IntEnum = .this

    var body: some View {
        List {
            Section(header: Text("Acts like a persistent @State")) {
                TextField("Change me", text: $text)
                TextField("Change me, too!", text: $text)
            }

            Section(header: Text("Change UserDefaults without property wrapper")) {
                Button("Sneakily change a UserDefault") {
                    UserDefaults.standard.setValue("One more thing...", forKey: "text")
                }
                Button("Remove a UserDefault") {
                    UserDefaults.standard.setValue(nil, forKey: "text")
                }
            }

            Section(header: Text("Enums with raw values")) {
                Picker("Pick Me", selection: $selectionString) {
                    Text("a").tag(StringEnum.a)
                    Text("b").tag(StringEnum.b)
                    Text("c").tag(StringEnum.c)
                }.pickerStyle(SegmentedPickerStyle())

                Picker("Pick Me", selection: $selectionInt) {
                    Text("this").tag(IntEnum.this)
                    Text("that").tag(IntEnum.that)
                    Text("the other").tag(IntEnum.theOther)
                }.pickerStyle(SegmentedPickerStyle())
            }

        }.listStyle(GroupedListStyle())
    }
}

Installation

Swift Package Manager

Add the package to your Package.swift file:

dependencies: [
    .package(url: "https://github.com/xavierLowmiller/AppStorage.git", .upToNextMajor(from: "1.0.4"))
]

CocoaPods

Add the pod to your Podfile:

platform :ios, '13.0'
use_frameworks!

target 'MyApp' do
  pod 'AppStorage', '~> 1.0.4'
end

Carthage

Add this line to your Cartfile:

github "xavierLowmiller/AppStorage" ~> 1.0.4

Manual

Since it's just a single file, you can just download and drag it to your project.

About

A drop-in replacement for the iOS 14 @AppStorage property wrapper compatible with iOS 13

Resources

License

Stars

Watchers

Forks

Packages

No packages published