StoreKit没有收到产品阵列

时间:2012-12-29 10:23:46

标签: ios in-app-purchase storekit

我在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

这是在app delegate:

  //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登录来确认它...我尝试了但是它说&#34;未知的苹果ID&# 34; ......如果这封电子邮件未经证实,这是一个问题吗?

2 个答案:

答案 0 :(得分:1)

您需要在sandbox环境中测试应用内购买。如果您在iTunes连接中设置了测试用户,则需要在设备设置中明确地从Apple商店注销。然后在提示您是否要登录以允许应用程序内购买时启动应用程序后,使用您的测试用户帐户登录。

答案 1 :(得分:0)

好的想通了......它是由ARC造成的...解决方案是保留我使用它的商店套件管理对象,否则它的发布速度更快,响应可以到达......