xamarin.ios - Xamarin SKPaymentQueue AddPayment 抛出异常

标签 xamarin.ios in-app-purchase xamarin

在我的 Xamarin 应用程序中,当我调用此方法时

private void MakePayment (SKProduct product)
{
    SKPayment payment = SKPayment.PaymentWithProduct (product);
    SKPaymentQueue.DefaultQueue.AddPayment (payment);
}

我收到此错误:

Failed to marshal the Objective-C object 0x14607110 (type: SKPaymentTransaction). Could not find an existing managed instance for this object, nor was it possible to create a new managed instance (because the type 'MonoTouch.StoreKit.SKPaymentTransaction[]' does not have a constructor that takes one IntPtr argument).

我不确定是否配置错误或者我的代码或 Xamarin 存在问题。

这是观察者的代码

internal class CustomPaymentObserver : SKPaymentTransactionObserver
{
    private InAppPurchase inAppPurchase;

    public CustomPaymentObserver (InAppPurchase inAppPurchase)
    {
        this.inAppPurchase = inAppPurchase;
    }

    public override void UpdatedTransactions (SKPaymentQueue queue, SKPaymentTransaction[] transactions)
    {
        Console.WriteLine ("UpdatedTransactions");
        foreach (SKPaymentTransaction transaction in transactions) {
            switch (transaction.TransactionState) {
            case SKPaymentTransactionState.Purchased:
                inAppPurchase.CompleteTransaction (transaction);
                break;
            case SKPaymentTransactionState.Failed:
                inAppPurchase.FailedTransaction (transaction);
                break;
            default:
                break;
            }
        }
    }

    public override void PaymentQueueRestoreCompletedTransactionsFinished (SKPaymentQueue queue)
    {
    }

    public override void RestoreCompletedTransactionsFailedWithError (SKPaymentQueue queue, NSError error)
    {
    }
}

这是完整的堆栈跟踪:

System.Exception: Failed to marshal the Objective-C object 0x17ecb680 (type: SKPaymentTransaction). Could not find an existing managed instance for this object, nor was it possible to create a new managed instance (because the type 'MonoTouch.StoreKit.SKPaymentTransaction[]' does not have a constructor that takes one IntPtr argument).
at MonoTouch.ObjCRuntime.Runtime.MissingCtor (IntPtr ptr, IntPtr klass, System.Type type, MissingCtorResolution resolution) [0x00046] in /Developer/MonoTouch/Source/monotouch/src/ObjCRuntime/.pp-Runtime.cs:352
at MonoTouch.ObjCRuntime.Runtime.ConstructNSObject[NSObject] (IntPtr ptr, System.Type type, MissingCtorResolution missingCtorResolution) [0x00000] in :0
at MonoTouch.ObjCRuntime.Runtime.GetNSObject (IntPtr ptr, System.Type target_type, MissingCtorResolution missingCtorResolution, System.Boolean& created) [0x00073] in /Developer/MonoTouch/Source/monotouch/src/ObjCRuntime/.pp-Runtime.cs:514
at MonoTouch.ObjCRuntime.Runtime.GetNSObjectWrapped (IntPtr ptr, IntPtr type_ptr, System.Boolean& created) [0x0000c] in /Developer/MonoTouch/Source/monotouch/src/ObjCRuntime/.pp-Runtime.cs:686
at at (wrapper native-to-managed) MonoTouch.ObjCRuntime.Runtime:GetNSObjectWrapped (intptr,intptr,int&)
at at (wrapper managed-to-native) MonoTouch.ObjCRuntime.Messaging:void_objc_msgSend_IntPtr (intptr,intptr,intptr)
at MonoTouch.StoreKit.SKPaymentQueue.AddPayment (MonoTouch.StoreKit.SKPayment payment) [0x00014] in /Developer/MonoTouch/Source/monotouch/src/StoreKit/.pp-SKPaymentQueue.g.cs:109
at IOS.Util.IAP.InAppPurchase.ReceivedResponse (MonoTouch.StoreKit.SKProductsRequest request, MonoTouch.StoreKit.SKProductsResponse response) [0x0001d] in /Users/aaron/Projects/budget-ease-xamarin/IOS/Util/IAP/InAppPurchase.cs:43
at at (wrapper managed-to-native) MonoTouch.UIKit.UIApplication:UIApplicationMain (int,string[],intptr,intptr)
at MonoTouch.UIKit.UIApplication.Main (System.String[] args, System.String principalClassName, System.String delegateClassName) [0x0004c] in /Developer/MonoTouch/Source/monotouch/src/UIKit/.pp-UIApplication.cs:38
at IOS.Application.Main (System.String[] args) [0x00008] in /Users/aaron/Projects/budget-ease-xamarin/IOS/Main.cs:16

最佳答案

这里发生的情况是,您的 CustomPaymentObserver 的 C# 实例被垃圾回收,而它的 native (“Objective-C”)对应实例仍然存在。当通知最终发送时, native 对象尝试调用现已死亡的 C# 对象并使您的应用程序崩溃。

要避免这种情况,请保留对您的 CustomPaymentObserver 的引用,例如在您的 AppDelegate 中以使其保持事件状态。

我不确定这是否在 Xamarin.iOS 的某处进行了记录(快速查看时找不到任何内容),但我相信 Xamarin.Droid 非常相似(例如 http://developer.xamarin.com/guides/android/advanced_topics/garbage_collection/ )

关于xamarin.ios - Xamarin SKPaymentQueue AddPayment 抛出异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20649232/

相关文章:

ios - UIPageViewController 第二个 View 变为空白

android - RecycleView 回收无法正常工作

xamarin - 可以使用 XAML 将多个绑定(bind)添加到标签吗?

multithreading - MonoTouch-AppDomain.CurrentDomain.UnhandledException挂起

objective-c - 与 iOS 存储和同步图像

ios - Xamarin 应用程序键盘的大小与 iOS 键盘不同

android - 如何在应用处于草稿状态时在 Google Play Developer Console 中激活应用内产品?

ios - 应用程序被拒绝 - 上传新的二进制文件 - 应用程序内购买 - 需要开发人员采取行动 - 等待审核

android - 在 2016 年 6 月 20 日之后取消 Google Play IAB 测试购买的订单

c# - 提取 JSON 数据 Xamarin