Ray
Ray

Reputation: 123

stopAnimating activity indicator from app delegate

I am having troubles stopping an activity indicator from a function in my app delegate, i know the function is getting called but i do not recieve any errors in my log.

I am creating the activityIndicator in my signInViewController like so

@IBAction func googleSignInButton(_ sender: Any) {

    GIDSignIn.sharedInstance().uiDelegate = self
    GIDSignIn.sharedInstance().signIn()


    activityIndicator.center = self.view.center
    activityIndicator.hidesWhenStopped = true
    activityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.white
    view.addSubview(activityIndicator)

    activityIndicator.startAnimating()
    UIApplication.shared.beginIgnoringInteractionEvents()


}

after this in my app delegate i have this function,

   func sign(_ signIn: GIDSignIn!, didSignInFor user: GIDGoogleUser!, withError error: Error?) {

            print("this function is running")
            SignInViewController().stopanimating()

            // ...
            if error != nil {
                // ...


                return
            }

I know this function is working fine as it prints the text in the log ect. and calls this function from the SignInViewController

func stopanimating() {
        print("stop animating function running")
        DispatchQueue.main.async {
            self.activityIndicator.stopAnimating()
            UIApplication.shared.endIgnoringInteractionEvents()
        }
    }

now i know this function is running as it also prints the expected text in the log and also the endIgnoringInteractionEvents does work but the activity indicator is still running

im quite new to swift but ive been having problems manipulating objects in viewcontrollers from my appdelegate before, is this possible?

Thanks in advance

Upvotes: 0

Views: 1017

Answers (4)

Marco Cardoso
Marco Cardoso

Reputation: 150

When you do:

SignInViewController().stopanimating()

What you're doing is creating a new SignInViewController and calling the method on it. What you want to do is get the existing one to stop the animation.

One way to do this is:

let vc = (GIDSignIn.sharedInstance().uiDelegate as! SignInViewController)
vc.stopanimating()

You might want to refactor that later, but this should get you on the right path!

Upvotes: 1

Aditya Srivastava
Aditya Srivastava

Reputation: 2650

What does this means -- SignInViewController(). It means that you are creating different instance of your SignInViewController and hence on this instance your Activity Indicator is not present.

Solution - First solution is to get the instance on which you showed the activity Indicator. This means you have to get currentViewController Instance in your case . Second solution is to move your func sign(_ signIn: GIDSignIn!, didSignInFor user: GIDGoogleUser!, withError error: Error?) delegate method to your SignInViewController class.

Upvotes: 1

ceekay
ceekay

Reputation: 1226

This is because you are creating a new instance of SignInViewController in appDelegate. That is SignInViewController().stopanimating(). You have to call same instance inorder to stop animating the activity indicator.

Upvotes: 2

Hitesh
Hitesh

Reputation: 896

You need to remove activity indicator from subview. Modify your stopanimating()

func stopanimating() {
        print("stop animating function running")
        DispatchQueue.main.async {
            self.activityIndicator.stopAnimating()
            self.activityIndicator.removeFromSuperview()
            UIApplication.shared.endIgnoringInteractionEvents()
        }
}

Upvotes: 0

Related Questions