Wait in Appdelegate::application end of a callback method from Viewcontroller to end

In my ViewController I have this method

func runOauth(){
        self.loadingLabel.isHidden=true
        let appDelegate = UIApplication.shared.delegate as! AppDelegate
        appDelegate.oauth2!.afterAuthorizeOrFail = self.callBackOAuth

        var url:URL?
        do{
            //the url for authorizing the user, kronos://oauth/callback" is called after the OAuth finish
            url = try appDelegate.oauth2!.authorizeURL(withRedirect:"kronos://oauth/callback", scope: "auth",params: ["tg":"addon/kronos/main","idx":"login.OAuth","formId":"iOS"])

            do{
                let authorizer = appDelegate.oauth2!.authorizer as! OAuth2Authorizer
                safariVC = try authorizer.authorizeSafariEmbedded(from: self,at: url!)

            }catch let error {
                DispatchQueue.main.async {
                    print("ERROR authorizing\(error)")
                }
            }
        }catch let error {
            DispatchQueue.main.async {
                print("ERROR creating OAuth URL \(error)")
            }
-        }
   }

This afterAuthorizeOrFail method is called in background

func callBackOAuth(authParameters:OAuth2JSON!, error: OAuth2Error!){
        let appDelegate = UIApplication.shared.delegate as! AppDelegate

        if (error ==  nil && appDelegate.oauth2!.accessToken != nil){
            //OAuth succeed in
            //we store the token and its experation date in keychain

            self.keychain!.set(appDelegate.oauth2!.accessToken!,forKey:"Token")
            let formatter = DateFormatter()
            formatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
            let myString = formatter.string(from: appDelegate.oauth2!.accessTokenExpiry!)
            self.keychain!.set(myString,forKey:"ExpiryDate")
            appDelegate.reloadView()

        }else if (error !=  nil){//OAUth failed
            print("OAuth error \(String(describing: error))")
        }else{//Another error
            print("Cannot login")
            self.showMessage(msg: "Login error", title: "Error")
            self.runOauth()
        }

    }

And I want to wait the end of callBackOAuth to call handleRedirectURL here in AppDelegate

func application(_ app: UIApplication,
                  open url: URL,
                  options: [UIApplication.OpenURLOptionsKey: Any] = [:]) -> Bool {
        let components = URLComponents(url: url, resolvingAgainstBaseURL: false)
        let site=components?.host

        print("Application")
        if site == "oauth"{//OAuth terminated
            if components?.path == "/callback" {
                let viewController = self.window?.rootViewController as! ViewController
                print("oauth")
                self.oauth2!.handleRedirectURL(url)
                viewController.hideSafariView()
            }
        }else if site == "logoff"{//User logoff
            print("logoff")            
        }
        return true
    }

Does someone has a solution?

I've a solution by deporting the call of the URL which logs the user (with the Oauth token stored in keychain) from viewDidAppear to viewDidLoad

Wait in Appdelegate::application end of a callback method from Viewcontroller to end
 
 
Q