InAppBillingPlugin 多平台适配:iOS、Android、Windows 平台差异详解

发布时间:2026/7/19 13:18:07
InAppBillingPlugin 多平台适配:iOS、Android、Windows 平台差异详解 InAppBillingPlugin 多平台适配iOS、Android、Windows 平台差异详解【免费下载链接】InAppBillingPluginCross-platform In App Billing Plugin for .NET项目地址: https://gitcode.com/gh_mirrors/in/InAppBillingPluginInAppBillingPlugin 是一款功能强大的跨平台应用内计费插件专为 .NET 开发者设计能够帮助应用轻松实现 iOS、Android 和 Windows 平台的应用内购买功能。本文将深入解析三大平台在集成和使用该插件时的核心差异助你快速掌握跨平台应用内计费的实现技巧。图InAppBillingPlugin 跨平台应用内计费架构示意图核心架构设计平台抽象与实现分离InAppBillingPlugin 采用接口抽象 平台实现的设计模式通过IInAppBilling.shared.cs定义统一接口各平台通过特定文件实现具体逻辑共享接口层src/Plugin.InAppBilling/Shared/IInAppBilling.shared.cs平台实现层Androidsrc/Plugin.InAppBilling/InAppBilling.android.csiOSsrc/Plugin.InAppBilling/InAppBilling.ios.csWindowssrc/Plugin.InAppBilling/InAppBilling.uwp.cs这种架构确保业务逻辑与平台细节解耦开发者可通过统一 API 调用实现跨平台计费功能。平台差异全解析关键功能对比1. 连接服务机制Android主动连接与状态管理Android 平台需要显式连接 Google Play 计费服务并处理连接状态// Android 连接实现 public override Taskbool ConnectAsync(bool enablePendingPurchases true, CancellationToken cancellationToken default) { BillingClient BillingClientBuilder.EnablePendingPurchases(pendingParams).Build(); BillingClient.StartConnection(OnSetupFinished, OnDisconnected); // 状态回调处理... }需特别注意BillingClient的生命周期管理确保在使用前完成连接。iOS自动管理连接状态iOS 通过SKPaymentQueue自动管理 App Store 连接无需显式连接步骤// iOS 初始化支付队列 paymentObserver new PaymentObserver(OnPurchaseComplete); SKPaymentQueue.DefaultQueue.AddTransactionObserver(paymentObserver);Windows模拟与正式环境切换Windows 平台通过InTestingMode开关切换测试/正式环境// Windows 环境切换 var listingInformation await CurrentAppMock.LoadListingInformationAsync(InTestingMode);2. 产品信息获取三大平台均通过GetProductInfoAsync方法获取产品信息但返回结构存在差异Android支持订阅和一次性商品// Android 产品类型映射 var skuType itemType switch { ItemType.InAppPurchase ProductType.Inapp, ItemType.InAppPurchaseConsumable ProductType.Inapp, _ ProductType.Subs };iOS提供本地化价格和折扣信息iOS 实现返回丰富的本地化信息和折扣详情// iOS 产品信息构造 new InAppBillingProduct { LocalizedPrice p.LocalizedPrice(), AppleExtras new InAppBillingProductAppleExtras { IntroductoryOffer p.IntroductoryPrice?.ToProductDiscount(), Discounts p.Discounts?.Select(s s.ToProductDiscount()).ToList() } }Windows包含促销和图片信息Windows 产品信息包含促销状态和图片 URI// Windows 产品扩展信息 WindowsExtras new InAppBillingProductWindowsExtras { IsOnSale product.IsOnSale, SaleEndDate product.SaleEndDate, ImageUri product.ImageUri }3. 购买流程实现Android支持订阅升级/降级Android 提供灵活的订阅管理功能// Android 订阅升级 public override async TaskInAppBillingPurchase UpgradePurchasedSubscriptionAsync( string newProductId, string purchaseTokenOfOriginalSubscription, SubscriptionProrationMode prorationMode SubscriptionProrationMode.ImmediateWithTimeProration) { // 实现订阅替换逻辑... }iOS自动处理订阅变更iOS 订阅升级通过 App Store 自动处理无需额外代码// iOS 订阅升级不支持手动实现 public override TaskInAppBillingPurchase UpgradePurchasedSubscriptionAsync(...) { throw new NotImplementedException(iOS not supported. Apple store manages upgrades natively.); }Windows简化的购买流程Windows 购买流程最为简化// Windows 购买实现 var purchaseResult await CurrentAppMock.RequestProductPurchaseAsync(InTestingMode, productId); return purchaseResult.ReceiptXml.ToInAppBillingPurchase(purchaseResult.Status).FirstOrDefault();4. 消耗型商品处理Android显式确认购买Android 消耗型商品需要显式确认// Android 消耗商品 public override async Taskbool ConsumePurchaseAsync(string productId, string transactionIdentifier) { var consumeParams ConsumeParams.NewBuilder() .SetPurchaseToken(transactionIdentifier).Build(); var result await BillingClient.ConsumeAsync(consumeParams); return ParseBillingResult(result.BillingResult); }iOS完成交易即消耗iOS 通过完成交易自动消耗商品// iOS 完成交易 SKPaymentQueue.DefaultQueue.FinishTransaction(transaction);Windows报告商品履行状态Windows 需要报告消耗型商品履行状态// Windows 消耗商品 public async override Taskbool ConsumePurchaseAsync(string productId, string transactionIdentifier) { var result await CurrentAppMock.ReportConsumableFulfillmentAsync( InTestingMode, productId, new Guid(transactionIdentifier)); return result FulfillmentResult.Succeeded; }平台适配最佳实践1. 通用适配策略使用条件编译区分平台代码#if __ANDROID__ // Android 特定逻辑 #elif __IOS__ // iOS 特定逻辑 #elif WINDOWS_UWP // Windows 特定逻辑 #endif异常处理需针对平台特性定制catch (InAppBillingPurchaseException ex) { if (ex.PurchaseError PurchaseError.UserCancelled) { // 用户取消处理 } // 其他错误处理... }2. 平台特有注意事项Android必须在AndroidManifest.xml中声明权限uses-permission android:namecom.android.vending.BILLING /处理pending purchases状态确保应用重启后恢复购买流程iOS配置App Store Connect中的商品信息实现SKPaymentTransactionObserver处理后台购买通知测试需使用沙盒测试账号Windows配置Microsoft Store 仪表板商品信息使用CurrentAppSimulator进行本地测试处理收据验证和 XML 解析官方资源与进一步学习完整文档docs/测试示例src/InAppBillingTests/异常处理指南docs/HandlingExceptions.md安全最佳实践docs/SecuringPurchases.md通过本文的解析你已经了解 InAppBillingPlugin 在三大平台的核心差异和适配要点。建议结合官方文档和测试项目进一步实践跨平台应用内计费功能的实现。如有疑问可参考项目中的示例代码或提交 issue 获取社区支持。【免费下载链接】InAppBillingPluginCross-platform In App Billing Plugin for .NET项目地址: https://gitcode.com/gh_mirrors/in/InAppBillingPlugin创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考