2

I am trying to implement a UIActivityIndicatorView that runs while the user is in the middle of an in app purchase. For some reason the UIActivityIndicatorView is not showing up even though I have made if a subview of the view.

class RemoveAdsViewController: UIViewController {

@IBAction func btnAdRemoval(sender: UIButton) {
    let buyProgress = UIActivityIndicatorView(activityIndicatorStyle: .White)
    buyProgress.center = self.view.center
    self.view.addSubview(buyProgress)
    buyProgress.startAnimating()
    print(buyProgress)
    PFPurchase.buyProduct("", block: { (error:NSError?) -> Void in
        if error != nil{
            let alert = UIAlertController(title: "Error", message: error?.localizedDescription, preferredStyle: UIAlertControllerStyle.Alert)

            alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))

            self.presentViewController(alert, animated: true, completion: nil)
        }
    })
    buyProgress.stopAnimating()
    buyProgress.removeFromSuperview()
}

PFRestore:

restoreProgress.startAnimating()
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), {
       PFPurchase.restore()
       dispatch_async(dispatch_get_main_queue(), {
           restoreProgress.stopAnimating()
       })
 })
2
  • See all of the Related questions shown to the right --> You should check those. This issue has been asked and answered before. Commented Oct 20, 2015 at 22:19
  • 1
    @rmaddy I've checked them already and the answers did not solve my problem which is why I posted my own question. Commented Oct 20, 2015 at 22:19

2 Answers 2

2

After taking another look, the problem is simple. You stop and remove the activity indicator much too soon. You need to stop and remove it in the completion block.

@IBAction func btnAdRemoval(sender: UIButton) {
    let buyProgress = UIActivityIndicatorView(activityIndicatorStyle: .White)
    buyProgress.center = self.view.center
    self.view.addSubview(buyProgress)
    buyProgress.startAnimating()
    print(buyProgress)
    PFPurchase.buyProduct("", block: { (error:NSError?) -> Void in
        buyProgress.stopAnimating()
        buyProgress.removeFromSuperview()

        if error != nil{
            let alert = UIAlertController(title: "Error", message: error?.localizedDescription, preferredStyle: UIAlertControllerStyle.Alert)

            alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))

            self.presentViewController(alert, animated: true, completion: nil)
        }
    })
}

You also need to make sure that the contents of the completion block is being done on the main thread.

Sign up to request clarification or add additional context in comments.

4 Comments

This does get the activity indicator to show up, but after the transaction is completed the UIActivityIndicatorView remains spinning, because there's no error
I put it in the wrong place, my mistake. Thank you.
I attempted to so something similar for PFPurchase using the link you had showed me initially but I still run into the same problem. Could you take a look? I updated the OP.
Looks like PFPurchase.restore() returns immediately.
1

The problem is you do this

buyProgress.startAnimating()

followed by this immediatly

buyProgress.stopAnimating()

because PFPurchase.buyProduct is an async call it will return immediately and your not seeing your activity indicator animate as its all happening in one run loop cycle.

you need to move

buyProgress.stopAnimating()

inside the closure like so

PFPurchase.buyProduct("", block: { (error:NSError?) -> Void in
            if error != nil{
                let alert = UIAlertController(title: "Error", message: error?.localizedDescription, preferredStyle: UIAlertControllerStyle.Alert)
                buyProgress.stopAnimating()

                alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))

                self.presentViewController(alert, animated: true, completion: nil)
            }
        })

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.