xcode - 在 ios 中的应用程序购买中,EXC_BAD_ACCESS

标签 xcode ios5 in-app-purchase exc-bad-access

我想在我的ios应用程序中实现应用程序内购买,我有升级按钮,当用户单击升级时,我使用SKPayment SKProductsRequest发送产品请求,并添加其委托(delegate)方法, 代码如下所示,每当我单击升级按钮时,2-3秒后,它会给我错误“执行错误访问”。它仅在 sendRequest 方法中给出错误。请给我解决方案。

-(void)sendRequest
{
    if ([SKPaymentQueue canMakePayments])
    {

        SKProductsRequest *request = [[SKProductsRequest alloc] initWithProductIdentifiers:[NSSet setWithObject:@"thisIsMyProdID"]];
        request.delegate = self;
        [request start];
    }
}
-(void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions
{
    for (SKPaymentTransaction *transaction in transactions)
    {
        switch (transaction.transactionState)
        {

            case SKPaymentTransactionStatePurchasing:
                NSLog(@"Processing...");
                break;

            case SKPaymentTransactionStatePurchased:
            {
                [[SKPaymentQueue defaultQueue] finishTransaction:transaction];
                //statusLabel.text = @"Done!";
                UIAlertView *tmp = [[UIAlertView alloc]
                                    initWithTitle:@"Complete"
                                    message:@"You have unlocked Feature 2!"
                                    delegate:self
                                    cancelButtonTitle:nil
                                    otherButtonTitles:@"Ok", nil];
                [tmp show];


            }
                               break;

            case SKPaymentTransactionStateRestored:
            {
                [[SKPaymentQueue defaultQueue] finishTransaction:transaction];
                // remove wait view here
            }
                break;

            case SKPaymentTransactionStateFailed:
            {
                if (transaction.error.code != SKErrorPaymentCancelled) {
                    NSLog(@"Error payment cancelled");
                }
                [[SKPaymentQueue defaultQueue] finishTransaction:transaction];
                // remove wait view here

            }
                break;

            default:
                break;
        }
    }
}

-(void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response
{
    NSArray *products = response.products;
    if (products.count != 0)
    {
        product = products[0];

        SKPayment *payment = [SKPayment paymentWithProduct:product];

        [[SKPaymentQueue defaultQueue] addTransactionObserver:self];
        [[SKPaymentQueue defaultQueue] addPayment:payment];

    }
    else
    {
        UIAlertView *tmp = [[UIAlertView alloc]
                            initWithTitle:@"Not Available"
                            message:@"No products to purchase"
                            delegate:self
                            cancelButtonTitle:nil
                            otherButtonTitles:@"Ok", nil];
        [tmp show];
    }
}

最佳答案

问题出在我的代码中,我没有处理 SKPayment 的对象,因此在得到苹果的响应之前,我的 SKPayment 对象被释放,所以我得到了错误的访问错误,我全局声明了该对象,然后它工作正常。这只是我的愚蠢错误,对此感到抱歉。

关于xcode - 在 ios 中的应用程序购买中,EXC_BAD_ACCESS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14601977/

相关文章:

android - 如何估算在应用中使用 AdMob 的潜在收入?

Swift 项目编译时 Xcode 6.0.1 段错误

iphone - twitter ios5 集成使用自定义登录名和密码进行自动化

objective-c - UITableView Cells 的图像改变了吗?

ios - 即使正在运行应用程序,是否可以让远程通知显示在通知中心中?

ios - 应用内购买 : Sign in required [Environment: Sandbox]

xcode - 添加新 subview 时 UIScrollView 中断

iOS刷新蓝牙特性

objective-c - 带有 NavigationController 和 TabController 的 Storyboard

android - 应用内订阅取消后仍为 "purchased"