All Projects → jevonmao → Permissionsswiftui

jevonmao / Permissionsswiftui

Licence: mit
A SwiftUI package to beautifully display and handle permissions.

Programming Languages

swift
15916 projects

Projects that are alternatives of or similar to Permissionsswiftui

Tosin
Initialize a npm package with everything included, from CI to documentation website
Stars: ✭ 142 (-35.45%)
Mutual labels:  library, package
Future.apply
🚀 R package: future.apply - Apply Function to Elements in Parallel using Futures
Stars: ✭ 159 (-27.73%)
Mutual labels:  programming, package
Laravel Paket
Composer GUI. Manage Laravel dependencies from web interface without switching to command line!
Stars: ✭ 143 (-35%)
Mutual labels:  library, package
State Of The Art Shitcode
💩State-of-the-art shitcode principles your project should follow to call it a proper shitcode
Stars: ✭ 2,207 (+903.18%)
Mutual labels:  programming, coding
Json table
Flutter package: Json Table Widget to create table from json array
Stars: ✭ 178 (-19.09%)
Mutual labels:  library, package
Overlap
Tiny iOS library to achieve overlap visual effect
Stars: ✭ 133 (-39.55%)
Mutual labels:  ux, ui-design
Fos
Web Components to turn your web app into a fake operating system
Stars: ✭ 151 (-31.36%)
Mutual labels:  library, ux
Guide To Staying Productive
If you're looking for ways to stay motivated and focused, while still having fun, this guide is for you! Contributions and any kind of improvements are very welcome!
Stars: ✭ 116 (-47.27%)
Mutual labels:  programming, coding
Move To Library Sketchplugin
You can now move symbol from your project to any library and re-attach all the symbol instances to this library. also it keep the overrides without any problems and it work with abstract that have libraries not in your local machine
Stars: ✭ 174 (-20.91%)
Mutual labels:  library, ux
Bttn.css
Awesome buttons for awesome projects!
Stars: ✭ 2,004 (+810.91%)
Mutual labels:  library, ui-design
Arare
Lightweight curried functional programming library
Stars: ✭ 127 (-42.27%)
Mutual labels:  programming, library
Effective Shell
Text, samples and website for my 'Effective Shell' series.
Stars: ✭ 204 (-7.27%)
Mutual labels:  programming, coding
Competitiveprogrammingquestionbank
This repository contains all the popular competitive programming and DSA questions with solutions.
Stars: ✭ 122 (-44.55%)
Mutual labels:  programming, coding
You Dont Know Ui
Learn how to build universal, modern and scalable user interfaces
Stars: ✭ 140 (-36.36%)
Mutual labels:  ux, ui-design
React Rainbow
🌈 React Rainbow Components. Build your web application in a snap.
Stars: ✭ 1,662 (+655.45%)
Mutual labels:  library, developer
Android Cheat Sheet
Cheat Sheet for Android Interviews
Stars: ✭ 1,891 (+759.55%)
Mutual labels:  programming, coding
Realtaiizor
C# WinForm UI/UX Component Library
Stars: ✭ 109 (-50.45%)
Mutual labels:  ux, ui-design
Overrideit Sketchplugin
Overrideit is sketch plugin that allow designers to search in overridelist and overrides dropdowns, and with many other features.
Stars: ✭ 113 (-48.64%)
Mutual labels:  library, ux
React Messenger
Chat UX components built with React, inspired by Facebook Messenger
Stars: ✭ 167 (-24.09%)
Mutual labels:  library, ux
Learning Go
Source code repository for my book "Learning Go Programming"
Stars: ✭ 194 (-11.82%)
Mutual labels:  programming, coding


PermissionsSwiftUI: A SwiftUI package to handle permissions

PermissionsSwiftUI displays and handles permissions in SwiftUI. It is largely inspired by SPPermissions. The UI is highly customizable and resembles an Apple style. If you like the project, don't forget to star ★ and follow me on GitHub.

   

PermissionsSwiftUI looks equally gorgeous on both ☀️light and 🌑dark mode.

Navigation

Usage

Installation

Requirements

  • iOS 13 or iPadOS 13
  • Xcode 12 and Swift 5.3
  • No tvOS, MacOS, and WatchOS support for now

Install

