Skip to content

Commit 38aa392

Browse files
authored
Merge pull request #291 from IFTTT/bugfix/event_registration_fixes2
Event registration fixes
2 parents ec6ee57 + 394c146 commit 38aa392

File tree

9 files changed

+152
-98
lines changed

9 files changed

+152
-98
lines changed

Examples/GroceryExpress/AppDelegate.swift

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,14 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
1919
private let connectionRedirectHandler = ConnectionRedirectHandler(redirectURL: AppDelegate.connectionRedirectURL)
2020

2121
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
22-
2322
ConnectButtonController.synchronizationLoggingEnabled = true
2423
ConnectButtonController.analyticsEnabled = true
2524
ConnectButtonController.initialize(options: .init(enableSDKBackgroundProcess: true, showPermissionsPrompts: true))
26-
ConnectButtonController.activate(connections: [DisplayInformation.locationConnection.connectionId])
25+
if ConnectionCredentials(settings: .init()).isLoggedIn {
26+
ConnectButtonController.activate(connections: [DisplayInformation.locationConnection.connectionId])
27+
} else {
28+
ConnectButtonController.deactivate()
29+
}
2730
ConnectButtonController.setBackgroundProcessClosures {
2831
print("Background process started!")
2932
} expirationHandler: {

Examples/GroceryExpress/ConnectionCredentials.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,17 @@ class ConnectionCredentials: ConnectionCredentialProvider, CustomStringConvertib
5858
Keys.token : token
5959
]
6060
UserDefaults.standard.set(user, forKey: Keys.user)
61+
ConnectButtonController.activate(
62+
connections: [DisplayInformation.locationConnection.connectionId],
63+
lifecycleSynchronizationOptions: .all
64+
)
6165
}
6266

6367
/// Clears the active IFTTT session
6468
func logout() {
6569
userToken = nil
6670
UserDefaults.standard.set(nil, forKey: Keys.user)
71+
ConnectButtonController.deactivate()
6772
}
6873

6974
/// Creates an instance of ConnectionCredentials

Examples/GroceryExpress/SettingsViewController.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ class SettingsViewController: UIViewController {
6060
}
6161
@IBAction func logoutTapped(_ sender: Any) {
6262
ConnectionCredentials(settings: settings).logout()
63-
ConnectButtonController.deactivate()
6463
update()
6564
}
6665

IFTTT SDK.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
B556033C25A851EE00A29A01 /* Localizable_cs.strings in Resources */ = {isa = PBXBuildFile; fileRef = B556032425A851EE00A29A01 /* Localizable_cs.strings */; };
5555
B556033D25A851EE00A29A01 /* Localizable_nl.strings in Resources */ = {isa = PBXBuildFile; fileRef = B556032525A851EE00A29A01 /* Localizable_nl.strings */; };
5656
DE25265623D8C49D0019C9CB /* Analytics.swift in Sources */ = {isa = PBXBuildFile; fileRef = DE25265523D8C49D0019C9CB /* Analytics.swift */; };
57+
DE260F6B26CAFC20004191D1 /* SynchronizationSchedulerTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = DE260F6A26CAFC20004191D1 /* SynchronizationSchedulerTests.swift */; };
5758
DE2906D3242BF66E00CC2825 /* Connection+Parsing.swift in Sources */ = {isa = PBXBuildFile; fileRef = DE2906D2242BF66E00CC2825 /* Connection+Parsing.swift */; };
5859
DE2F524A2429404200EF986A /* Connection+Location.swift in Sources */ = {isa = PBXBuildFile; fileRef = DE2F52492429404200EF986A /* Connection+Location.swift */; };
5960
DE2F524C242940AD00EF986A /* CLCircularRegion+Parsing.swift in Sources */ = {isa = PBXBuildFile; fileRef = DE2F524B242940AD00EF986A /* CLCircularRegion+Parsing.swift */; };
@@ -226,6 +227,7 @@
226227
B556032525A851EE00A29A01 /* Localizable_nl.strings */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.strings; name = Localizable_nl.strings; path = Resources/Localizable_nl.strings; sourceTree = "<group>"; };
227228
DE1712BC2565B295000B13E6 /* RegionEventsRegistryTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RegionEventsRegistryTests.swift; sourceTree = "<group>"; };
228229
DE25265523D8C49D0019C9CB /* Analytics.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Analytics.swift; sourceTree = "<group>"; };
230+
DE260F6A26CAFC20004191D1 /* SynchronizationSchedulerTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SynchronizationSchedulerTests.swift; sourceTree = "<group>"; };
229231
DE2906D2242BF66E00CC2825 /* Connection+Parsing.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "Connection+Parsing.swift"; sourceTree = "<group>"; };
230232
DE2F52492429404200EF986A /* Connection+Location.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Connection+Location.swift"; sourceTree = "<group>"; };
231233
DE2F524B242940AD00EF986A /* CLCircularRegion+Parsing.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "CLCircularRegion+Parsing.swift"; sourceTree = "<group>"; };
@@ -439,6 +441,7 @@
439441
DEC29DE525840DE300BF56EE /* LocationServiceTests.swift */,
440442
DEC29EE7258419FC00BF56EE /* Info.plist */,
441443
DEF4A4962587BA1A00735E98 /* ArrayHelpersTests.swift */,
444+
DE260F6A26CAFC20004191D1 /* SynchronizationSchedulerTests.swift */,
442445
);
443446
path = SDKHostAppTests;
444447
sourceTree = "<group>";
@@ -864,6 +867,7 @@
864867
DEC29F0125841A0800BF56EE /* String_EmailDataDetectorTests.swift in Sources */,
865868
DEC29F0525841A0800BF56EE /* RegionsMonitorTests.swift in Sources */,
866869
DEC29F0625841A0800BF56EE /* Connection_ParsingTests.swift in Sources */,
870+
DE260F6B26CAFC20004191D1 /* SynchronizationSchedulerTests.swift in Sources */,
867871
DEF4A4972587BA1A00735E98 /* ArrayHelpersTests.swift in Sources */,
868872
DEC29F0225841A0800BF56EE /* EventPublisherTests.swift in Sources */,
869873
);

