-
Notifications
You must be signed in to change notification settings - Fork 7
/
WatchConnectivityManager.swift
151 lines (116 loc) · 6.55 KB
/
WatchConnectivityManager.swift
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*
Copyright (C) 2016 Apple Inc. All Rights Reserved.
See LICENSE.txt for this sample’s licensing information
Abstract:
This file contains the `WatchConnectivityManager` class and its delegate which encapsulate the `WatchConnectivity` behavior of the app.
*/
import WatchConnectivity
/**
The `WatchConnectivityManagerDelegate` protocol enables a `WatchConnectivityManager` object to notify another
object of changes to the activation state of the default `WCSession`. On iOS the receiver is provided with the
designator and color selected by the user to identify the watch associated with the default session. On the
Watch app the receiver is provided with any new morse codes.
*/
protocol WatchConnectivityManagerPhoneDelegate: class {
func watchConnectivityManager(_ watchConnectivityManager: WatchConnectivityManager, updateWithRoutine routineType: String, Routine: [String:String], amPM: String)
}
protocol WatchConnectivityManagerWatchDelegate: class {
func watchConnectivityManager(_ watchConnectivityManager: WatchConnectivityManager, updateWithPetList petList: [String:Any])
func watchConnectivityManager(_ watchConnectivityManager: WatchConnectivityManager, updateWithRoutine routine: [String:Any])
func watchConnectivityManager(_ watchConnectivityManager: WatchConnectivityManager, deletePet pet: [String:Any])
func watchConnectivityManager(_ watchConnectivityManager: WatchConnectivityManager, updateRoutine routine: [String:Any])
}
class WatchConnectivityManager: NSObject, WCSessionDelegate {
// MARK: Static Properties
static let sharedConnectivityManager = WatchConnectivityManager()
// MARK: Properties
#if os(iOS)
weak var delegate: WatchConnectivityManagerPhoneDelegate?
#else
weak var delegate: WatchConnectivityManagerWatchDelegate?
#endif
// MARK: Initialization
private override init() {
super.init()
}
// MARK: Convenience
func configureDeviceDetailsWithApplicationContext(applicationContext: [String: Any]) {
#if os(iOS)
// Extract relevant values from the application context.
guard let routineType = applicationContext["routineType"] as? String, let routine = applicationContext["Routine"] as? [String:String], let amPM = applicationContext["amPM"] as? String else {
// If the expected values are unavailable in the `applicationContext`, inform the delegate using default values.
delegate?.watchConnectivityManager(self, updateWithRoutine: "-", Routine: [:], amPM: "-")
return
}
// Inform the delegate.
delegate?.watchConnectivityManager(self, updateWithRoutine: routineType, Routine: routine, amPM: amPM)
#endif
}
// MARK: WCSessionDelegate
func session(_ session: WCSession, didReceiveApplicationContext applicationContext: [String : Any]) {
print("session (in state: \(session.activationState.rawValue)) received application context \(applicationContext)")
configureDeviceDetailsWithApplicationContext(applicationContext: applicationContext)
// NOTE: The guard is here as `watchDirectoryURL` is only available on iOS and this class is used on both platforms.
#if os(iOS)
print("session watch directory URL: \(session.watchDirectoryURL?.absoluteString)")
#endif
#if os(watchOS)
delegate?.watchConnectivityManager(self, updateWithPetList: applicationContext)
#endif
}
func session(_ session: WCSession, didReceiveUserInfo userInfo: [String : Any] = [:]) {
#if os(watchOS)
if userInfo["TypeSended"] as! String == "Pet" {
delegate?.watchConnectivityManager(self, updateWithPetList: userInfo)
} else if userInfo["TypeSended"] as! String == "Routine" {
delegate?.watchConnectivityManager(self, updateWithRoutine: userInfo)
} else if userInfo["TypeSended"] as! String == "Delete"{
delegate?.watchConnectivityManager(self, deletePet: userInfo)
} else if userInfo["TypeSended"] as! String == "RoutineUpdate" {
delegate?.watchConnectivityManager(self, updateRoutine: userInfo)
}
#endif
}
func session(_ session: WCSession, didFinish userInfoTransfer: WCSessionUserInfoTransfer, error: Error?) {
if let error = error {
print("\(error.localizedDescription)")
} else {
print("completed transfer of \(userInfoTransfer)")
}
}
// MARK: WCSessionDelegate - Asynchronous Activation
// The next method is required in order to support asynchronous session activation as well as for quick watch switching.
func session(_ session: WCSession, activationDidCompleteWith activationState: WCSessionActivationState, error: Error?) {
if let error = error {
print("session activation failed with error: \(error.localizedDescription)")
return
}
print("session activated with state: \(activationState.rawValue)")
configureDeviceDetailsWithApplicationContext(applicationContext: session.receivedApplicationContext)
// NOTE: The guard is here as `watchDirectoryURL` is only available on iOS and this class is used on both platforms.
#if os(iOS)
print("session watch directory URL: \(session.watchDirectoryURL?.absoluteString)")
#endif
}
#if os(iOS)
// The next 2 methods are required in order to support quick watch switching.
func sessionDidBecomeInactive(_ session: WCSession) {
/*
The `sessionDidBecomeInactive(_:)` callback indicates sending has been disabled. If your iOS app
sends content to its Watch extension it will need to stop trying at this point. This sample
doesn’t send content to its Watch Extension so no action is required for this transition.
*/
print("session did become inactive")
}
func sessionDidDeactivate(_ session: WCSession) {
print("session did deactivate")
/*
The `sessionDidDeactivate(_:)` callback indicates `WCSession` is finished delivering content to
the iOS app. iOS apps that process content delivered from their Watch Extension should finish
processing that content and call `activateSession()`. This sample immediately calls
`activateSession()` as the data provided by the Watch Extension is handled immediately.
*/
session.activate()
}
#endif
}