Skip to content

Instantly share code, notes, and snippets.

import UIKit
import Purchases
class CatsViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// 1. Add an observer for our kPurchaserInfoUpdated notification that triggers a function to update the view
import Purchases
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// 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)
})
})
// ... continued
// 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
}
// ... continued
@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
func shouldShowSubscriptionOffer(_ purchaserInfo: PurchaserInfo) -> Bool {
// Check that the user has a 'pro_cat' entitlement that expired >= 7 days ago
// and check that we haven't previously shown this offer to the user
if (purchaserInfo.expirationDate(forEntitlement: "pro_cat") ?? Date()).addingTimeInterval(7*86400) <= Date() &&
!UserDefaults.standard.bool(forKey: "has_seen_promo_cat_discount")
{
return true
}
func configureCatContentFor(purchaserInfo: PurchaserInfo?) {
if let purchaserInfo = purchaserInfo {
if purchaserInfo.activeEntitlements.contains("pro_cat") {
// ...
// ...
// ...
} else {
@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
@rkotzy
rkotzy / BuildConfig.swift
Created March 1, 2019 18:55
Determine what environment your iOS app is running in
import Foundation
enum AppConfiguration : String {
case debug = "debug"
case testFlight = "testFlight"
case appStore = "appStore"
}
struct Config {
// This is private because the use of 'appConfiguration' is preferred.
@rkotzy
rkotzy / notifications.swift
Created August 2, 2017 16:51
Fire Local Notifications in Swift
func fireNotification(title: String?, body: String?, id: String) {
let content = UNMutableNotificationContent()
if let title = title {
content.title = title
}
if let body = body {
content.body = body != "" ? body : "arrival"
}
content.sound = UNNotificationSound.default()