Created
April 25, 2019 02:17
-
-
Save rkotzy/87168233f81efa67b7847068cc12bfb3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@available(iOS 12.2, *) | |
func showPromoUpsell() { | |
Purchases.shared.entitlements { (entitlements, error) in | |
// 1: - Get the discount for the product we want to offer | |
// | |
guard let product = entitlements?["pro_cat"]?.offerings["monthly_cats"]?.activeProduct else { | |
print("Error finding monthly_cat product") | |
return | |
} | |
guard let productDiscount = product.discounts.first(where: { $0.identifier == "promo_cat" }) else { | |
print("Error finding promo_cat discount") | |
return | |
} | |
// 2: - Fetch the payment discount | |
// | |
Purchases.shared.paymentDiscount(for: productDiscount, product: product, completion: { (paymentDiscount, err) in | |
guard let discount = paymentDiscount else { | |
print("Payment discount doesn't exist. Check error.") | |
return | |
} | |
// 3: - Show the user the offer to purchase and save a flag | |
// in local storage so we don't show again | |
self.presentPromoAlert(productDiscount, shouldPurchase: { | |
Purchases.shared.makePurchase(product, discount: discount, { (transaction, info, error, cancelled) in | |
self.configureCatContentFor(purchaserInfo: info) | |
}) | |
}) | |
}) | |
} | |
} | |
// Helper function to present promo alert view | |
func presentPromoAlert(_ discount: SKProductDiscount, shouldPurchase: @escaping (() -> Void)) { | |
let price = discount.price | |
let promoAlert = UIAlertController( | |
title: "Thanks for being a loyal user!", | |
message: "We'd like to offer you an exclusive deal of only \(price) for 3-months of pro access.", | |
preferredStyle: .alert) | |
promoAlert.addAction(UIAlertAction( | |
title: "No thanks", | |
style: .cancel, | |
handler: nil)) | |
promoAlert.addAction(UIAlertAction( | |
title: "Sure!", | |
style: .default, | |
handler: { _ in | |
shouldPurchase() | |
})) | |
UserDefaults.standard.set(true, forKey: "has_seen_promo_cat_discount") | |
self.present(promoAlert, animated: true, completion: nil) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment