-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUserNotificationCondition.swift
More file actions
122 lines (93 loc) · 4.16 KB
/
UserNotificationCondition.swift
File metadata and controls
122 lines (93 loc) · 4.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/*
Copyright (C) 2015 Apple Inc. All Rights Reserved.
See LICENSE.txt for this sample’s licensing information
Abstract:
This file shows an example of implementing the OperationCondition protocol.
*/
#if os(iOS)
import UIKit
/**
A condition for verifying that we can present alerts to the user via
`UILocalNotification` and/or remote notifications.
*/
struct UserNotificationCondition: OperationCondition {
enum Behavior {
/// Merge the new `UIUserNotificationSettings` with the `currentUserNotificationSettings`.
case Merge
/// Replace the `currentUserNotificationSettings` with the new `UIUserNotificationSettings`.
case Replace
}
static let name = "UserNotification"
static let currentSettings = "CurrentUserNotificationSettings"
static let desiredSettings = "DesiredUserNotificationSettigns"
static let isMutuallyExclusive = false
let settings: UIUserNotificationSettings
let application: UIApplication
let behavior: Behavior
/**
The designated initializer.
- parameter settings: The `UIUserNotificationSettings` you wish to be
registered.
- parameter application: The `UIApplication` on which the `settings` should
be registered.
- parameter behavior: The way in which the `settings` should be applied
to the `application`. By default, this value is `.Merge`, which means
that the `settings` will be combined with the existing settings on the
`application`. You may also specify `.Replace`, which means the `settings`
will overwrite the exisiting settings.
*/
init(settings: UIUserNotificationSettings, application: UIApplication, behavior: Behavior = .Merge) {
self.settings = settings
self.application = application
self.behavior = behavior
}
func dependencyForOperation(operation: Operation) -> NSOperation? {
return UserNotificationPermissionOperation(settings: settings, application: application, behavior: behavior)
}
func evaluateForOperation(operation: Operation, completion: OperationConditionResult -> Void) {
let result: OperationConditionResult
let current = application.currentUserNotificationSettings()
switch (current, settings) {
case (let current?, let settings) where current.contains(settings):
result = .Satisfied
default:
let error = NSError(code: .ConditionFailed, userInfo: [
OperationConditionKey: self.dynamicType.name,
self.dynamicType.currentSettings: current ?? NSNull(),
self.dynamicType.desiredSettings: settings
])
result = .Failed(error)
}
completion(result)
}
}
/**
A private `Operation` subclass to register a `UIUserNotificationSettings`
object with a `UIApplication`, prompting the user for permission if necessary.
*/
private class UserNotificationPermissionOperation: Operation {
let settings: UIUserNotificationSettings
let application: UIApplication
let behavior: UserNotificationCondition.Behavior
init(settings: UIUserNotificationSettings, application: UIApplication, behavior: UserNotificationCondition.Behavior) {
self.settings = settings
self.application = application
self.behavior = behavior
super.init()
addCondition(AlertPresentation())
}
override func execute() {
dispatch_async(dispatch_get_main_queue()) {
let current = self.application.currentUserNotificationSettings()
let settingsToRegister: UIUserNotificationSettings
switch (current, self.behavior) {
case (let currentSettings?, .Merge):
settingsToRegister = currentSettings.settingsByMerging(self.settings)
default:
settingsToRegister = self.settings
}
self.application.registerUserNotificationSettings(settingsToRegister)
}
}
}
#endif