我的 iOS 应用程序中有一个应用程序内购买,允许用户升级到应用程序的完整版本。我做了一个应该处理商店的类。问题是我在测试商店时收到无效的 ProductID 错误。我已经确定问题是无法从应用商店获取产品...
我该如何解决?
这是代码:
#import <Foundation/Foundation.h>
#import <StoreKit/StoreKit.h>
@interface PSStoreKitManager : NSObject <SKProductsRequestDelegate,SKPaymentTransactionObserver>
@property (strong, nonatomic) NSArray *products;
-(void)initStore;
-(void)upgradeToFull;
@end
@implementation PSStoreKitManager
@synthesize products = _products;
-(void)initStore {
if ([SKPaymentQueue canMakePayments]) {
[self requestProductData];
}else {
NSLog(@"payments disabbled");
}
NSLog(@"prod id %@",kFullVersionProductId);
}
-(void)upgradeToFull {
SKProduct *fullVersion;
NSLog(@"no items in array products %d",[self.products count]);
for (SKProduct *crt in self.products) {
if ([crt.productIdentifier isEqualToString:kFullVersionProductId]) {
fullVersion = crt;
}
}
SKPayment *payment = [SKPayment paymentWithProduct:fullVersion];
[[SKPaymentQueue defaultQueue] addPayment:payment];
}
-(void)requestProductData {
SKProductsRequest *request = [[SKProductsRequest alloc] initWithProductIdentifiers:[NSSet setWithObject:kFullVersionProductId]];
[request setDelegate:self];
[request start];
}
-(void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response {
self.products = response.products;
NSLog(@"data in response: %d",[response.products count]);
}
#pragma mark -- Transaction observer
-(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
{
// Your application should implement these two methods.
[self recordTransaction:transaction];
[self provideContent:transaction.payment.productIdentifier];
// Remove the transaction from the payment queue.
[[SKPaymentQueue defaultQueue] finishTransaction: transaction];
}
- (void) restoreTransaction: (SKPaymentTransaction *)transaction
{
[self recordTransaction: transaction];
[self provideContent: transaction.originalTransaction.payment.productIdentifier];
[[SKPaymentQueue defaultQueue] finishTransaction: transaction];
}
- (void) failedTransaction: (SKPaymentTransaction *)transaction
{
if (transaction.error.code != SKErrorPaymentCancelled) {
// Optionally, display an error here.
}
[[SKPaymentQueue defaultQueue] finishTransaction: transaction];
}
-(void)recordTransaction:(SKPaymentTransaction *)transaction {
if ([transaction.payment.productIdentifier isEqualToString:kFullVersionProductId]) {
[[NSUserDefaults standardUserDefaults] setValue:transaction.transactionReceipt forKey:kUpgradeToFullReceipt];
[[NSUserDefaults standardUserDefaults] synchronize];
}
}
-(void)provideContent:(NSString *)productId {
if ([productId isEqualToString:kFullVersionProductId]) {
NSUserDefaults *def = [NSUserDefaults standardUserDefaults];
[def setBool:YES forKey:kIsUpgradedToFull];
[def synchronize];
}
}
@end
这是在应用程序委托中:
//set up store observer
PSStoreKitManager *observer = [[PSStoreKitManager alloc] init];
[[SKPaymentQueue defaultQueue] addTransactionObserver:observer];
这被设置applicationDidFinishLaunching
为商店观察者。它是这样使用的:
PSStoreKitManager *store = [[PSStoreKitManager alloc] init];
[store initStore];
[store upgradeToFull];
另一个问题:当我在 itunnes 中注册一个测试用户时,连接它通过电子邮件发送该地址并要求通过使用我的 Apple Id 登录来确认它......我试过了,但它说“未知的 Apple id”......如果这封电子邮件未经确认有问题吗?