应用内购买各种故障(iOS)

时间:2014-10-21 18:00:47

标签: ios objective-c iphone in-app-purchase

我一直在通过应用内购买遇到各种故障。这里有几个。

当我点按某个项目进行购买时,会显示确认您的应用内购买提醒,然后在一秒左右后,它会自行关闭并打开登录提醒。 有时候我会登录,然后它会让我再次登录。警报弹出并自行消失,看起来非常小问题。有时应用程序崩溃,没有错误消息。

我无法搜索任何答案,因为这些问题很奇怪。

这是我正在使用的代码(我使用的是名为Game的类和类别名称Game-Resources)

Game.m:

- (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response{
    product = response;
}

- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions{
    if (purchasing == NO){
    // if I don't put (purchasing) in, this method is run multiples times
        purchasing = YES;
        [[SKPaymentQueue defaultQueue] addTransactionObserver:self];
        for (SKPaymentTransaction * transaction in transactions){
            switch (transaction.transactionState) {
                case SKPaymentTransactionStatePurchasing:
                    NSLog(@"purchasing");
                    break;
                case SKPaymentTransactionStatePurchased:
                    purchasing = NO;
                    NSLog(@"done");       
                    if ([[NSUserDefaults standardUserDefaults] objectForKey:purchaseNSUserName] != nil){
                        int i = (int) [[NSUserDefaults standardUserDefaults] integerForKey:purchaseNSUserName];
                        [[NSUserDefaults standardUserDefaults] setInteger:i + purchaseNSUserAmount forKey:purchaseNSUserName];
                        [[NSUserDefaults standardUserDefaults] synchronize];
                    }else{
                        [[NSUserDefaults standardUserDefaults] setInteger:purchaseNSUserAmount forKey:purchaseNSUserName];
                        [[NSUserDefaults standardUserDefaults] synchronize];
                    }
                    [[SKPaymentQueue defaultQueue] finishTransaction:transaction];
                    break;
                case SKPaymentTransactionStateRestored:
                    purchasing = NO;
                    break;
                case SKPaymentTransactionStateFailed:
                    purchasing = NO;
                    if (transaction.error.code != SKErrorPaymentCancelled){
                        [[SKPaymentQueue defaultQueue] finishTransaction:transaction];
                        //error happened
                    }
                    break;
                case SKPaymentTransactionStateDeferred:
                    purchasing = NO;
                    break;
                default:
                    break;
            }
            purchaseNSUserName = nil;
            purchaseNSUserAmount = 0;
        }
    }
}

在ViewDidLoad中:

if ([SKPaymentQueue canMakePayments]){
    self.productsRequest = [[SKProductsRequest alloc] initWithProductIdentifiers:[NSSet setWithObjects:@"somethinghere", @"anotherthing", nil]];
    self.productsRequest.delegate = self;
    [self.productsRequest start];
}

在游戏资源中(按下购买按钮时):

SKPayment *payment = [SKPayment paymentWithProduct: product.products[productID]];
[[SKPaymentQueue defaultQueue] addTransactionObserver:self];
[[SKPaymentQueue defaultQueue] addPayment:payment];

0 个答案:

没有答案