在应用程序中购买非消费品

时间:2011-04-27 18:24:13

标签: iphone in-app-purchase

我写了以下代码。

但是当我执行此代码时,我得到了日志,显示“没有可用的产品”。 我无法找到原因。

-(void)viewDidLoad {

    [super viewDidLoad];

if ([SKPaymentQueue canMakePayments]) 
{

SKProductsRequest *productsRequest=[[SKProductsRequest alloc]initWithProductIdentifiers:[NSSet setWithObject:@"com.cmp.name.prdt"]];

productsRequest.delegate=self;
[productsRequest start];

}

else {
NSLog(@"Parental Control are enabled");
}

}


-(IBAction)btnpurchase

{

NSString* isPurchased = [[NSUserDefaults standardUserDefaults] stringForKey:@"com.cmp.name.prdt"];

if ([@"purchased" compare:isPurchased]==NSOrderedSame)
{

///do some task

}

else
{

SKPayment * payment=[SKPayment paymentWithProductIdentifier:@"com.cmp.name.prdt"];
[[SKPaymentQueue defaultQueue] addTransactionObserver:self];
[[SKPaymentQueue defaultQueue] addPayment:payment];

}

}


-(void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response

{

SKProduct *validProduct=nil;

int count=[response.products count];

if (count>0) {

validProduct=[response.products objectAtIndex:0];

}

else if(!validProduct)

{

NSLog(@"no products available");

}

}

-(void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions
{

for (SKPaymentTransaction *transaaction in transactions)

{

switch (transaaction.transactionState)

{

case SKPaymentTransactionStatePurchasing:

break;

case SKPaymentTransactionStatePurchased:

[[SKPaymentQueue defaultQueue]finishTransaction:transaaction];
[[NSUserDefaults standardUserDefaults] setObject:@"purchased" forKey:@"com.cmp.name.prdt"];
[[NSUserDefaults standardUserDefaults] synchronize];

break;

case SKPaymentTransactionStateRestored:

[[SKPaymentQueue defaultQueue]finishTransaction:transaaction];

break;

case SKPaymentTransactionStateFailed:

if (transaaction.error.code!=SKErrorPaymentCancelled) {

NSLog(@"Error encountered");

}

[[SKPaymentQueue defaultQueue] finishTransaction:transaaction];

break;

default:

break;

}

}

}

有人可以帮我解决这个问题吗?

2 个答案:

答案 0 :(得分:1)

您可以尝试使用以下代码 //产品请求代理方法

- (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response
{

    NSLog(@"The product request didReceiveResponse :%@",[response description]);
    NSLog(@"The products are :%@",[response.products description]);

    NSLog(@"The invalidProductIdentifiers are:%@",[response.invalidProductIdentifiers description]);

    NSArray *products=response.products;


    for(SKProduct *currentProduct in products){

         NSLog(@"THE Product price is :%@",currentProduct.price);
         NSLog(@"THE Product description is :%@",currentProduct.localizedDescription);
         NSLog(@"THE Product title is :%@",currentProduct.localizedTitle);
         NSLog(@"THE Product's product identifier is :%@",currentProduct.productIdentifier);

         }

}

这将记录在控制台窗口连接的iTUNES上注册的产品详细信息

答案 1 :(得分:0)

很遗憾,Apple没有回复任何有关您的购买未显示的有用信息。我会经历这个checklist。在我的应用程序中,事情几天都没有工作,然后突然开始工作。确保您的财务信息正确无误。