 import Foundation
 import SafariServices
 
-@objc(SpotifyOAuthPlugin) class SpotifyOAuthPlugin: CDVPlugin {
+@objc(SpotifyOAuthPlugin) class SpotifyOAuthPlugin: CDVPlugin, SFSafariViewControllerDelegate {
+    private var currentCommand: CDVInvokedUrlCommand?
+    private var currentNsObserver: AnyObject?
+    
     func authorize(_ command: CDVInvokedUrlCommand) {
         let auth = SPTAuth.defaultInstance()!
         

        auth.requestedScopes = command.argument(at: 3) as! Array
        
        let svc = SFSafariViewController(url: auth.spotifyWebAuthenticationURL())
        var observer: NSObjectProtocol?
        observer = NotificationCenter.default.addObserver(
            forName: NSNotification.Name.CDVPluginHandleOpenURL,

                    messageAs: [
                        "access_token": sess!.accessToken,
                        "encrypted_refresh_token": sess!.encryptedRefreshToken,
                        "expires_in": (sess!.expirationDate.timeIntervalSinceNow * 1000).rounded(.down)
                    ]
                )
                
