Flutter中如何从RevenueCat获取用户AppStore Locale?
关于通过RevenueCat获取App Store Locale的说明
- RevenueCat目前没有直接提供获取用户App Store locale(国家/地区代码)的接口,你提到的
StoreProduct.currencyCode仅能获取货币代码,无法等价于App Store的locale(同一货币可能对应多个地区),所以无法通过RevenueCat直接实现需求。
Flutter端获取App Store Storefront国家代码的方案
1. 自定义Flutter插件封装原生代码
自己实现一个轻量插件调用iOS原生API,示例如下:
- iOS端Swift代码:
import Flutter import StoreKit class StorefrontHandler: NSObject, FlutterPlugin { static func register(with registrar: FlutterPluginRegistrar) { let channel = FlutterMethodChannel(name: "app/storefront", binaryMessenger: registrar.messenger()) let instance = StorefrontHandler() registrar.addMethodCallDelegate(instance, channel: channel) } func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) { if call.method == "getCountryCode" { let countryCode = SKPaymentQueue.default().storefront?.countryCode result(countryCode) } else { result(FlutterMethodNotImplemented) } } } - Flutter端调用代码:
import 'package:flutter/services.dart'; Future<String?> fetchAppStoreCountryCode() async { const channel = MethodChannel('app/storefront'); try { return await channel.invokeMethod('getCountryCode'); } on PlatformException catch (e) { print("获取失败: ${e.message}"); return null; } }
2. 使用官方in_app_purchase插件
官方插件已封装StoreStorefront相关能力,iOS 13+可用:
import 'package:in_app_purchase/in_app_purchase.dart'; Future<String?> getAppStoreCountryCode() async { final storefront = await InAppPurchase.instance.storefrontStream.first; return storefront?.countryCode; }
内容的提问来源于stack exchange,提问作者Chris
相关产品推荐
相关产品推荐

