Norway


Topics

Topics

Our apps can do tons of useful things with users’ . Before we can though, we must always ask for their permission. Whether it’s their physical , photos, or contacts, the user is always in control of when and how we can their data.

Each of these respective APIs has it’s own way of asking the user if they will allow us to access the data within.

Today we’ll check out a library called Permission that unifies all of these permission request into one beautiful API. Let’s dive in.

We begin by declaring which piece of data we’d like access to:

let permission: Permission = .Photos

At this point, permission.status will report .NotDetermined (as we’d expect). Next we’ll request the permission, this is what causes the prompt to be presented to the user:

permission.request { status in
  switch status {
  case .Authorized:    print("yay!")
  case .Denied:        print("boo")
  case .Disabled:      print("boo boo")
  case .NotDetermined: print(_(ツ)_/¯")
  }
}

But wait, there’s more!

Permission is smart. If we request access to something the user has already said ‘no’ to, it will present an alert (with customizable title/message/buttons) that allows the user to jump to the part of Settings. needed to flip the switch back to ‘yes’.

Additionally, Permission also provides a PermissionSet type to do this on groups of permissions (i.e. Contacts + Camera + Photos).

There’s also a PermissionButton (a UIButton subclass) that can display the current permission state, and prompt when tapped.

More info about Permission can be found at git.io/permission



Source link

LEAVE A REPLY

Please enter your comment!
Please enter your name here