Introduction:
Welcome to the documentation for SwiftStoreKit, a lightweight In-App Purchases framework written in Swift. In this guide, you will learn how to integrate SwiftStoreKit into your iOS applications to enable seamless In-App Purchases functionality.
Installation:
Using CocoaPods:
- Open your terminal and navigate to your project directory.
- Initialize a Podfile by running the command
pod init
. - Add SwiftStoreKit to your Podfile:
pod 'SwiftStoreKit'
- Save the Podfile and run the command
pod install
to install the framework. - Make sure to open the project using the newly generated
.xcworkspace
file.
Manually:
- Visit the SwiftStoreKit repository on GitHub.
- Download the latest release zip file.
- Unzip the downloaded file and drag the
SwiftStoreKit.swift
file into your Xcode project. Make sure to select “Copy items if needed” when prompted.
Getting Started:
Initializing SwiftStoreKit:
To get started, import SwiftStoreKit into your Swift file:
// import SwiftStoreKit
import SwiftStoreKit
Verifying App Receipt:
Before making any In-App Purchase requests, it is essential to verify the app receipt to ensure its authenticity. Use the following method to verify the receipt:
SwiftStoreKit.verifyReceipt(using: sharedSecret) { result in
switch result {
case .success(let receipt):
// Receipt verification successful
print("Receipt verified: \(receipt)")
case .error(let error):
// Receipt verification failed
print("Receipt verification failed: \(error)")
}
}
Making a Purchase:
Once the app receipt is verified, you can proceed with making a purchase request. Use the following method to initiate a purchase request:
SwiftStoreKit.purchaseProduct(productId) { result in
switch result {
case .success(let purchase):
// Purchase successful
print("Purchase Success: \(purchase.productId)")
// Provide the user with the purchased content
case .error(let error):
// Purchase failed
print("Purchase Failed: \(error)")
}
}
Restoring Purchases:
In case a user wants to restore their previous purchases, use the following method:
SwiftStoreKit.restorePurchases() { results in
if results.restoreFailedPurchases.count > 0 {
// Restoring purchases failed for some products
print("Restore Failed: \(results.restoreFailedPurchases)")
}
else if results.restoredPurchases.count > 0 {
// All purchases restored successfully
print("Purchases Restored: \(results.restoredPurchases)")
}
else {
// No previous purchases found
print("Nothing to Restore")
}
}
Frequently Asked Questions (FAQ):
How do I test In-App Purchases in the sandbox environment?
Follow these steps:
- Create a Sandbox Tester Account on App Store Connect.
- In Xcode, go to “Signing & Capabilities” and add your Sandbox Tester Account.
- Ensure that you are using a sandbox build for testing.
- You can now make test purchases using the Sandbox Tester Account credentials.
How do I display localized pricing?
To display localized pricing, use the following method:
SwiftStoreKit.retrieveProductsInfo([productId]) { result in
if let product = result.retrievedProducts.first {
let priceString = product.localizedPrice
// Display the localized price to the user
}
}
Conclusion:
Congratulations! You have successfully integrated SwiftStoreKit into your iOS application. You can now provide users with a seamless In-App Purchases experience. For more advanced usage and additional functionality, make sure to refer to the official SwiftStoreKit repository documentation. Happy coding!