Platforms to show: All Mac Windows Linux Cross-Platform

/Swift/StoreKit2/StoreKit2 iOS


Last modified Wed, 14th Jul 2026.

You find this example project in your MBS Xojo Plugin download as a Xojo project file within the examples folder: /Swift/StoreKit2/StoreKit2 iOS

Download this example: StoreKit2 iOS.zip

Project "StoreKit2 iOS.xojo_binary_project"
Class App Inherits MobileApplication
EventHandler Sub Opening() End EventHandler
End Class
Class MainScreen Inherits MobileScreen
Control LogTable Inherits iOSMobileTable
ControlInstance LogTable Inherits iOSMobileTable
Constraint Constraint 1
Constraint Constraint 2
Constraint Constraint 3
Constraint Constraint 4
End Control
Control PurchaseButton Inherits MobileButton
ControlInstance PurchaseButton Inherits MobileButton
Constraint Constraint 1
Constraint Constraint 2
Constraint Constraint 3
Constraint Constraint 4
EventHandler Sub Pressed() Log "Purchase..." Var purchaseOptions As New Dictionary purchaseOptions.value("quantity") = 2 'purchaseOptions.value("appAccountToken") = "xxx" 'purchaseOptions.value("simulatesAskToBuyInSandbox") = True 'purchaseOptions.value("customkey") = "customvalue" StoreKit2MBS.PurchaseProduct("test1", purchaseOptions, AddressOf PurchaseCompleted) End EventHandler
End Control
Control SyncButton Inherits MobileButton
ControlInstance SyncButton Inherits MobileButton
Constraint Constraint 1
Constraint Constraint 2
Constraint Constraint 3
Constraint Constraint 4
EventHandler Sub Pressed() Log "Sync..." StoreKit2MBS.Sync AddressOf syncCompleted End EventHandler
End Control
Control ReviewButton Inherits MobileButton
ControlInstance ReviewButton Inherits MobileButton
Constraint Constraint 1
Constraint Constraint 2
Constraint Constraint 3
Constraint Constraint 4
EventHandler Sub Pressed() Log "Review..." StoreKit2MBS.RequestReview End EventHandler
End Control
EventHandler Sub Opening() Module1.theMainScreen = Self System.DebugLog "Start..." StoreKit2MBS.QueryProducts Array("test1"), AddressOf QueryProductsCompleted StoreKit2MBS.SetUpdates AddressOf updatesReceived StoreKit2MBS.CurrentEntitlements AddressOf CurrentEntitlementsCompleted End EventHandler
Sub CurrentEntitlementsCompleted(errorMessage as String, transactions() as Dictionary) Log CurrentMethodName If errorMessage <> "" Then Log "Failed to get updates: "+errorMessage End If If transactions <> Nil Then Var today As DateTime = DateTime.Now For Each transaction As Dictionary In transactions Var id As String = transaction.Value("id") Var productID As String = transaction.Value("productID") Var purchaseDate As DateTime = transaction.Value("purchaseDate") Var purchasedQuantity As Integer = transaction.Value("purchasedQuantity") Var isUpgraded As Boolean = transaction.Value("isUpgraded") If transaction.HasKey("verified") Then Var verified As Boolean = transaction.value("verified") If Not verified Then Continue // ignore End If Var revocationDate As DateTime = transaction.Lookup("revocationDate", Nil) If revocationDate <> Nil Then // Remove access to the product identified by transaction.productID. // Transaction.revocationReason provides details about // the revoked transaction. Continue End If Var expirationDate As DateTime = transaction.Lookup("expirationDate", Nil) If expirationDate < today Then // Do nothing, this subscription is expired. Continue End If If isUpgraded Then // Do nothing, there is an active transaction for a higher level of service. Continue End If // Provide access to the product identified by transaction.productID. If productID = "test1" Then test1Purchased = True end if Break // see in debugger Next End If End Sub
Sub Log(s as string) LogTable.AddRow s End Sub
Sub PurchaseCompleted(errorMessage as String, result as Dictionary) Log CurrentMethodName If errorMessage <> "" Then Log "Error: "+errorMessage End If var status As String = result.lookup("result", "") Var verified As Boolean = result.Lookup("verified", False) Var transaction As Dictionary = result.Lookup("transaction", Nil) var verificationError as string = result.Lookup("verificationError", "") Select Case status Case "success" // Successful purchase If verified Then // okay Var id As String = transaction.Value("id") Var productID As String = transaction.Value("productID") If verified Then // Provide access to the product identified by transaction.productID. If productID = "test1" Then test1Purchased = True PurchaseButton.Enabled = false End If End If Else // Successful purchase but transaction/receipt can't be verified // Could be a jailbroken phone end if case "pending" // Transaction waiting on SCA (Strong Customer Authentication) or approval from Ask to Buy Case "userCancelled" Case "unknown" case "failed" end Select Break End Sub
Sub QueryProductsCompleted(errorMessage as String, products() as Dictionary) Log CurrentMethodName If errorMessage <> "" Then Log "Failed to query products: "+errorMessage End If If products <> Nil Then For Each product As Dictionary In products Var id As String = product.Value("id") Var displayName As String = product.Value("displayName") Var description As String = product.Value("description") Var price As double = product.Value("price") Var displayPrice As String = product.Value("displayPrice") Var isFamilyShareable As boolean = product.Value("isFamilyShareable") Var type As Dictionary = product.Value("type") Var typeValue As String = type.Value("value") // iOS 15.4, macOS 12.3 or newer var typeDescription as string = type.Lookup("localizedDescription", "") Log displayName+": "+displayPrice If id = "test1" Then PurchaseButton.Enabled = True End If 'Break // see in debugger Next End If End Sub
Sub TransactionsCompleted(errorMessage as string, transactions() as Dictionary) Log CurrentMethodName If errorMessage <> "" Then Log "Error: "+errorMessage End If End Sub
Sub syncCompleted(ErrorMessage as string) Log CurrentMethodName if errorMessage <> "" then Log "Error: "+errorMessage End If End Sub
Sub updatesReceived(errorMessage as String, transactions() as Dictionary) Log CurrentMethodName If errorMessage <> "" Then Log "Failed to get updates: "+errorMessage End If If transactions <> Nil Then var today as DateTime = DateTime.now For Each transaction As Dictionary In transactions Var id As String = transaction.Value("id") var productID as string = transaction.Value("productID") var purchaseDate as DateTime = transaction.Value("purchaseDate") Var purchasedQuantity As Integer = transaction.Value("purchasedQuantity") var isUpgraded as Boolean = transaction.Value("isUpgraded") if transaction.HasKey("verified") then Var verified As Boolean = transaction.value("verified") If Not verified Then Continue // ignore end if Var revocationDate As DateTime = transaction.Lookup("revocationDate", Nil) If revocationDate <> Nil Then // Remove access to the product identified by transaction.productID. // Transaction.revocationReason provides details about // the revoked transaction. Continue end if Var expirationDate As DateTime = transaction.Lookup("expirationDate", Nil) If expirationDate < today Then // Do nothing, this subscription is expired. continue End If If isUpgraded Then // Do nothing, there is an active transaction for a higher level of service. Continue end if // Provide access to the product identified by transaction.productID. If productID = "test1" Then test1Purchased = True End If Break // see in debugger Next End If End Sub
Property e As NSExceptionMBS
End Class
Class LaunchScreen Inherits MobileScreen
End Class
Sign
End Sign
Sign
End Sign
Module Module1
Sub Log(s as string) System.DebugLog s theMainScreen.log s End Sub
Property test1Purchased As Boolean
Property theMainScreen As MainScreen
End Module
End Project

See also:

Download this example: StoreKit2 iOS.zip

The items on this page are in the following plugins: MBS Swift Plugin.


The biggest plugin in space...