changeset 12:ce7ea84f67f5

display product prices
author Dennis C. M. <dennis@denniscm.com>
date Sun, 09 Oct 2022 19:46:44 +0200
parents 039b26a99a48
children bdfff35dd43c
files GeoQuiz.xcodeproj/project.pbxproj GeoQuiz/BuyPremiumModalView.swift GeoQuiz/GeoQuizApp.swift GeoQuiz/Info.plist GeoQuiz/Logic/StoreKitRCClass.swift
diffstat 5 files changed, 67 insertions(+), 18 deletions(-) [+]
line wrap: on
line diff
--- a/GeoQuiz.xcodeproj/project.pbxproj	Sun Oct 09 17:02:34 2022 +0200
+++ b/GeoQuiz.xcodeproj/project.pbxproj	Sun Oct 09 19:46:44 2022 +0200
@@ -13,6 +13,7 @@
 		950C535328F2FA3300179C78 /* BuyPremiumModalView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 950C535228F2FA3300179C78 /* BuyPremiumModalView.swift */; };
 		950C535628F3172C00179C78 /* RevenueCat in Frameworks */ = {isa = PBXBuildFile; productRef = 950C535528F3172C00179C78 /* RevenueCat */; };
 		950C535928F3178B00179C78 /* StoreKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 950C535828F3178B00179C78 /* StoreKit.framework */; };
+		95197EFD28F339AE00FE67E9 /* StoreKitRCClass.swift in Sources */ = {isa = PBXBuildFile; fileRef = 95197EFC28F339AE00FE67E9 /* StoreKitRCClass.swift */; };
 		951AFAEA28E5655C00A4A4BD /* cities.json in Resources */ = {isa = PBXBuildFile; fileRef = 951AFAE828E5655C00A4A4BD /* cities.json */; };
 		951AFAED28E5657500A4A4BD /* CityModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 951AFAEC28E5657500A4A4BD /* CityModel.swift */; };
 		951AFAEF28E565FE00A4A4BD /* CountryModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 951AFAEE28E565FE00A4A4BD /* CountryModel.swift */; };
@@ -51,6 +52,7 @@
 		9509A8E128E5A3D700CFCDBA /* GuessThePopulationView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GuessThePopulationView.swift; sourceTree = "<group>"; };
 		950C535228F2FA3300179C78 /* BuyPremiumModalView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BuyPremiumModalView.swift; sourceTree = "<group>"; };
 		950C535828F3178B00179C78 /* StoreKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = StoreKit.framework; path = System/Library/Frameworks/StoreKit.framework; sourceTree = SDKROOT; };
+		95197EFC28F339AE00FE67E9 /* StoreKitRCClass.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StoreKitRCClass.swift; sourceTree = "<group>"; };
 		951AFAE828E5655C00A4A4BD /* cities.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = cities.json; sourceTree = "<group>"; };
 		951AFAEC28E5657500A4A4BD /* CityModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CityModel.swift; sourceTree = "<group>"; };
 		951AFAEE28E565FE00A4A4BD /* CountryModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CountryModel.swift; sourceTree = "<group>"; };
@@ -109,6 +111,7 @@
 				95FA409B28D9881100129B60 /* CountryGameClass.swift */,
 				951AFAF028E5735400A4A4BD /* CityGameClass.swift */,
 				95919DB528F076BF00F21F8F /* UserClass.swift */,
+				95197EFC28F339AE00FE67E9 /* StoreKitRCClass.swift */,
 				95AE8D5628C8750E0067F219 /* LoadFunc.swift */,
 			);
 			path = Logic;
@@ -280,6 +283,7 @@
 				955A65A928D7815E00CEEC6D /* HapticsClass.swift in Sources */,
 				95BC392D28EC42570049AB49 /* CityMapHelper.swift in Sources */,
 				952E41E928DC521200198643 /* GameAlertsModifier.swift in Sources */,
