Giter Site home page Giter Site logo

abcincs / applock Goto Github PK

View Code? Open in Web Editor NEW
2.0 0.0 0.0 596 KB

AppLock is a simple SwiftUI framework that makes it easy to create a PinCode user interface. It provides a struct, AppLockView, that can be shown inside a view or a sheet for better user experience.

Swift 100.00%
spm pincode pin code applocker applock passcode swift swiftui

applock's Introduction

AppLock

Twitter: @cedricbahirwe

AppLock is a simple SwiftUI framework that makes it easy to create a PinCode user interface. It provides a struct, AppLockView, that can be shown inside a view or a sheet for better user experience.

Natively, Apple only provides Touch ID and Face ID as authentication mechanisms and only brings Passcode when authentication with those ones have failed. The goal here was to provide a fully fledged Component that can be customized and reuse with ease.

Usage

You should create an instance of AppLockView with three parameters: the pin to check against, a boolean flag to indicate whether or not you want AppLockView to provide an Error UI, and a closure that will be called when a result is ready. This closure must accept a Result<Bool, UnlockError>, where the bool is the indicator of a matching that was found on success and UnlockError will be either badPin.

Important: AppLockView requires a project with a version greater or equal to 14.0

Examples

Here's some example code to create an AppLock view that prints when the pin matched code.

AppLockView(pincode: .init("1975")) { result  in
    switch result {
    case .success(_):
        print("Match pin code")
    case .failure(let error):
        print(error.localizedDescription)
    }
}

Your completion closure is probably where you want to dismiss the AppLockView.

Here's an example on how to present the AppLockview as a sheet and how we can pass to the next view in a NavigationView when pin code has matched:

struct AppLockExampleView: View {
    @State var isPresentingLocker = false
    @State var matchedCode: Bool = false

    var body: some View {
        NavigationView {
            VStack(spacing: 10) {
                if matchedCode {
                    NavigationLink("Next page", destination: NextView(), isActive: .constant(true)).hidden()
                }
                Button("Unlock App") {
                    self.isPresentingLocker = true
                }
                .sheet(isPresented: $isPresentingLocker) {
                    self.appLockSheet
                }
                Text("Unlock the app to begin")
            }

        }
    }

    var appLockSheet : some View {
        AppLockView(
            pincode: .init("2021"),
            completion: { result in
                if case .success(_) = result {
                    self.matchedCode = true
                    self.isPresentingLocker = false
                }
            }
        )
    }
}

Credits

AppLock was made by Cédric Bahirwe. It’s available under the MIT license, which permits commercial use, modification, distribution, and private use.

License

MIT License.

Copyright (c) 2021 Cédric Bahirwe

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

applock's People

Contributors

cedricbahirwe avatar

Stargazers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.