Skip to content

Commit 5826099

Browse files
authored
Merge pull request #279 from cybex-dev/273-ios-unregister-fails-silently
273 [iOS] unregister() Fails Silently on Second Logout with Same Credentials
2 parents 65b69bb + 73a4d9e commit 5826099

File tree

2 files changed

+24
-9
lines changed

2 files changed

+24
-9
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
## 0.3.1
22

3+
* Fix: [iOS] unregister removes device push token preventing new access token registration (i.e. user 1 logs out, user 2 and more won't receive any calls). Thanks to [@VinceDollo](https://github.com/VinceDollo) & [@Erchil66](https://github.com/Erchil66) [Issue #273](https://github.com/cybex-dev/twilio_voice/issues/273)
34
* Fix: [Android] placing outgoing call then ending immediately results in no Flutter plugin not registering call ended. Thanks to [@RageshAntonyHM](https://github.com/RageshAntonyHM) & [@Erchil66](https://github.com/Erchil66) [Issue #275](https://github.com/cybex-dev/twilio_voice/issues/275)
45
* Fix: [Android] Migrate to declarative plugins block from Flutter's `app_plugin_loader` Gradle plugin. (see: [Deprecated imperative apply of Flutter's Gradle plugins](https://docs.flutter.dev/release/breaking-changes/flutter-gradle-plugin-apply))
56
* Docs: update CHANGELOG, TODO

ios/Classes/SwiftTwilioVoicePlugin.swift

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,16 @@ public class SwiftTwilioVoicePlugin: NSObject, FlutterPlugin, FlutterStreamHand
110110
let arguments:Dictionary<String, AnyObject> = flutterCall.arguments as! Dictionary<String, AnyObject>;
111111

112112
if flutterCall.method == "tokens" {
113-
guard let token = arguments["accessToken"] as? String else {return}
114-
self.accessToken = token
115-
if let deviceToken = deviceToken, let token = accessToken {
113+
guard let token = arguments["accessToken"] as? String else {
114+
result(FlutterError(code: "INVALID_ARGUMENTS", message: "Missing accessToken", details: nil))
115+
return
116+
}
117+
self.accessToken = token;
118+
guard let deviceToken = deviceToken else {
119+
self.sendPhoneCallEvents(description: "LOG|Device token is nil. Cannot register for VoIP push notifications.", isError: true)
120+
return
121+
}
122+
if let token = accessToken {
116123
self.sendPhoneCallEvents(description: "LOG|pushRegistry:attempting to register with twilio", isError: false)
117124
TwilioVoiceSDK.register(accessToken: token, deviceToken: deviceToken) { (error) in
118125
if let error = error {
@@ -429,8 +436,12 @@ public class SwiftTwilioVoicePlugin: NSObject, FlutterPlugin, FlutterStreamHand
429436
return
430437
}
431438

432-
guard registrationRequired() || deviceToken != credentials.token else { return }
439+
guard registrationRequired() || deviceToken != credentials.token else {
440+
self.sendPhoneCallEvents(description: "LOG|pushRegistry:didUpdatePushCredentials device token unchanged, no update needed.", isError: true)
441+
return
442+
}
433443

444+
self.sendPhoneCallEvents(description: "LOG|pushRegistry:didUpdatePushCredentials:forType: device token updated", isError: false)
434445
let deviceToken = credentials.token
435446

436447
self.sendPhoneCallEvents(description: "LOG|pushRegistry:attempting to register with twilio", isError: false)
@@ -457,16 +468,18 @@ public class SwiftTwilioVoicePlugin: NSObject, FlutterPlugin, FlutterStreamHand
457468
* will return true, else false.
458469
*/
459470
func registrationRequired() -> Bool {
460-
guard
461-
let lastBindingCreated = UserDefaults.standard.object(forKey: kCachedBindingDate)
462-
else { return true }
471+
guard let lastBindingCreated = UserDefaults.standard.object(forKey: kCachedBindingDate) else {
472+
self.sendPhoneCallEvents(description: "LOG|Registration required: true, last binding date not found", isError: false)
473+
return true
474+
}
463475

464476
let date = Date()
465477
var components = DateComponents()
466478
components.setValue(kRegistrationTTLInDays/2, for: .day)
467479
let expirationDate = Calendar.current.date(byAdding: components, to: lastBindingCreated as! Date)!
468480

469481
if expirationDate.compare(date) == ComparisonResult.orderedDescending {
482+
self.sendPhoneCallEvents(description: "LOG|Registration required: false, half of TTL not passed", isError: false)
470483
return false
471484
}
472485
return true;
@@ -485,6 +498,7 @@ public class SwiftTwilioVoicePlugin: NSObject, FlutterPlugin, FlutterStreamHand
485498
func unregister() {
486499

487500
guard let deviceToken = deviceToken, let token = accessToken else {
501+
self.sendPhoneCallEvents(description: "LOG|Missing required parameters to unregister", isError: true)
488502
return
489503
}
490504

@@ -499,10 +513,10 @@ public class SwiftTwilioVoicePlugin: NSObject, FlutterPlugin, FlutterStreamHand
499513
self.sendPhoneCallEvents(description: "LOG|Successfully unregistered from VoIP push notifications.", isError: false)
500514
}
501515
}
502-
UserDefaults.standard.removeObject(forKey: kCachedDeviceToken)
516+
//UserDefaults.standard.removeObject(forKey: kCachedDeviceToken)
503517

504518
// Remove the cached binding as credentials are invalidated
505-
UserDefaults.standard.removeObject(forKey: kCachedBindingDate)
519+
//UserDefaults.standard.removeObject(forKey: kCachedBindingDate)
506520
}
507521

508522
/**

0 commit comments

Comments
 (0)