+				95197EFD28F339AE00FE67E9 /* StoreKitRCClass.swift in Sources */,
 				95919DB828F079D100F21F8F /* UserSettingsModel.swift in Sources */,
 				9509A8E228E5A3D700CFCDBA /* GuessThePopulationView.swift in Sources */,
 				955A658328D733E400CEEC6D /* GameProtocol+Extension.swift in Sources */,
--- a/GeoQuiz/BuyPremiumModalView.swift	Sun Oct 09 17:02:34 2022 +0200
+++ b/GeoQuiz/BuyPremiumModalView.swift	Sun Oct 09 19:46:44 2022 +0200
@@ -9,6 +9,7 @@
 
 struct BuyPremiumModalView: View {
     @Environment(\.dismiss) var dismiss
+    @StateObject var storeKitRC = StoreKitRC()
     
     var body: some View {
         NavigationView {
@@ -54,15 +55,20 @@
                             .fontWeight(.semibold)
                             .foregroundColor(.secondary)
                         
-                        Button {
-                            // Buy
-                        } label: {
-                            Text("Buy for $2.99")
-                                .font(.headline)
-                                .padding()
+                        if let productPrice = storeKitRC.productPrice {
+                            Button {
+                                // Buy
+                            } label: {
+                                Text("Buy for \(productPrice)")
+                                    .font(.headline)
+                                    .padding()
+                            }
+                            .buttonStyle(.borderedProminent)
+                            .padding(.top)
+                        } else {
+                            ProgressView()
+                                .padding(.top)
                         }
-                        .buttonStyle(.borderedProminent)
-                        .padding(.top)
                     }
                     .padding()
                     
@@ -72,6 +78,7 @@
                     }
                     .font(.callout)
                     .foregroundColor(.secondary)
+                    .padding()
                 }
             }
             .navigationBarTitleDisplayMode(.inline)
@@ -84,6 +91,11 @@
                     }
                 }
             }
+            .alert("Something went wrong šŸ¤•", isPresented: $storeKitRC.showingErrorAlert) {
+                Button("OK", role: .cancel) { dismiss() }
+            } message: {
+                Text(storeKitRC.errorMessage)
+            }
         }
     }
 }
--- a/GeoQuiz/GeoQuizApp.swift	Sun Oct 09 17:02:34 2022 +0200
+++ b/GeoQuiz/GeoQuizApp.swift	Sun Oct 09 19:46:44 2022 +0200
@@ -18,16 +18,6 @@
     var body: some Scene {
         WindowGroup {
             ContentView()
-                .onAppear(perform: testRevenueCat)
-        }
-    }
-    
-    private func testRevenueCat() {
-        Purchases.shared.getOfferings { (offerings, error) in
-            if let packages = offerings?.current?.availablePackages {
-                // Display packages for sale
-                print(packages)
-            }
         }
     }
 }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/GeoQuiz/Info.plist	Sun Oct 09 19:46:44 2022 +0200
@@ -0,0 +1,10 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
+<plist version="1.0">
+<dict>
+	<key>UIBackgroundModes</key>
+	<array>
+		<string>audio</string>
+	</array>
+</dict>
+</plist>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/GeoQuiz/Logic/StoreKitRCClass.swift	Sun Oct 09 19:46:44 2022 +0200
@@ -0,0 +1,33 @@
+//
+//  StoreKitRCClass.swift
+//  GeoQuiz
+//
+//  Created by Dennis Concepción Martín on 9/10/22.
+//
+
+import Foundation
+import RevenueCat
+
+class StoreKitRC: ObservableObject {
+    @Published var productPrice: String?
+    @Published var showingErrorAlert = false
+    @Published var errorMessage = ""
+
+    init() {
+        
+        // Get product metadata
+        Purchases.shared.getOfferings { (offerings, error) in
+            if let package = offerings?.current?.lifetime?.storeProduct {
+                self.productPrice = package.localizedPriceString
+            } else {
+                self.errorMessage = "There was an error fetching the product. Please, contact the developer at dmartin@dennistech.io."
+                self.showingErrorAlert = true
+            }
+            
+            if let error = error {
+                self.errorMessage = error.localizedDescription
+                self.showingErrorAlert = true
+            }
+        }
+    }
+}