8000 GitHub - yHomework/AppStorage
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

yHomework/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

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Swift 94.7%
  • Ruby 5.3%
0