我已经浏览了谷歌,几乎每一个关于添加应用内购买的stackoverflow问题,但它们似乎都不起作用。当我尝试在我的应用程序上进行购买时,一切正常,产品可用且一切,但有关购买的警报视图从未显示出来。以下是IAP的所有代码:
- (IBAction)tapsRemoveAdsButton{
NSLog(@"User taps remove ads button");
[self tapsRemoveAds];
}
- (void)tapsRemoveAds{
NSLog(@"User requests to remove ads");
if([SKPaymentQueue canMakePayments]){
NSLog(@"User can make payments");
SKProductsRequest *productsRequest = [[SKProductsRequest alloc] initWithProductIdentifiers:[NSSet setWithObject:kRemoveAdsProductIdentifier]];
productsRequest.delegate = self;
[productsRequest start];
}
else{
NSLog(@"User cannot make payments due to parental controls");
}
}
- (IBAction)purchase{
SKProduct *removeAdsProductID = (SKProduct *)kRemoveAdsProductIdentifier;
SKPayment *payment = [SKPayment paymentWithProduct:(SKProduct *)kRemoveAdsProductIdentifier];
[[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];
NSLog(@"Products Avalable!");
//[self sloganizeWord];
}
else if(!validProduct){
NSLog(@"No products avalable");
}
}
- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions{
for(SKPaymentTransaction *transaction in transactions){
switch (transaction.transactionState){
case SKPaymentTransactionStatePurchasing: NSLog(@"Transaction state -> Purchasing");
break;
case SKPaymentTransactionStatePurchased: [self doRemoveAds];
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
NSLog(@"Transaction state -> Purchased");
break;
case SKPaymentTransactionStateRestored:
NSLog(@"Transaction state -> Restored");
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
break;
case SKPaymentTransactionStateFailed:
if(transaction.error.code != SKErrorPaymentCancelled){
NSLog(@"Transaction state -> Cancelled");
}
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
break;
}
}
}
- (void)doRemoveAds{
areAdsRemoved = YES;
[[NSUserDefaults standardUserDefaults] setBool:areAdsRemoved forKey:@"areAdsRemoved"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
如果我正确地执行此操作,警报视图应显示在方法“paymentQueue”中,所有在我获得的控制台中:
user taps remove ads button
user requests to remove ads
user can make payments
products available!
直到paymentQueue方法,它还应该记录
transaction state -> purchasing
和其他人,我已经在iTunes Connect中设置了IAP,并且IAP的ID是正确的。 (否则它不会记录产品可用)我在iOS7上使用非越狱设备并且我已经设置了所有配置文件(或者至少我认为,因为我已经使用广告将应用程序提交到应用商店)同样,我已经看了一些其他问题,但它们似乎都没有给我任何帮助。提前感谢任何帮助过的人和每个人:)
答案 0 :(得分:0)
这不行:SKProduct *removeAdsProductID = (SKProduct *)kRemoveAdsProductIdentifier;
- 您无法使用NSString*
代替SKProduct*
。
您应该使用validProduct
中的- (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response
。
您只需要1个按钮/操作tapRemoveAds
即可开始购买(不需要purchase
IBAction
)。
您需要掌握实际的SKProduct*
:
- (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response{
SKProduct *validProduct = nil;
int count = [response.products count];
if(count > 0){
validProduct = [response.products objectAtIndex:0];
NSLog(@"Products Avalable!");
// initiate the actual purchase
[self purchase:validProduct];
}
else if(!validProduct){
NSLog(@"No products avalable");
}
}
-(void)purchase:(SKProduct *)product{
SKPayment *payment = [SKPayment paymentWithProduct:product];
[[SKPaymentQueue defaultQueue] addTransactionObserver:self];
[[SKPaymentQueue defaultQueue] addPayment:payment];
}
更好的解决方案是在上一步中检索有效产品列表(例如:应用程序启动时),并且仅在upgrade
对象可用时启用SKProduct *
按钮
还有其他问题,例如不处理恢复(应该像处理SKPaymentTransactionStatePurchasing
一样处理)。我建议您查看In-App Purchase Programming Guide以便更好地理解。