From 4eb155b36bc94f4bb72823e21bedb6a75eed3881 Mon Sep 17 00:00:00 2001 From: Hernan Zalazar Date: Mon, 28 Nov 2016 17:46:33 -0300 Subject: [PATCH] Allow developer to get controller to show lock. Always calls callback on success/critical error even if lock was not presented --- Lock/Router.swift | 14 ++++++++++---- LockTests/Router/RouterSpec.swift | 14 ++++++++++++++ 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/Lock/Router.swift b/Lock/Router.swift index 73f6ed66b..0e29c026a 100644 --- a/Lock/Router.swift +++ b/Lock/Router.swift @@ -50,11 +50,17 @@ class Router: Navigable { } } self.onAuthentication = { [weak controller] credentials in - Queue.main.async { - controller?.presentingViewController?.dismiss(animated: true, completion: { _ in - lock.callback(.success(credentials)) - }) + let closure: () -> () + if let presentingController = controller?.presentingViewController { + closure = { + presentingController.dismiss(animated: true, completion: { _ in + lock.callback(.success(credentials)) + }) + } + } else { + closure = { lock.callback(.success(credentials)) } } + Queue.main.async(closure) } self.onBack = { diff --git a/LockTests/Router/RouterSpec.swift b/LockTests/Router/RouterSpec.swift index 16c4917f2..41549dd5b 100644 --- a/LockTests/Router/RouterSpec.swift +++ b/LockTests/Router/RouterSpec.swift @@ -145,6 +145,20 @@ class RouterSpec: QuickSpec { } } + it("should call callback with auth result when lock was displayed without present") { + controller.presenting = nil + let credentials = Credentials(accessToken: "ACCESS_TOKEN", tokenType: "bearer") + waitUntil(timeout: 2) { done in + lock.callback = { result in + if case .success(let actual) = result { + expect(actual) == credentials + done() + } + } + router.onAuthentication(credentials) + } + } + describe("back") { beforeEach {