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

Dismissed callback #222

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion PermissionScope.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = 'PermissionScope'
s.version = '1.1.1'
s.version = '1.1.2'
s.license = 'MIT'
s.summary = 'A Periscope-inspired way to ask for iOS permissions'
s.homepage = 'https://github.com/nickoneill/PermissionScope'
Expand Down
21 changes: 14 additions & 7 deletions PermissionScope/PermissionScope.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import Contacts
public typealias statusRequestClosure = (_ status: PermissionStatus) -> Void
public typealias authClosureType = (_ finished: Bool, _ results: [PermissionResult]) -> Void
public typealias cancelClosureType = (_ results: [PermissionResult]) -> Void
public typealias dismissedClosureType = (_ canceled: Bool) -> Void
typealias resultsForConfigClosure = ([PermissionResult]) -> Void

@objc public class PermissionScope: UIViewController, CLLocationManagerDelegate, UIGestureRecognizerDelegate, CBPeripheralManagerDelegate {
Expand Down Expand Up @@ -93,17 +94,19 @@ typealias resultsForConfigClosure = ([PermissionResult]) -> Void
// Useful for direct use of the request* methods

/// Callback called when permissions status change.
public var onAuthChange: authClosureType? = nil
public var onAuthChange: authClosureType? = nil
/// Callback called when the user taps on the close button.
public var onCancel: cancelClosureType? = nil
public var onCancel: cancelClosureType? = nil
/// Callback called when the controller is dismissed
public var onDismiss: dismissedClosureType? = nil

/// Called when the user has disabled or denied access to notifications, and we're presenting them with a help dialog.
public var onDisabledOrDenied: cancelClosureType? = nil
/// View controller to be used when presenting alerts. Defaults to self. You'll want to set this if you are calling the `request*` methods directly.
public var viewControllerForAlerts : UIViewController?

/**
Checks whether all the configured permission are authorized or not.
Checks whether all the configured permissions are authorized or not.

- parameter completion: Closure used to send the result of the check.
*/
Expand All @@ -117,7 +120,7 @@ typealias resultsForConfigClosure = ([PermissionResult]) -> Void
}

/**
Checks whether all the required configured permission are authorized or not.
Checks whether all the required configured permissions are authorized or not.
**Deprecated** See issues #50 and #51.

- parameter completion: Closure used to send the result of the check.
Expand Down Expand Up @@ -996,11 +999,12 @@ typealias resultsForConfigClosure = ([PermissionResult]) -> Void
- parameter authChange: Called when a status is detected on any of the permissions.
- parameter cancelled: Called when the user taps the Close button.
*/
@objc public func show(_ authChange: authClosureType? = nil, cancelled: cancelClosureType? = nil) {
@objc public func show(_ authChange: authClosureType? = nil, cancelled: cancelClosureType? = nil, dismissed: dismissedClosureType? = nil) {
assert(!configuredPermissions.isEmpty, "Please add at least one permission")

onAuthChange = authChange
onCancel = cancelled
onDismiss = dismissed

DispatchQueue.main.async {
while self.waitingForBluetooth || self.waitingForMotion { }
Expand Down Expand Up @@ -1073,14 +1077,17 @@ typealias resultsForConfigClosure = ([PermissionResult]) -> Void
/**
Hides the modal viewcontroller with an animation.
*/
public func hide() {
public func hide(canceled: Bool = false) {
let window = UIApplication.shared.keyWindow!

DispatchQueue.main.async(execute: {
UIView.animate(withDuration: 0.2, animations: {
self.baseView.frame.origin.y = window.center.y + 400
self.view.alpha = 0
}, completion: { finished in
if let onDismiss = self.onDismiss {
onDismiss(canceled)
}
self.view.removeFromSuperview()
})
})
Expand Down Expand Up @@ -1120,7 +1127,7 @@ typealias resultsForConfigClosure = ([PermissionResult]) -> Void
Called when the users taps on the close button.
*/
func cancel() {
self.hide()
self.hide(canceled: true)

if let onCancel = onCancel {
getResultsForConfig({ results in
Expand Down
13 changes: 9 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ class ViewController: UIViewController {
print("got results \(results)")
}, cancelled: { (results) -> Void in
print("thing was cancelled")
}, dismissed: { canceled in
print("the controller was dismissed")
})
}
}
Expand Down Expand Up @@ -157,17 +159,20 @@ You will probably also want to set the `onAuthChange`, `onCancel`, and `onDisabl

```swift
pscope.onAuthChange = { (finished, results) in
println("Request was finished with results \(results)")
print("Request was finished with results \(results)")
if results[0].status == .Authorized {
println("They've authorized the use of notifications")
print("They've authorized the use of notifications")
UIApplication.sharedApplication().registerForRemoteNotifications()
}
}
pscope.onCancel = { results in
println("Request was cancelled with results \(results)")
print("Request was cancelled with results \(results)")
}
pscope.onDisabledOrDenied = { results in
println("Request was denied or disabled with results \(results)")
print("Request was denied or disabled with results \(results)")
}
pscope.onDismissed = { canceled in
print("The controller was dismissed by cancel action = \(canceled)")
}
```

Expand Down