0

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

Hitesh
  • 896
  • 1
  • 9
  • 22
Ray
  • 123
  • 11
  • first make sure the the order of execution is current . if you tried to disable the animation from your app delegate delegate in and async manner . it might get called befor the start animation . secondly , i might be wrong , but looks like you are using the class and not an instance of your view controller when trying to disable the animation. is that on purpose ? – Nevgauker Sep 17 '17 at 11:56

4 Answers4

2

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.

ceekay
  • 1,136
  • 13
  • 11
1

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.

Aditya Srivastava
  • 2,630
  • 2
  • 13
  • 23
1

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!

Marco Cardoso
  • 150
  • 1
  • 10
0

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()
        }
}
Hitesh
  • 896
  • 1
  • 9
  • 22