我正在嘗試實施一些inapp購買。我遵循在線教程,我的代碼在下面發佈。我已將應用內購買添加到我的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
我覺得沒有代碼的問題你得到無效的,因爲你沒有完成的一些步驟我提一些條件請這 – morroko