|
|
@@ -7,6 +7,7 @@
|
|
|
|
|
|
import Flutter
|
|
|
import UIKit
|
|
|
+import StoreKit
|
|
|
|
|
|
class FlutterMethodChannelManager: NSObject {
|
|
|
|
|
|
@@ -69,6 +70,15 @@ class FlutterMethodChannelManager: NSObject {
|
|
|
case "openKeyboardGuide":
|
|
|
KeyboardSharedDataManager.shared.saveIsShowGuide()
|
|
|
result(true)
|
|
|
+ case "isHasDiscount":
|
|
|
+ if let args = call.arguments as? [String: Any],
|
|
|
+ let appleGoodId = args["appleGoodId"] as? String {
|
|
|
+ self.checkProductDiscount(appleGoodId: appleGoodId, completion: { hasDiscount in
|
|
|
+ result(hasDiscount)
|
|
|
+ })
|
|
|
+ } else {
|
|
|
+ result(FlutterError(code: "INVALID_ARGUMENTS", message: "无效的参数", details: nil))
|
|
|
+ }
|
|
|
default:
|
|
|
result(FlutterMethodNotImplemented)
|
|
|
}
|
|
|
@@ -101,55 +111,80 @@ class FlutterMethodChannelManager: NSObject {
|
|
|
return currentKeyboardName == appDisplayName
|
|
|
}
|
|
|
|
|
|
- // 是否添加键盘
|
|
|
- func isKeyboardEnabled() -> Bool {
|
|
|
-
|
|
|
- // 获取系统中已安装的键盘列表
|
|
|
- let keyboards = UserDefaults.standard.dictionaryRepresentation()
|
|
|
- .filter { $0.key.contains("Keyboard") }
|
|
|
- if let keyboardList = keyboards["AppleKeyboards"] as? [String] {
|
|
|
-
|
|
|
- let keyboardBundleId = "com.qihuan.zhuiaijianpan.AiKeyboard"
|
|
|
- let isAdded = keyboardList.contains(keyboardBundleId)
|
|
|
- return isAdded
|
|
|
- }
|
|
|
- return false
|
|
|
- }
|
|
|
+}
|
|
|
+
|
|
|
+extension FlutterMethodChannelManager {
|
|
|
|
|
|
- // 是否为默认键盘
|
|
|
- func isCustomKeybroad() -> Bool {
|
|
|
+ private func checkProductDiscount(appleGoodId: String, completion: @escaping (Bool) -> Void) {
|
|
|
|
|
|
- let currentKeyboardName = (((UITextInputMode.activeInputModes as NSArray).filtered(using: NSPredicate(format: "isDisplayed = YES"))).last as? NSObject)?.value(forKey: "extendedDisplayName") as? String
|
|
|
-
|
|
|
- let infoDictionary = Bundle.main.infoDictionary!
|
|
|
- let appDisplayName = infoDictionary["CFBundleDisplayName"] as? String
|
|
|
+ if #available(iOS 15.0, *) {
|
|
|
+ Task {
|
|
|
+ do {
|
|
|
+// print("开始请求产品信息 (StoreKit 2)")
|
|
|
+ let products = try await Product.products(for: [appleGoodId])
|
|
|
+ print("成功获取产品信息: \(products.count) 个产品")
|
|
|
+ if let product = products.first {
|
|
|
+ // 检查是否有优惠
|
|
|
+ var hasDiscount = await product.subscription?.isEligibleForIntroOffer ?? false
|
|
|
+
|
|
|
+ DispatchQueue.main.async {
|
|
|
+ completion(hasDiscount)
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ print("未找到产品")
|
|
|
+ DispatchQueue.main.async {
|
|
|
+ completion(false)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } catch {
|
|
|
+ print("获取产品失败 (StoreKit 2): \(error)")
|
|
|
+ DispatchQueue.main.async {
|
|
|
+ completion(false)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
- return currentKeyboardName == appDisplayName
|
|
|
+// // 使用StoreKit查询商品信息
|
|
|
+// let request = SKProductsRequest(productIdentifiers: [appleGoodId])
|
|
|
+// request.delegate = ProductRequestDelegate(completion: { product in
|
|
|
+// if let product = product {
|
|
|
+// // iOS 12.2及以上版本支持促销优惠
|
|
|
+// if #available(iOS 12.2, *) {
|
|
|
+// // 检查商品是否有促销优惠
|
|
|
+// let hasDiscount = product.discounts.count > 0
|
|
|
+// completion(hasDiscount)
|
|
|
+// } else {
|
|
|
+// completion(false)
|
|
|
+// }
|
|
|
+// } else {
|
|
|
+// completion(false)
|
|
|
+// }
|
|
|
+// })
|
|
|
+// request.start()
|
|
|
}
|
|
|
+}
|
|
|
+
|
|
|
+// 处理StoreKit产品请求的代理
|
|
|
+class ProductRequestDelegate: NSObject, SKProductsRequestDelegate {
|
|
|
|
|
|
- // 是否添加键盘
|
|
|
- func isKeyboardEnabled() -> Bool {
|
|
|
-
|
|
|
- // 获取系统中已安装的键盘列表
|
|
|
- let keyboards = UserDefaults.standard.dictionaryRepresentation()
|
|
|
- .filter { $0.key.contains("Keyboard") }
|
|
|
- if let keyboardList = keyboards["AppleKeyboards"] as? [String] {
|
|
|
-
|
|
|
- let keyboardBundleId = "com.qihuan.zhuiaijianpan.AiKeyboard"
|
|
|
- let isAdded = keyboardList.contains(keyboardBundleId)
|
|
|
- return isAdded
|
|
|
+ private let completion: (SKProduct?) -> Void
|
|
|
+
|
|
|
+ init(completion: @escaping (SKProduct?) -> Void) {
|
|
|
+ self.completion = completion
|
|
|
+ super.init()
|
|
|
+ }
|
|
|
+
|
|
|
+ func productsRequest(_ request: SKProductsRequest, didReceive response: SKProductsResponse) {
|
|
|
+ if let product = response.products.first {
|
|
|
+ completion(product)
|
|
|
+ } else {
|
|
|
+ completion(nil)
|
|
|
}
|
|
|
- return false
|
|
|
}
|
|
|
-
|
|
|
- // 是否为默认键盘
|
|
|
- func isCustomKeybroad() -> Bool {
|
|
|
-
|
|
|
- let currentKeyboardName = (((UITextInputMode.activeInputModes as NSArray).filtered(using: NSPredicate(format: "isDisplayed = YES"))).last as? NSObject)?.value(forKey: "extendedDisplayName") as? String
|
|
|
-
|
|
|
- let infoDictionary = Bundle.main.infoDictionary!
|
|
|
- let appDisplayName = infoDictionary["CFBundleDisplayName"] as? String
|
|
|
-
|
|
|
- return currentKeyboardName == appDisplayName
|
|
|
+
|
|
|
+ func request(_ request: SKRequest, didFailWithError error: Error) {
|
|
|
+ print("商品请求失败: \(error.localizedDescription)")
|
|
|
+ completion(nil)
|
|
|
}
|
|
|
}
|