3

我想在我的 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];
    }
}
4

3 回答 3

3

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

于 2013-08-29T11:11:54.517 回答
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 ) 的教程中有一个很好的例子。

于 2013-01-30T19:14:35.683 回答
0
@property (strong, nonatomic) SKProductsRequest *request;
@property (strong, nonatomic) SKPaymentQueue *skPaymentQueue;

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

self.skPaymentQueue = [SKPaymentQueue defaultQueue];
于 2014-08-28T09:59:24.547 回答