-1

我环顾了谷歌,几乎每一个关于添加应用内购买的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 上使用的是非越狱设备,并且我已经设置了所有配置文件(或者至少我认为,因为我已经将应用程序提交到带有广告的应用程序商店) 同样,我已经查看了其他一些问题,但似乎没有一个问题对我有任何帮助。在此先感谢所有提供帮助的人:)

4

1 回答 1

0

这不行:SKProduct *removeAdsProductID = (SKProduct *)kRemoveAdsProductIdentifier;- 你不能使用 aNSString*而不是 a SKProduct*

您应该改用validProductfrom - (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以获得更好的理解。

于 2013-10-15T07:48:01.890 回答