0

我的 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”......如果这封电子邮件未经确认有问题吗?

4

2 回答 2

1

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

于 2012-12-29T10:33:58.653 回答
0

好的,想通了...这是由ARC引起的...解决方案是在我使用它的地方保留存储工具包管理对象,否则它会更快地释放以使响应可以到达...

于 2013-01-01T19:58:31.610 回答