Skip to content
This repository has been archived by the owner on Feb 27, 2019. It is now read-only.

Commit

Permalink
Added Disabled status: system level only.
Browse files Browse the repository at this point in the history
  • Loading branch information
bre7 committed Apr 27, 2015
1 parent 0b46f21 commit b270f3a
Showing 1 changed file with 25 additions and 1 deletion.
26 changes: 25 additions & 1 deletion PermissionScope/PermissionScope.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public enum PermissionStatus: String {
case Authorized = "Authorized"
case Unauthorized = "Unauthorized"
case Unknown = "Unknown"
case Disabled = "Disabled" // System-level
}

public enum PermissionDemands: String {
Expand Down Expand Up @@ -341,6 +342,10 @@ public class PermissionScope: UIViewController, CLLocationManagerDelegate, UIGes
// MARK: dealing with system permissions

public func statusLocationAlways() -> PermissionStatus {
if !CLLocationManager.locationServicesEnabled() {
return .Disabled
}

let status = CLLocationManager.authorizationStatus()
switch status {
case .AuthorizedAlways:
Expand All @@ -359,12 +364,18 @@ public class PermissionScope: UIViewController, CLLocationManagerDelegate, UIGes
locationManager.requestAlwaysAuthorization()
case .Unauthorized:
self.showDeniedAlert(.LocationAlways)
case .Disabled:
self.showDisabledAlert(.LocationInUse)
default:
break
}
}

public func statusLocationInUse() -> PermissionStatus {
if !CLLocationManager.locationServicesEnabled() {
return .Disabled
}

let status = CLLocationManager.authorizationStatus()
// if you're already "always" authorized, then you don't need in use
// but the user can still demote you! So I still use them separately.
Expand All @@ -385,6 +396,8 @@ public class PermissionScope: UIViewController, CLLocationManagerDelegate, UIGes
locationManager.requestWhenInUseAuthorization()
case .Unauthorized:
self.showDeniedAlert(.LocationInUse)
case .Disabled:
self.showDisabledAlert(.LocationInUse)
default:
break
}
Expand Down Expand Up @@ -675,7 +688,7 @@ public class PermissionScope: UIViewController, CLLocationManagerDelegate, UIGes

func showDeniedAlert(permission: PermissionType) {
var alert = UIAlertController(title: "Permission for \(permission.rawValue) was denied.",
message: "Please enable access to \(permission.rawValue) in Settings",
message: "Please enable access to \(permission.rawValue) in the App's Settings",
preferredStyle: .Alert)
alert.addAction(UIAlertAction(title: "OK",
style: .Cancel,
Expand All @@ -690,6 +703,17 @@ public class PermissionScope: UIViewController, CLLocationManagerDelegate, UIGes
animated: true, completion: nil)
}

func showDisabledAlert(permission: PermissionType) {
var alert = UIAlertController(title: "\(permission.rawValue) is currently disabled.",
message: "Please enable access to \(permission.rawValue) in Settings",
preferredStyle: .Alert)
alert.addAction(UIAlertAction(title: "OK",
style: .Cancel,
handler: nil))
self.presentViewController(alert,
animated: true, completion: nil)
}

// MARK: gesture delegate

public func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldReceiveTouch touch: UITouch) -> Bool {
Expand Down

0 comments on commit b270f3a

Please sign in to comment.