应用内购买无法为某些客户工作或恢复

时间:2016-11-14 10:26:05

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

我从我的一些客户那里收到的是,即使他们在应用程序购买中,也没有加载产品。但我的大多数客户都没有这个问题。我指示他们做恢复操作,但这没有帮助。问题是他们可以在具有相同Apple ID的其他设备中正确恢复。

没有特定的设备或IOS版本,应用程序没有崩溃。只是它说正确恢复或正确购买,但它没有这样做。

这让我很疯狂,因为我在许多设备上试过,它对我来说是正常的。

我想分享我使用的代码,这可能有助于您理解我无法解决的问题。

- (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response {

    SKProduct *validProduct = nil;
    NSUInteger count = [response.products count];

    if( count > 0 ) {
        validProduct = [response.products objectAtIndex:0];
        NSLog(@"Products Available!");
        [self purchase:validProduct];
    }
    else if(!validProduct){
        NSLog(@"No products available");
        [self hideProgressHud];
        isProgress = NO;

        UIAlertController *alert = [UIAlertController alertControllerWithTitle:LocalizedString(@"Notice") message:LocalizedString(@"Product is not available now.") preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *cancelBtn = [UIAlertAction actionWithTitle:LocalizedString(@"OK") style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        }];
        [alert addAction:cancelBtn];
        [self presentViewController:alert animated:true completion:nil];
    }
}

- (void)purchase:(SKProduct *)product {
    SKPayment *payment = [SKPayment paymentWithProduct:product];
    [[SKPaymentQueue defaultQueue] addPayment:payment];
}

- (void) paymentQueueRestoreCompletedTransactionsFinished:(SKPaymentQueue *)queue {

    NSLog(@"received restored transactions: %lu", (unsigned long)queue.transactions.count);
    for(SKPaymentTransaction *transaction in queue.transactions){
        if(transaction.transactionState == SKPaymentTransactionStateRestored || transaction.transactionState == SKPaymentTransactionStateRestored){
            NSLog(@"Transaction state -> Restored");
            NSString *productID = transaction.payment.productIdentifier;
            [[InAppPurchaseManager sharedManager] savePurchaseItem:productID];
            [[SKPaymentQueue defaultQueue] finishTransaction:transaction];
            break;
        }
    }
    [self hideProgressHud];
}

- (void)paymentQueue:(SKPaymentQueue *)queue restoreCompletedTransactionsFailedWithError:(NSError *)error {

    [self hideProgressHud];
}

- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions{

    static bool isOnce = false;
    for(SKPaymentTransaction *transaction in transactions){
        if (transaction.transactionState == SKPaymentTransactionStatePurchasing) {
            //called when the user is in the process of purchasing, do not add any of your own code here.
            NSLog(@"Transaction state -> Purchasing");
        }
        else if (transaction.transactionState == SKPaymentTransactionStatePurchased) {               
            [[SKPaymentQueue defaultQueue] finishTransaction:transaction];
            [self refresh_purchaseBtn];

            return;
        }
        else if (transaction.transactionState == SKPaymentTransactionStateRestored) {
            NSLog(@"Transaction state -> Restored");
            [self hideProgressHud];
            isProgress = NO;
            //add the same code as you did from SKPaymentTransactionStatePurchased here
            NSString *productID = transaction.payment.productIdentifier;
            if ([productID isEqualToString:kPURCHASE_ITEM_TYPE_A] || [productID isEqualToString:kPURCHASE_ITEM_TYPE_B] || [productID isEqualToString:kPURCHASE_ITEM_TYPE_C]) {
                [[InAppPurchaseManager sharedManager] savePurchaseItem:productID];
            }
            else {
                NSArray *arr = [Workout MR_findByAttribute:@"productId" withValue:productID inContext:ApplicationDelegate.managedObjectContext];
                if (arr.count > 0) {
                    Workout *workout = (Workout *)[arr firstObject];
                    workout.state = @"purchased";
                    [ApplicationDelegate.managedObjectContext MR_saveOnlySelfAndWait];
                }
                [[InAppPurchaseManager sharedManager] savePurchasedReadyProgram:productID];
            }

            if (!isOnce) {
//                UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:LocalizedString(@"Notice") message:LocalizedString(@"Purchased Item restored succesfully") delegate:self cancelButtonTitle:LocalizedString(@"Ok") otherButtonTitles: nil];
//                [alertView show];   
                isOnce = true;
            }

            [[SKPaymentQueue defaultQueue] finishTransaction:transaction];
            [self refresh_purchaseBtn];
        }
        else if (transaction.transactionState == SKPaymentTransactionStateFailed) {
            //called when the transaction does not finish
            if(transaction.error.code == SKErrorPaymentCancelled){
                NSLog(@"Transaction state -> Cancelled");
                //the user cancelled the payment
            }
            [self hideProgressHud];
            isProgress = NO;
            [[SKPaymentQueue defaultQueue] finishTransaction:transaction];
        }
    }
}

0 个答案:

没有答案