I want to display an activityIndicator when the user presses the iTunes buy button to finally purchase a subscription. There is a bit of time processing the purchase and soon as it's done I want to clear the activityIndicator.
I've tried different solutions with one of them using applicationWillResignActive to detect the iTunes pop up and this is detected right where user is is asked to type in the password. I have also posted a notification when products are requested but still the same thing. Is there a way to know when the iTunes buy button is pressed. Below is my code.
extension Notification.Name {
public static let AppDelegatePurchasesIsProccessing = NSNotification.Name("AppDelegatePurchasesIsProccessing")
}
func applicationWillResignActive(_ application: UIApplication) {
NotificationCenter.default.post(name: .AppDelegatePurchasesIsProccessing, object: self)
}
}
Overlay that is displayed to make purchases
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.notifyForUserIsPurchasingProduct {
self.activityIndicator.startAnimating()
}
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
self.notifyForUserHasPurchasedProduct {
self.activityIndicator.stopAnimating()
self.dismiss(animated: true)
}
}
func notifyForUserHasPurchasedProduct(_ action: @escaping () -> Void) {
NotificationCenter.default.addObserver(forName: .AppDelegateUserHasPurchasedProductNotification, object: nil, queue: nil) { (_) in
action()
}
}
func notifyForUserIsPurchasingProduct(_ action: @escaping () -> Void) {
NotificationCenter.default.addObserver(forName: .AppDelegatePurchasesIsProccessing, object: nil, queue: nil) { (_) in
action()
}
}