2014-03-03 35 views
5

我正在嘗試實施一些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 
+0

我覺得沒有代碼的問題你得到無效的,因爲你沒有完成的一些步驟我提一些條件請這 – morroko

回答

-1

Fullfill這些

1.Have啓用了應用內購買你的應用程序ID的所有條件?

2.您是否已檢查Cleared for sale for your product?

3.您是否已提交(並可以拒絕)您的應用程序二進制文件?

4.您的項目的.plist套件ID是否與您的App ID匹配?

5.您是否爲新App ID生成並安裝了新的供應配置文件?

6.您是否使用此新配置文件將項目配置爲代碼簽名?

7.您是否爲iPhone OS 3.0或更高版本構建?

8.製作SKProductRequest時是否使用完整產品ID?

9.將產品添加到iTunes Connect後,您是否等了幾個小時?

10.您的銀行信息是否在iTunes Connect上處於活動狀態? (via Mark)

11.您是否嘗試從您的設備中刪除應用程序並重新安裝?

如果您對這些問題中的任何一個回答「否」,則說明您的問題。

+0

我還沒有提交我的二進制文件,但在開發中測試應用程序時,這是否有必要? –

+0

如果您只是爲了測試目的而實現,那麼不需要提交二進制文件。您是否在控制檯中獲得無效的產品標識符或空的產品列表? – morroko

+0

既不是,它直接跳到失敗的錯誤,不知道如何檢查產品ID是否有效 –

1

僅供參考 - 您必須在設備上運行應用程序以正確獲取IAP產品。我遇到了將所有產品標識符分配爲「未購買」的問題,但當我試圖在桌面視圖中顯示它們時,則顯示「未能加載產品列表」。這是在模擬器中運行的時候。一旦我在設備上運行,完美工作!

我用這個教程中獲得基本的輔助文件做所有繁重: http://www.raywenderlich.com/21081/introduction-to-in-app-purchases-in-ios-6-tutorial