|
| 1 | +import Foundation |
| 2 | +import StoreKit |
| 3 | +import UIKit |
| 4 | +import Flutter |
| 5 | + |
| 6 | +final class AppStoreUpdateBridge: NSObject, SKStoreProductViewControllerDelegate { |
| 7 | + private weak var viewController: UIViewController? |
| 8 | + |
| 9 | + init(viewController: UIViewController?) { |
| 10 | + self.viewController = viewController |
| 11 | + super.init() |
| 12 | + } |
| 13 | + |
| 14 | + func checkAndShowUpdate(result: @escaping FlutterResult) { |
| 15 | + guard let bundleId = Bundle.main.bundleIdentifier else { |
| 16 | + result(false) |
| 17 | + return |
| 18 | + } |
| 19 | + |
| 20 | + let currentVersion = Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String ?? "0" |
| 21 | + let urlString = "https://itunes.apple.com/lookup?bundleId=\(bundleId)" |
| 22 | + guard let url = URL(string: urlString) else { |
| 23 | + result(false) |
| 24 | + return |
| 25 | + } |
| 26 | + |
| 27 | + URLSession.shared.dataTask(with: url) { [weak self] data, _, error in |
| 28 | + if let error = error { |
| 29 | + print("[AppStoreUpdateBridge] Apple API error: \(error)") |
| 30 | + result(false) |
| 31 | + return |
| 32 | + } |
| 33 | + |
| 34 | + guard |
| 35 | + let data = data, |
| 36 | + let json = try? JSONSerialization.jsonObject(with: data) as? [String: Any], |
| 37 | + let results = json["results"] as? [[String: Any]], |
| 38 | + let first = results.first, |
| 39 | + let latestVersion = first["version"] as? String, |
| 40 | + let trackId = first["trackId"] as? Int |
| 41 | + else { |
| 42 | + result(false) |
| 43 | + return |
| 44 | + } |
| 45 | + |
| 46 | + let hasUpdate = self?.isVersionNewer(latest: latestVersion, current: currentVersion) ?? false |
| 47 | + if !hasUpdate { |
| 48 | + result(false) |
| 49 | + return |
| 50 | + } |
| 51 | + |
| 52 | + self?.presentStoreProduct(trackId: trackId) { presented in |
| 53 | + result(presented) |
| 54 | + } |
| 55 | + }.resume() |
| 56 | + } |
| 57 | + |
| 58 | + private func presentStoreProduct(trackId: Int, completion: @escaping (Bool) -> Void) { |
| 59 | + DispatchQueue.main.async { [weak self] in |
| 60 | + guard let presenter = self?.viewController ?? UIApplication.shared.windows.first(where: { $0.isKeyWindow })?.rootViewController else { |
| 61 | + completion(false) |
| 62 | + return |
| 63 | + } |
| 64 | + |
| 65 | + let storeVC = SKStoreProductViewController() |
| 66 | + storeVC.delegate = self |
| 67 | + let parameters = [SKStoreProductParameterITunesItemIdentifier: NSNumber(value: trackId)] |
| 68 | + storeVC.loadProduct(withParameters: parameters) { loaded, error in |
| 69 | + if let error = error { |
| 70 | + print("[AppStoreUpdateBridge] Failed to load product: \(error)") |
| 71 | + completion(false) |
| 72 | + return |
| 73 | + } |
| 74 | + if loaded { |
| 75 | + presenter.present(storeVC, animated: true) { |
| 76 | + completion(true) |
| 77 | + } |
| 78 | + } else { |
| 79 | + completion(false) |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + private func isVersionNewer(latest: String, current: String) -> Bool { |
| 86 | + let latestParts = latest.split(separator: ".").map { Int($0) ?? 0 } |
| 87 | + let currentParts = current.split(separator: ".").map { Int($0) ?? 0 } |
| 88 | + let count = max(latestParts.count, currentParts.count) |
| 89 | + |
| 90 | + for index in 0..<count { |
| 91 | + let latestValue = index < latestParts.count ? latestParts[index] : 0 |
| 92 | + let currentValue = index < currentParts.count ? currentParts[index] : 0 |
| 93 | + if latestValue != currentValue { |
| 94 | + return latestValue > currentValue |
| 95 | + } |
| 96 | + } |
| 97 | + return false |
| 98 | + } |
| 99 | + |
| 100 | + func productViewControllerDidFinish(_ viewController: SKStoreProductViewController) { |
| 101 | + viewController.dismiss(animated: true) |
| 102 | + } |
| 103 | +} |
0 commit comments