Search code examples
iosswiftin-app-purchaseapp-store

How to activate trial subscription of monthly subscription (auto-renewing)?


I created a monthly subscription (auto-renewing) and installed a trial of 14 days for it in the App Store Connect. How to activate it now? I want that immediately after registering the user in the application a window appears with a proposal to purchase a monthly subscription or take advantage of the 14-day free version. How to use it in code?


Solution

  • It doesn't matter whether or not your subscription has introductory period. You need to implement: 1. products fetching using SKProductsRequest:

    func loadProducts(){
        let request = SKProductsRequest.init(productIdentifiers: productIds)
        request.delegate = self
        request.start()
    }
    
    func productsRequest(_ request: SKProductsRequest, didReceive response: SKProductsResponse) {                
        products = response.products
    }
    
    
    1. initiate payment process of that product using SKPaymentQueue:
    let payment = SKPayment(product: product)
    SKPaymentQueue.default().add(payment)
    
    1. Handle each switch-case in updated transactions method:
    func paymentQueue(_ queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) {
            for transaction in transactions {
                switch (transaction.transactionState) {
                case .purchased:
                    SKPaymentQueue.default().finishTransaction(transaction)
                    // handle here
                    break
                case .failed:
                    SKPaymentQueue.default().finishTransaction(transaction)
                    // handle here
                    break
                case .restored:
                    SKPaymentQueue.default().finishTransaction(transaction)
                    // handle here
                    break
                case .deferred, .purchasing:
                    break
                default:
                    break
                }
            }
    

    More details you can read in my article: https://blog.apphud.com/swift-tutorial-subscriptions/

    You can also use the SwiftyStoreKit library.