You can install PermissionsSwiftUI into your Xcode project via Swift Package Manager. To learn more about Swift Package Manager, click here

  1. In Xcode, open your project and navigate to FileSwift PackagesAdd Package Dependency...
  2. Paste the repository URL (https://github.com/jevonmao/PermissionsSwiftUI) and click Next.
  3. For Version, select Up to next major.
  4. Click Next and click Finish.
  5. You are all set, have fun using PermissionsSwiftUI!

Quickstart

Before you start, please star ★ this repository. Your star is my biggest motivation to pull all-nighters and maintain this open source project.

Modal Style

To use PermissionsSwiftUI, simply add the JMModal modifier to any view:

.JMModal(showModal: $showModal, for: [.locationAlways, .photo, .microphone])`

Pass in a Binding<Bool> to show the modal view, and add whatever permissions you want to show. For example:

   struct ContentView: View {
       @State var showModal = false
       var body: some View {
           Button(action: {
               showModal=true
           }, label: {
               Text("Ask user for permissions")
           })
           .JMModal(showModal: $showModal, for: [.locationAlways, .photo, .microphone])
       }
   }

Alert Style

The alert style is equally gorgeous, and allows for more versatile use. It is recommended when you have less than 3 permissions.
To show a permission pop up alert, use:
.JMAlert(showModal: $showModal, for: [.locationAlways, .photo])

Similar to the previous JMPermissions, you need to pass in a Binding<Bool> to show the view, and add whatever permissions you want to show.

Usage

Customize Permission Texts

😱 Be aware. Features ahead will wow you - the customization is so advanced, yet so simple. Have fun!

To customize permission texts, use the modifier setPermissionComponent() For example, you can change title, description, and image icon:

.setPermissionComponent(for: .camera, 
                        image: AnyView(Image(systemName: "camera.fill")), 
                        title: "Camcorder",
                        description: "App needs to record videos")

and the result:


Or only change 1 of title and description:
setPermissionComponent(for: .tracking, title: "Trackers")
setPermissionComponent(for: .tracking, description: "Tracking description")

Note:

  • The parameters you don't provide will show the default text
  • Add the setPermissionComponent modifier on your root level view, after JMPermissions modifier

The image parameter accepts AnyView, so feel free to use SF Symbols or your custom asset:

.setPermissionComponent(for: .camera, 
                        image: AnyView(Image("Your-cool-image"))

Even full SwiftUI views will work😱:

.setPermissionComponent(for: .camera, 
                        image: AnyView(YourCoolView())

You can use custom text and icon for all the supported permissions, with a single line of code.

Customize Header Texts

To customize the header title, use the modifier changeHeaderTo: Annotated for headers screen

.JMPermissions(showModal: $showModal, for: [.camera, .location, .calendar])
.changeHeaderTo("App Permissions")

To customize the header description, use the modifier changeHeaderDescriptionTo:

.JMPermissions(showModal: $showModal, for: [.camera, .location, .photo])
.changeHeaderDescriptionTo("Instagram need certain permissions in order for all the features to work.")

To customize the bottom description, use the modifier changeBottomDescriptionTo:

.JMPermissions(showModal: $showModal, for: [.camera, .location, .photo])
.changeBottomDescriptionTo("If not allowed, you have to enable permissions in settings")

onAppear and onDisappear Override

You might find it incredibly useful to execute your code, or perform some update action when a PermissionsSwiftUI view appears and disappears.
You can perform some action when PermissionsSwiftUI view appears or disappears by:

.JMPermissions(showModal: $showModal, for: [.locationAlways, .photo, .microphone], onAppear: {}, onDisappear: {})

The onAppear and onDisappear closure parameters will be executed everytime PermissionsSwiftUI view appears and disappears.
The same view modifier closure for state changes are available for the JMAlert modifier:

.JMAlert(showModal: $showModal,
                     for: [.locationAlways, .photo],
                     onAppear: {print("Appeared")},
                     onDisappear: {print("Disappeared")})

Auto Check Authorization

PermissionsSwiftUI by default will automatically check for authorization status. It will only show permissions that are currently notDetermined status. (iOS system prevent developers from asking denied permissions. Allowed permissions will also be ignored by PermissionsSwiftUI). If all permissions are allowed or denied, PermissionsSwiftUI will not show the modal or alert at all. To set auto check authorization, use the autoCheckAuthorization parameter:

.JMModal(showModal: $showModal, for: [.camera], autoCheckAuthorization: false)

same applies for JMAlert

.JMAlert(showModal: $showModal, for: [.camera], autoCheckAuthorization: false)

Auto Dismiss

PermissionsSwiftUI by default will automatically dismiss the modal or alert after user allows the last permission item. However, you can override this behavior.

func JMModal(showModal: Binding<Bool>, for permissions: [PermissionType], autoDismiss: Bool) -> some View

Pass in true or false to select whether to automatically dismiss the view.

Customize Colors

Using PermissionSwiftUI's capabilities, developers and designers can customize all the UI colors with incredible flexibility. You can fully configure all color at all states with your custom colors.
To easily change the accent color:

.setAccentColor(to: Color(.systemPurple))

To change the primary (default Apple blue) and tertiary (default Apple red) colors:

.setAccentColor(toPrimary: Color(.systemPurple),
                toTertiary: Color(.systemGreen))
Alert pop up with customized colors

⚠️ .setAccentColor() and .setAllowButtonColor() should never be used at the same time.

To unleash the full customization of all button colors under all states, you need to pass in the AllButtonColors struct:

.setAllowButtonColor(to: .init(buttonIdle: ButtonColor(foregroundColor: Color,
                                                               backgroundColor: Color),
                                       buttonAllowed: ButtonColor(foregroundColor: Color,
                                                                  backgroundColor: Color),
                                       buttonDenied: ButtonColor(foregroundColor: Color,
                                                                 backgroundColor: Color)))

For more information regarding the above method, reference the official documentation.

Restrict Dismissal

PermissionsSwiftUI will by default, prevent the user from dismissing the modal and alert, before all permissions have been interacted. This means if the user has not explictly denied or allowed EVERY permission shown, they will not be able to dismiss the PermissionsSwiftUI view. This restrict dismissal behavior can be overriden by the var restrictModalDismissal: Bool or var restrictAlertDismissal: Bool properties. To disable the default restrict dismiss behavior:

.JMModal(showModal: $show, for permissions: [.camera], restrictDismissal: false)

You can also configure with the model:

let model: PermissionStore = {
        var model = PermissionStore()
        model.permissions = [.camera]
        model.restrictModalDismissal = false
        model.restrictAlertDismissal = false
        return model
    }
    ......

    .JMModal(showModal: $showModal, forModel: model)

Configuring Health Permissions

Unlike all the other permissions, the configuration for health permission is a little different. Because Apple require developers to explictly set read and write types, PermissionsSwiftUI greatly simplifies the process.

HKAccess

The structure HKAccess is required when initalizing health permission’s enum associated values. It encapsulates the read and write type permissions for the health permission.

To set read and write health types (activeEnergyBurned is used as example here):

let healthTypes = Set([HKSampleType.quantityType(forIdentifier: .activeEnergyBurned)!])
.JMModal(showModal: $show, for: [.health(categories: .init(readAndWrite: healthTypes))])

//Same exact syntax for JMAlert styles
.JMAlert(showModal: $show, for: [.health(categories: .init(readAndWrite: healthTypes))])

To set read or write individually:

let readTypes = Set([HKSampleType.quantityType(forIdentifier: .activeEnergyBurned)!])
let writeTypes = Set([HKSampleType.quantityType(forIdentifier: .appleStandTime)!])
.JMModal(showModal: $showModal, for: [.health(categories: .init(read: readTypes, write: writeTypes))])

You may also set only read or write type:

let readTypes = Set([HKSampleType.quantityType(forIdentifier: .activeEnergyBurned)!])
.JMModal(showModal: $showModal, for: [.health(categories: .init(read: readTypes))])

Supported Permissions

Here is a list of all permissions PermissionsSwiftUI already supports support(health not in image but is supported). Yup, even the newest tracking permission for iOS 14 so you can stay on top of your game. All permissions in PermissionsSwiftUI come with a default name, description, and a stunning Apple native SF Symbols icon.


A card of all the permissions

Additional Information

Acknowledgement

SPPermissions is in large a SwiftUI remake of famous Swift library SPPermissions by @verabeis. SPPermissions was initially created in 2017, and today on GitHub has over 4000 stars. PermissionsSwiftUI aims to deliver a just as beautiful and powerful library in SwiftUI. If you star ★ my project PermissionsSwiftUI, be sure to checkout the original project SPPermissions where I borrowed the UI Design, some parts of README.md page, and important source code references along the way.

License

PermissionsSwiftUI is created by Jingwen (Jevon) Mao and licensed under the MIT License

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].