应用内购买产品请求不起作用
Posted
技术标签:
【中文标题】应用内购买产品请求不起作用【英文标题】:In App Purchase Product Request Not Working 【发布时间】:2013-05-18 04:08:20 【问题描述】:我正在尝试将一些应用内购买添加到我的 ios 应用中。一切都完成了,但我似乎无法让这些 iap 工作。我从一个教程网站上提取了一些代码,它似乎在那个项目上工作,我不确定我做错了什么。这些 iap 用于游戏中的金币,可以多次使用。
.h
#import <UIKit/UIKit.h>
#import <StoreKit/StoreKit.h>
@interface IAP : UIViewController <SKProductsRequestDelegate, SKPaymentTransactionObserver, UIAlertViewDelegate>
+(void)myIAPWithItem:(NSString *)Item;
@end
.m
#import "IAP.h"
#import "Money.h"
@interface IAP ()
@end
@implementation IAP;
#define kStoredData @"com.emirbytes.IAPNoobService"
+(void)myIAPWithItem:(NSString *)Item
if ([SKPaymentQueue canMakePayments])
NSString *PurchaseAddress = [[NSString alloc] initWithString:[NSString stringWithFormat:@"TTE_%@kGold" , Item]];
PurchaseAddress 是 itunesconnect 中这些 iap 的 appid。
SKProductsRequest *request = [[SKProductsRequest alloc] initWithProductIdentifiers:[NSSet setWithObject:PurchaseAddress]];
request.delegate = self;
[request start];
else
UIAlertView *tmp = [[UIAlertView alloc]
initWithTitle:@"Prohibited"
message:@"Parental Control is enabled, cannot make a purchase!"
delegate:self
cancelButtonTitle:nil
otherButtonTitles:@"Ok", nil];
[tmp show];
#pragma mark StoreKit Delegate
-(void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions
for (SKPaymentTransaction *transaction in transactions)
switch (transaction.transactionState)
case SKPaymentTransactionStatePurchasing:
// show wait view here
break;
case SKPaymentTransactionStatePurchased:
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
// remove wait view and unlock feature 2
UIAlertView *tmp = [[UIAlertView alloc]
initWithTitle:@"Complete"
message:@"You have unlocked Feature 2!"
delegate:self
cancelButtonTitle:nil
otherButtonTitles:@"Ok", nil];
[tmp show];
// apply purchase action - hide lock overlay and
NSLog(@"It worked!");
// do other thing to enable the features
break;
case SKPaymentTransactionStateRestored:
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
// remove wait view here
break;
case SKPaymentTransactionStateFailed:
if (transaction.error.code != SKErrorPaymentCancelled)
NSLog(@"Error payment cancelled");
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
// remove wait view here
break;
default:
break;
-(void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response
// remove wait view here
SKProduct *validProduct = nil;
int count = [response.products count];
if (count>0)
validProduct = [response.products objectAtIndex:0];
SKPayment *payment = [SKPayment paymentWithProductIdentifier:@"com.emirbytes.IAPNoob.01"];
[[SKPaymentQueue defaultQueue] addTransactionObserver:self];
[[SKPaymentQueue defaultQueue] addPayment:payment];
else
UIAlertView *tmp = [[UIAlertView alloc]
initWithTitle:@"Not Available"
message:@"No products to purchase"
delegate:self
cancelButtonTitle:nil
otherButtonTitles:@"Ok", nil];
[tmp show];
-(void)requestDidFinish:(SKRequest *)request
-(void)request:(SKRequest *)request didFailWithError:(NSError *)error
NSLog(@"Failed to connect with error: %@", [error localizedDescription]);
#pragma mark AlertView Delegate
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
@end
所以当我运行它时,什么也没有发生。我可以看到它发送了产品请求,但之后什么也没有。有谁知道我做错了什么?
【问题讨论】:
【参考方案1】:您是否检查过您的 response.products 计数是否为零? 你不带产品购买错误吗? 尝试提供更多信息,也许可以提供更好的帮助。 另外我认为这不是您购买的产品 ID
SKPayment *payment = [SKPayment paymentWithProductIdentifier:@"com.emirbytes.IAPNoob.01"];
【讨论】:
我解决了这个问题,但我不确定它是什么。今晚晚些时候将发布代码。以上是关于应用内购买产品请求不起作用的主要内容,如果未能解决你的问题,请参考以下文章
应用内购买在 Apple Store 中不起作用,但在沙盒中起作用