IFTTT SDK/ConnectButtonController+Public.swift

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -70,25 +70,24 @@ extension ConnectButtonController {
7070
}
7171
}
7272

73-
/// Performs setup of the SDK. Starts the synchronization of in the SDK. Registers background process with the system if desired.
73+
/// Performs setup of the SDK.Activated synchronization
7474
///
7575
/// - Parameters:
7676
/// - credentials: An optional object conforming to `ConnectionCredentialProvider` which is used to setup the SDK. If this is nil, the SDK will attempt to use cached values.
77-
/// - lifecycleSynchronizationOptions: An instance of `ApplicationLifecycleSynchronizationOptions` that defines which app lifecycle events the synchronization should occur on. If this parameter is not set, a default value of `ApplicationLifecycleSynchronizationOptions.all` will be used.
78-
public static func setup(with credentials: ConnectionCredentialProvider?,
79-
lifecycleSynchronizationOptions: ApplicationLifecycleSynchronizationOptions = .all) {
77+
public static func setup(with credentials: ConnectionCredentialProvider?) {
8078
if let credentials = credentials {
8179
Keychain.update(with: credentials)
8280
}
83-
ConnectionsSynchronizer.shared.setup(lifecycleSynchronizationOptions: lifecycleSynchronizationOptions)
8481
}
8582

8683
/// Call this method to activate the synchronization. This starts synchronization for the parameter connections.
8784
///
8885
/// - Parameters:
8986
/// - connections: An optional list of `Connection` to activate synchronization with.
90-
public static func activate(connections ids: [String]? = nil) {
91-
ConnectionsSynchronizer.shared.activate(connections: ids)
87+
/// - lifecycleSynchronizationOptions: An instance of `ApplicationLifecycleSynchronizationOptions` that defines which app lifecycle events the synchronization should occur on. If this parameter is not set, a default value of `ApplicationLifecycleSynchronizationOptions.all` will be used.
88+
public static func activate(connections ids: [String]? = nil,
89+
lifecycleSynchronizationOptions: ApplicationLifecycleSynchronizationOptions = .all) {
90+
ConnectionsSynchronizer.shared.activate(connections: ids, lifecycleSynchronizationOptions: lifecycleSynchronizationOptions)
9291
}
9392

9493
/// Call this method to deactivate the synchronization of connection and native service data. This stops synchronization and performs cleanup of any stored data. This will also remove any registered geofences.

IFTTT SDK/ConnectionsSynchronizer.swift

Lines changed: 8 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -133,14 +133,6 @@ final class ConnectionsSynchronizer {
133133
location.start()
134134
}
135135

136-
/// Performs basic setup of the SDK with Application lifecycle synchronization options.
137-
///
138-
/// - Parameters:
139-
/// - lifecycleSynchronizationOptions: The synchronization options to use in setting up App Lifecycle notification observers.
140-
func setup(lifecycleSynchronizationOptions: ApplicationLifecycleSynchronizationOptions) {
141-
scheduler.setup(lifecycleSynchronizationOptions: lifecycleSynchronizationOptions)
142-
}
143-
144136
/// Can be used to force a synchronization.
145137
///
146138
/// - Parameters:
@@ -151,19 +143,20 @@ final class ConnectionsSynchronizer {
151143
eventPublisher.onNext(event)
152144
}
153145

154-
/// Used to start the synchronization with an optional list of connection ids to monitor.
146+
/// Used to start the synchronization.
155147
///
156148
/// - Parameters:
157149
/// - connections: An optional list of connections to start monitoring.
158-
func activate(connections ids: [String]? = nil) {
150+
/// - lifecycleSynchronizationOptions: The app lifecycle synchronization options to use with the scheduler
151+
func activate(connections ids: [String]? = nil, lifecycleSynchronizationOptions: ApplicationLifecycleSynchronizationOptions) {
152+
start(lifecycleSynchronizationOptions: lifecycleSynchronizationOptions)
153+
update(isActivation: true)
159154
if let ids = ids {
160155
registry.addConnections(with: ids, shouldNotify: false)
161156
ConnectButtonController.synchronizationLog("Activated synchronization with connection ids: \(ids)")
162157
} else {
163158
ConnectButtonController.synchronizationLog("Activated synchronization")
164159
}
165-
start()
166-
update(isActivation: true)
167160
}
168161

169162
/// Used to deactivate and stop synchronization.
@@ -174,19 +167,17 @@ final class ConnectionsSynchronizer {
174167
}
175168

176169
/// Call this to start the synchronization. Safe to be called multiple times.
177-
private func start() {
170+
private func start(lifecycleSynchronizationOptions: ApplicationLifecycleSynchronizationOptions) {
178171
if state == .running { return }
179172

180-
setupNotifications()
181173
performPreflightChecks()
182174
Keychain.resetIfNecessary(force: false)
183-
scheduler.start()
175+
scheduler.start(lifecycleSynchronizationOptions: lifecycleSynchronizationOptions)
184176
state = .running
185177
}
186178

187179
/// Call this to stop the synchronization completely. Safe to be called multiple times.
188180
private func stop() {
189-
stopNotifications()
190181
Keychain.resetIfNecessary(force: true)
191182
scheduler.stop()
192183
state = .stopped
@@ -198,22 +189,7 @@ final class ConnectionsSynchronizer {
198189
ConnectButtonController.synchronizationLog("Background location not enabled for this target! Enable background location to allow location updates to be delivered to the app in the background.")
199190
}
200191
}
201-
202-
/// Peforms internal setup to allow the SDK to perform work in response to notification center notifications.
203-
private func setupNotifications() {
204-
if #available(iOS 13.0, *) {
205-
NotificationCenter.default.addObserver(self,
206-
selector: #selector(applicationDidEnterBackground),
207-
name: UIApplication.didEnterBackgroundNotification,
208-
object: nil)
209-
}
210-
}
211-
212-
/// Stops notification observation
213-
private func stopNotifications() {
214-
NotificationCenter.default.removeObserver(self)
215-
}
216-
192+
217193
private func setupRegistryNotifications() {
218194
NotificationCenter.default.addObserver(forName: .UpdateConnectionsName,
219195
object: nil,

IFTTT SDK/SynchronizationManager.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ final class SynchronizationManager {
3636
/// Creates a `SyncManager`
3737
///
3838
/// - Parameter subscribers: The `SyncSubscribers to perform during syncs
39-
init(subscribers: Array<SynchronizationSubscriber>) {
39+
init(subscribers: [SynchronizationSubscriber]) {
4040
self.subscribers = subscribers
4141
}
4242

@@ -126,7 +126,7 @@ extension SynchronizationManager {
126126
finish()
127127
}
128128

129-
private(set) var subscribers: Array<SynchronizationSubscriber> = []
129+
private(set) var subscribers = [SynchronizationSubscriber]()
130130

131131
private var resultsBySubscriber: [String : UIBackgroundFetchResult] = [:]
132132

0 commit comments

Comments
 (0)