gpt4 book ai didi

ios - 应用内购买无法加载产品列表

转载 作者:可可西里 更新时间:2023-11-01 04:58:21 24 4
gpt4 key购买 nike

我正在尝试实现一些应用内购买。我已经按照在线教程进行操作,我的代码发布在下面。我已将应用内购买添加到我的 iTunes 连接帐户,但当我运行代码时,它会调用“didfailwitherror”方法,该方法无法加载产品列表。有没有其他人遇到过这个问题并且可能知道解决方案?

#import "IAPHelper.h"
#import <StoreKit/StoreKit.h>
NSString *const IAPHelperProductPurchasedNotification = @"IAPHelperProductPurchasedNotification";

@interface IAPHelper () <SKProductsRequestDelegate, SKPaymentTransactionObserver>

@end

@implementation IAPHelper{
SKProductsRequest *_productsRequest;
RequestProductsCompletionHandler _completionHandler;
NSSet * _productIdentifiers;
NSMutableSet * _purchasedProductIdentifiers;
}

-(id)initWithProductIdentifiers:(NSSet *)productIdentifiers{

NSLog(@"get product ids");

if((self=[self init])){
_productIdentifiers = productIdentifiers;
_purchasedProductIdentifiers = [NSMutableSet set];
[[SKPaymentQueue defaultQueue] addTransactionObserver:self];
for (NSString* productIdentifier in _productIdentifiers) {
NSLog(@"product id is %@", productIdentifier);

BOOL productPurchased = [[NSUserDefaults standardUserDefaults] boolForKey:productIdentifier];
if (productPurchased) {
[_purchasedProductIdentifiers addObject:productIdentifier];
}
}
}
return self;
}

-(void)requestProductsWithCompletionHandler:(RequestProductsCompletionHandler)completionHandler{

NSLog(@"Attempt to complete request for products");

_completionHandler = [completionHandler copy];
_productsRequest = [[SKProductsRequest alloc] initWithProductIdentifiers:_productIdentifiers];
_productsRequest.delegate=self;
[_productsRequest start];
}

#pragma mark - SKProductsRequestDelegate

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

NSLog(@"Loaded list of products...");
_productsRequest = nil;

NSArray * skProducts = response.products;
for (SKProduct * skProduct in skProducts) {
NSLog(@"Found product: %@ %@ %0.2f",
skProduct.productIdentifier,
skProduct.localizedTitle,
skProduct.price.floatValue);
}

_completionHandler(YES, skProducts);
_completionHandler = nil;

}

- (void)request:(SKRequest *)request didFailWithError:(NSError *)error {

NSLog(@"Failed to load list of products.");
_productsRequest = nil;

_completionHandler(NO, nil);
_completionHandler = nil;

}

- (BOOL)productPurchased:(NSString *)productIdentifier {
return [_purchasedProductIdentifiers containsObject:productIdentifier];
}

- (void)buyProduct:(SKProduct *)product {

NSLog(@"Buying %@...", product.productIdentifier);

SKPayment * payment = [SKPayment paymentWithProduct:product];
[[SKPaymentQueue defaultQueue] addPayment:payment];

}

- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions
{
for (SKPaymentTransaction * transaction in transactions) {
switch (transaction.transactionState)
{
case SKPaymentTransactionStatePurchased:
[self completeTransaction:transaction];
break;
case SKPaymentTransactionStateFailed:
[self failedTransaction:transaction];
break;
case SKPaymentTransactionStateRestored:
[self restoreTransaction:transaction];
default:
break;
}
};
}
- (void)completeTransaction:(SKPaymentTransaction *)transaction {
NSLog(@"completeTransaction...");

[self provideContentForProductIdentifier:transaction.payment.productIdentifier];
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
}

- (void)restoreTransaction:(SKPaymentTransaction *)transaction {
NSLog(@"restoreTransaction...");

[self provideContentForProductIdentifier:transaction.originalTransaction.payment.productIdentifier];
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
}

- (void)failedTransaction:(SKPaymentTransaction *)transaction {

NSLog(@"failedTransaction...");
if (transaction.error.code != SKErrorPaymentCancelled)
{
NSLog(@"Transaction error: %@", transaction.error.localizedDescription);
}

[[SKPaymentQueue defaultQueue] finishTransaction: transaction];
}

- (void)provideContentForProductIdentifier:(NSString *)productIdentifier {
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
int currentHints = [userDefaults integerForKey:@"hintAmount"];
[_purchasedProductIdentifiers addObject:productIdentifier];
if ([productIdentifier isEqual: @"1"]) {
[userDefaults setInteger:(currentHints+50) forKey:@"hintAmount"];
}else if ([productIdentifier isEqual: @"2"]){
[userDefaults setInteger:(currentHints+100) forKey:@"hintAmount"];
}else if ([productIdentifier isEqual: @"3"]){
[userDefaults setInteger:(currentHints+200) forKey:@"hintAmount"];
}else if ([productIdentifier isEqual: @"4"]){
[userDefaults setInteger:(currentHints+500) forKey:@"hintAmount"];
}
[[NSUserDefaults standardUserDefaults] synchronize];
[[NSNotificationCenter defaultCenter] postNotificationName:IAPHelperProductPurchasedNotification object:productIdentifier userInfo:nil];

}


@end

最佳答案

仅供引用 - 您必须在设备上运行该应用才能正确获取适用于 IAP 的产品。我遇到了一个问题,它把所有产品标识符吐出为“未购买”,但当我试图在表格 View 中显示它们时却说“无法加载产品列表”。这是在模拟器中运行时。一旦我在设备上运行,就完美无缺!

我使用本教程获取了完成所有繁重工作的基本帮助程序文件: http://www.raywenderlich.com/21081/introduction-to-in-app-purchases-in-ios-6-tutorial

关于ios - 应用内购买无法加载产品列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22146398/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com