|
| 1 | +// |
| 2 | +// © 2024-present https://github.com/cengiz-pz |
| 3 | +// |
| 4 | + |
| 5 | +import Foundation |
| 6 | +import StoreKit |
| 7 | +import OSLog |
| 8 | + |
| 9 | +@available(iOS 16.0, *) |
| 10 | +@objcMembers public class InappReview : NSObject |
| 11 | +{ |
| 12 | + |
| 13 | + private static let logger = Logger(subsystem: "org.godotengine.plugin", category: "InappReview") |
| 14 | + |
| 15 | + static func requestReview() { |
| 16 | + logger.debug("Attempting to request in-app review.") |
| 17 | + |
| 18 | + // Dispatch to main thread (Godot may call us from background) |
| 19 | + DispatchQueue.main.async { |
| 20 | + // Find the active foreground scene |
| 21 | + guard let scene = UIApplication.shared.connectedScenes |
| 22 | + .compactMap({ $0 as? UIWindowScene }) |
| 23 | + .first(where: { $0.activationState == .foregroundActive }) else { |
| 24 | + |
| 25 | + logger.error("Failed to find an active foreground UIWindowScene. Cannot request review.") |
| 26 | + return |
| 27 | + } |
| 28 | + |
| 29 | + if #available(iOS 18.0, *) { |
| 30 | + logger.debug("Using AppStore.requestReview(in:).") |
| 31 | + AppStore.requestReview(in: scene) |
| 32 | + } else if #available(iOS 14.0, *) { |
| 33 | + logger.debug("Using SKStoreReviewController.requestReview(in:).") |
| 34 | + SKStoreReviewController.requestReview(in: scene) |
| 35 | + } else { |
| 36 | + logger.debug("Using SKStoreReviewController.requestReview().") |
| 37 | + SKStoreReviewController.requestReview() |
| 38 | + } |
| 39 | + |
| 40 | + logger.info("requestReview method called successfully.") |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + func fetchAppStoreID(completion: @escaping (String?) -> Void) { |
| 45 | + Self.logger.debug("Starting fetchAppStoreID process.") |
| 46 | + |
| 47 | + // Get the local Bundle ID |
| 48 | + guard let bundleId = Bundle.main.bundleIdentifier, |
| 49 | + let encodedBundleId = bundleId.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) else { |
| 50 | + Self.logger.error("Failed to get or encode bundle identifier.") |
| 51 | + completion(nil) |
| 52 | + return |
| 53 | + } |
| 54 | + |
| 55 | + Self.logger.debug("Bundle ID: \(bundleId, privacy: .public)") |
| 56 | + |
| 57 | + // Construct the iTunes Lookup URL |
| 58 | + let lookupURLString = "https://itunes.apple.com/lookup?bundleId=\(encodedBundleId)" |
| 59 | + guard let lookupURL = URL(string: lookupURLString) else { |
| 60 | + Self.logger.error("Failed to construct valid lookup URL from string: \(lookupURLString, privacy: .public)") |
| 61 | + completion(nil) |
| 62 | + return |
| 63 | + } |
| 64 | + |
| 65 | + Self.logger.debug("Lookup URL: \(lookupURL.absoluteString, privacy: .public)") |
| 66 | + |
| 67 | + // Perform the network request |
| 68 | + URLSession.shared.dataTask(with: lookupURL) { data, response, error in |
| 69 | + |
| 70 | + // Check for network errors |
| 71 | + if let error = error { |
| 72 | + Self.logger.error("Network request failed: \(error.localizedDescription, privacy: .public)") |
| 73 | + completion(nil) |
| 74 | + return |
| 75 | + } |
| 76 | + |
| 77 | + // Check for data presence |
| 78 | + guard let data = data else { |
| 79 | + Self.logger.error("Network request returned no data and no error.") |
| 80 | + completion(nil) |
| 81 | + return |
| 82 | + } |
| 83 | + |
| 84 | + do { |
| 85 | + // Parse the JSON response |
| 86 | + if let json = try JSONSerialization.jsonObject(with: data, options: []) as? [String: Any], |
| 87 | + let results = json["results"] as? [[String: Any]], |
| 88 | + let firstResult = results.first, |
| 89 | + let trackId = firstResult["trackId"] as? Int { |
| 90 | + |
| 91 | + let storeId = String(trackId) // The trackId is the App Store ID |
| 92 | + |
| 93 | + Self.logger.info("Successfully fetched App Store ID: \(storeId, privacy: .public)") |
| 94 | + completion(storeId) |
| 95 | + } else { |
| 96 | + Self.logger.error("JSON parsing failed to extract 'trackId' (App Store ID).") |
| 97 | + completion(nil) |
| 98 | + } |
| 99 | + } catch { |
| 100 | + Self.logger.error("JSON serialization error: \(error.localizedDescription, privacy: .public)") |
| 101 | + completion(nil) |
| 102 | + } |
| 103 | + }.resume() |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * Asynchronously fetches the application's Store ID and constructs the URL |
| 108 | + * that directs the user directly to the 'Write a Review' page in the App Store. |
| 109 | + * |
| 110 | + * @param completion A closure that receives the constructed review URL or nil on failure. |
| 111 | + */ |
| 112 | + func getAppReviewUrl(completion: @escaping (URL?) -> Void) { |
| 113 | + Self.logger.debug("Starting getAppReviewUrl process.") |
| 114 | + |
| 115 | + fetchAppStoreID { storeId in |
| 116 | + guard let storeId = storeId, |
| 117 | + let url = URL(string: "https://apps.apple.com/app/id\(storeId)?action=write-review") else { |
| 118 | + |
| 119 | + Self.logger.error("Failed to get App Store ID or construct review URL.") |
| 120 | + completion(nil) |
| 121 | + return |
| 122 | + } |
| 123 | + |
| 124 | + Self.logger.info("Successfully constructed review URL: \(url.absoluteString, privacy: .public)") |
| 125 | + completion(url) |
| 126 | + } |
| 127 | + } |
| 128 | +} |
0 commit comments