如果之前没有购买的In App产品,则显示警报

时间:2016-05-09 13:25:19

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

我已在我的应用中实施了In App购买和恢复购买方式。

如果之前没有购买的In App产品,我只想显示提醒信息。

在下面的当前示例中,付款方法会在还原请求后立即初始化。

// Buy Button that initiates In App purchase request
-(IBAction)buyButtonPressed:(id)sender {
    if (self.products != nil)
    {
        SKPayment *payment = [SKPayment paymentWithProduct:self.products];
        [[SKPaymentQueue defaultQueue]addTransactionObserver:self];
        [[SKPaymentQueue defaultQueue]addPayment:payment];
        [self.activityIndicator startAnimating];
    }
}

//Restore Button initiates In App Restore
- (IBAction)restoreButtonPressed:(id)sender {
    [[SKPaymentQueue defaultQueue] addTransactionObserver:self];
    [[SKPaymentQueue defaultQueue] restoreCompletedTransactions];
}

- (void)paymentQueueRestoreCompletedTransactionsFinished:(SKPaymentQueue *)queue
{
//Currently I’m displaying an alert here if there are no previous purchased products available.
    if(queue.transactions.count == 0)
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Alert" message:@"There is no restore available for your account" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
        [alert show];
        [self.activityIndicator stopAnimating];
        return;
    }

    NSMutableArray *purchasedItemIDs = [[NSMutableArray alloc] init];

        for (SKPaymentTransaction *transaction in queue.transactions)
        {
            NSString *productID = transaction.payment.productIdentifier;
            [purchasedItemIDs addObject:productID];
                if ([productID isEqualToString:@"com.mydomain.app.product”])
                {
                //Unlocks the App
                [self unlockApp];
                [self.activityIndicator stopAnimating];
                }
            }

purchasedItemIDs = nil;
}

//Payment QUEUE
-(void) paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions {
    for (SKPaymentTransaction *transaction in transactions) {
        switch (transaction.transactionState) {
        case SKPaymentTransactionStatePurchasing:
            {
            [self.activityIndicator startAnimating];
            break;
            }
        case SKPaymentTransactionStatePurchased:
            {
            [self unlockApp];
            [[SKPaymentQueue defaultQueue] finishTransaction:transaction];
            [self.activityIndicator stopAnimating];
            break;  
            } 
        case SKPaymentTransactionStateRestored:
            {
            [[SKPaymentQueue defaultQueue] finishTransaction:transaction];
            [self unlockApp];
            [self showAlert:@"Application successfully restored"];
            [self.activityIndicator stopAnimating];
            break; 
            }
    case SKPaymentTransactionStateFailed:
    {
        [[SKPaymentQueue defaultQueue] finishTransaction:transaction];
        [self.activityIndicator stopAnimating];
        break;        
     }
    case SKPaymentTransactionStateDeferred:
            {
            [[SKPaymentQueue defaultQueue] finishTransaction:transaction];
            [self.activityIndicator stopAnimating];
            break;
            }
            default:
            [[SKPaymentQueue defaultQueue] finishTransaction:transaction];
            [self.activityIndicator stopAnimating];
            break;
        }
    }
}

1 个答案:

答案 0 :(得分:1)

paymentQueueRestoreCompletedTransactionsFinished中查看if queue.transactions.count == 0。如果是,则没有要恢复的购买。例如:

-(void)paymentQueueRestoreCompletedTransactionsFinished:(SKPaymentQueue *)queue {
    // Check to see if there are purchases to restore
    if (queue.transactions.count == 0) {
        // No purchases to restore
        // Show alert
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"No Purchases"
                                                        message:@"There are no purchases to restore."
                                                       delegate:self
                                              cancelButtonTitle:@"Close"
                                              otherButtonTitles:nil];
        [alert show];
    }
    else {
        // There are purchases to restore
        // Do something else
    }
}

SKPaymentTransactionObserver Protocol Reference