如何在iOS目标C中进行应用内购买?

时间:2018-08-03 11:59:48

标签: objective-c xcode9.4 ios11.4

我正在尝试在应用程序中添加应用内购买。借助一些YouTube视频和其他教程,我在项目中执行了一些代码。但是点击按钮没有任何反应。这是我已集成到我的项目中的代码。请帮助我完成此任务

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

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

}
}

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

    [[SKPaymentQueue defaultQueue] addTransactionObserver:self];
    [[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){

            NSLog(@"Transaction state -> Restored");

            [self buyBasicEdition];
            [[SKPaymentQueue defaultQueue] finishTransaction:transaction];
            break;
        }
    }
}

- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions{
    for(SKPaymentTransaction *transaction in transactions){

        switch(transaction.transactionState){
            case SKPaymentTransactionStatePurchasing: NSLog(@"Transaction state -> Purchasing");

                break;
            case SKPaymentTransactionStatePurchased:

                [self buyBasicEdition];
                [[SKPaymentQueue defaultQueue] finishTransaction:transaction];
                NSLog(@"Transaction state -> Purchased");
                break;
            case SKPaymentTransactionStateRestored:
                NSLog(@"Transaction state -> Restored");

                [[SKPaymentQueue defaultQueue] finishTransaction:transaction];
                break;
            case SKPaymentTransactionStateFailed:

                if(transaction.error.code == SKErrorPaymentCancelled){
                    NSLog(@"Transaction state -> Cancelled");

                }
                [[SKPaymentQueue defaultQueue] finishTransaction:transaction];
                break;
        }
    }
}

-(void)buyBasicEdition {

    [[NSUserDefaults standardUserDefaults]setObject:@"low" forKey:@"points"];
    UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    SelectTeamVC *viewVc = [storyBoard instantiateViewControllerWithIdentifier:@"SelectTeamVC"];
    [self.navigationController  pushViewController:viewVc animated:YES];
}

这是我点击以初始化付款过程的按钮

- (IBAction)edition1:(id)sender {

    if([SKPaymentQueue canMakePayments]){
        NSLog(@"User can make payments");

        SKProductsRequest *productsRequest = [[SKProductsRequest alloc] initWithProductIdentifiers:[NSSet setWithObject:_productId]];
        productsRequest.delegate = self;
        [productsRequest start];

    }
    else{
        NSLog(@"User cannot make payments due to parental controls");

    }

}

编辑:我在控制台上看到的日志

  

Taboo [1649:358442]用户可以付款2018-08-03 18:23:10.954499 + 0530禁忌[1649:358442]可用产品! 2018-08-03 18:23:10.956229 + 0530忌讳[1649:358442]交易状态->采购

0 个答案:

没有答案