这让我很疯狂,但我正试图通过应用内购买来移除iAd。我的应用程序是完整的,除了这一部分,我不能为我的生活弄清楚如何做到这一点。我已经通过在故事板中使用iAd横幅然后添加代码
将iAd添加到应用程序-(void)bannerViewDidLoadAd:(ADBannerView *)banner {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1];
[banner setAlpha:1];
[UIView commitAnimations];
}
- (void)bannerView:(ADBannerView *) banner didFailToReceiveAdWithError:error{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1];
[banner setAlpha:0];
[UIView commitAnimations];
}
在我的.m中,iAd在我的模拟器中工作,但我想要通过应用内购买来删除它们。我已经在iTunes连接中完成了这个过程以允许这个但我无法弄清楚Xcode中的编码需要实现这个
我试过实现这个
#define kRemoveAdsProductIdentifier @"put your product id (the one that we just made in iTunesConnect) in here"
- (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");
//this is called the user cannot make payments, most likely due to parental controls
}
}
- (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 Available!");
[self purchase:validProduct];
}
else if(!validProduct){
NSLog(@"No products available");
//this is called if your product id is not valid, this shouldn't be called unless that happens.
}
}
- (IBAction)purchase:(SKProduct *)product{
SKPayment *payment = [SKPayment paymentWithProduct:product];
[[SKPaymentQueue defaultQueue] addTransactionObserver:self];
[[SKPaymentQueue defaultQueue] addPayment:payment];
}
- (IBAction) restore{
//this is called when the user restores purchases, you should hook this up to a button
[[SKPaymentQueue defaultQueue] restoreCompletedTransactions];
}
- (void) paymentQueueRestoreCompletedTransactionsFinished:(SKPaymentQueue *)queue
{
NSLog(@"received restored transactions: %i", queue.transactions.count);
for (SKPaymentTransaction *transaction in queue.transactions)
{
if(SKPaymentTransactionStateRestored){
NSLog(@"Transaction state -> Restored");
//called when the user successfully restores a purchase
[self doRemoveAds];
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
break;
}
}
}
- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions{
for(SKPaymentTransaction *transaction in transactions){
switch (transaction.transactionState){
case SKPaymentTransactionStatePurchasing: NSLog(@"Transaction state -> Purchasing");
//called when the user is in the process of purchasing, do not add any of your own code here.
break;
case SKPaymentTransactionStatePurchased:
//this is called when the user has successfully purchased the package (Cha-Ching!)
[self doRemoveAds]; //you can add your code for what you want to happen when the user buys the purchase here, for this tutorial we use removing ads
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
NSLog(@"Transaction state -> Purchased");
break;
case SKPaymentTransactionStateRestored:
NSLog(@"Transaction state -> Restored");
//add the same code as you did from SKPaymentTransactionStatePurchased here
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
break;
case SKPaymentTransactionStateFailed:
//called when the transaction does not finnish
if(transaction.error.code != SKErrorPaymentCancelled){
NSLog(@"Transaction state -> Cancelled");
//the user cancelled the payment ;(
}
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
break;
}
}
}
答案 0 :(得分:0)
您需要检查用户是否已购买IAP。如果他已完成,请删除iAd视图(或setAlpha = 0)
<强>更新强>
用户完成付款的那一刻(您收到SKPaymentTransactionStatePurchased
/ SKPaymentTransactionStateRestored
的地方,将标记保存到NSUserDefaults
,以便您知道用户是否购买了IAP,并显示/相应地隐藏iAd。
如果NSUserDefaults
中没有任何内容,则表示:(1)用户尚未购买IAP或(2)用户已购买但不知何时信息已在NSUserDefaults
中丢失(例如用户删除并重新安装应用程序)。在任何一种情况下,正确的方法是要求用户购买IAP。如果(2)发生,App Store将自动恢复IAP,您将收到SKPaymentTransactionStateRestored
。 SKPaymentTransactionStateRestored
和SKPaymentTransactionStatePurchased
的代码应该类似:设置NSUserDefaults
标记。
为避免混淆,某些应用会提供还原按钮,如果用户已购买了某些IAP,则会调用[SKPaymentQueue restoreCompletedTransactions]
并生成SKPaymentTransactionStateRestored
。如果用户没有购买任何IAP,则无效。