Xcode 在ios中进行应用内购买,EXC_坏访问

Xcode 在ios中进行应用内购买,EXC_坏访问,xcode,ios5,in-app-purchase,exc-bad-access,Xcode,Ios5,In App Purchase,Exc Bad Access,我想在我的ios应用程序中实现应用内购买,我有升级按钮,当用户点击升级时,我使用SKPayment SKProductsRequest发送产品请求,并添加其委托方法, 代码如下所示,每当我点击升级按钮,在2-3秒后,它给我错误为“执行错误访问”。它仅在sendRequest方法中显示错误。请给我答案 -(void)sendRequest { if ([SKPaymentQueue canMakePayments]) { SKProductsRequest *re

我想在我的ios应用程序中实现应用内购买,我有升级按钮,当用户点击升级时,我使用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];
    }
}

因为SKProductRequest是SKRequest的一个子类,所以您需要实现委托

  • (无效)请求:(SKRequest*)请求失败错误:(NSError*)错误

要获得任何错误,这可能会将您指向正确的位置。raywenderlich.com()的教程中有一个很好的例子。

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

@property (strong, nonatomic) SKProductsRequest *request;
@property (strong, nonatomic) SKPaymentQueue *skPaymentQueue;
可以通过将内存定义为属性来保留内存 在viewDidLoad中

self.skPaymentQueue = [SKPaymentQueue defaultQueue];

可能不相关,但在不保留对已分配对象的引用的情况下,不应将委托设置为self。这适用于
SKProductsRequest*request
UIAlertView*tmp
:当您将委托设置为self时,您应该将指针存储在某个地方(在某些NSArray中?)为了能够在
dealloc
@Cœur上取消代理设置,我也不喜欢这样做,但它通常工作正常,因为警报、请求和URL连接由iOS保留。就我所知您在delegates中发布它是的,我已经实现了该方法,并且没有显示任何错误。