在app中购买ios,EXC_BAD_ACCESS

时间:2013-01-30 10:38:49

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

我想在我的ios应用程序中实现In-App-Purchase,我有UPGRADE按钮,当用户点击升级时,我正在使用SKPayment SKProductsRequest发送产品请求,并添加了其委托方法, 代码如下所示,每当我点击升级按钮,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];
    }
}

3 个答案:

答案 0 :(得分:3)

问题在于我的代码,我没有处理SKPayment的对象,因此在从Apple获得响应之前,我的SKPayment对象被释放了,所以我得到了错误的访问错误,我在全局声明了对象然后它的工作正常。这只是我的愚蠢错误,对不起。

答案 1 :(得分:0)

由于SKProductRequest是SKRequest的子类,因此您需要实现委托

  • (void)request:(SKRequest *)request didFailWithError:(NSError *)error

要获得任何错误,这可能会指向正确的位置。一个很好的例子来自raywenderlich.com(http://www.raywenderlich.com/21081/introduction-to-in-app-purchases-in-ios-6-tutorial)的教程。

答案 2 :(得分:0)

@property (strong, nonatomic) SKProductsRequest *request;
@property (strong, nonatomic) SKPaymentQueue *skPaymentQueue;

您可以通过将其定义为属性来保留内存 在viewDidLoad

self.skPaymentQueue = [SKPaymentQueue defaultQueue];