我成功实施了应用内购买,以便在我的应用中删除广告。它是我第一次尝试它,但在我在手机上运行应用程序几次之后,它开始只是显示一个白色广告横幅,而不是像过去那样隐藏广告横幅。
以下是我的应用程序StartScreen.m的代码以及购买IAP以删除广告的PurchaseViewController.m。我还得到一个警告,说我正在使用10个ADBanner实例,即使我在视图消失时删除了它们。感谢您的帮助。
//
//StartScreen.m
//
@interface StartScreen ()
{
BOOL _bannerIsVisible;
}
@end
@implementation StartScreen
- (void)viewDidLoad {
//Ads
self.adBanner.delegate = self;
}
- (void)viewWillDisappear:(BOOL)animated
{
[self.adBanner removeFromSuperview];
self.adBanner.delegate = nil;
self.adBanner = nil;
}
- (void)bannerViewDidLoadAd:(ADBannerView *)banner
{
// Check for Remove Ads IAP
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
if ([prefs boolForKey:@"RemoveAds"] == TRUE) {
self.adBanner.hidden = YES;
_bannerIsVisible = NO;
} else if (!_bannerIsVisible)
{
self.adBanner.hidden = NO;
_bannerIsVisible = YES;
}
}
- (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error
{
NSLog(@"Failed to retreive ad");
// Check for Remove Ads IAP
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
if ([prefs boolForKey:@"RemoveAds"] == TRUE) {
self.adBanner.hidden = YES;
_bannerIsVisible = NO;
}
}
//
// PurcahseViewController.m
//
#import "PurcahseViewController.h"
@implementation PurcahseViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.productID = @"com.app.iap1";
[self getProductID:self];
self.buyButton.enabled = NO;
NSLog(@"%@", self.productID);
}
- (void)getProductID:(UIViewController *)viewController {
if ([SKPaymentQueue canMakePayments]) {
SKProductsRequest *request = [[SKProductsRequest alloc] initWithProductIdentifiers:[NSSet setWithObject:self.productID]];
request.delegate = self;
[request start];
} else {
self.productDescription.text = @"Please enable in app purchase in your settings";
}
}
- (IBAction)purchaseItem:(id)sender {
SKPayment *payment = [SKPayment paymentWithProduct:self.product];
[[SKPaymentQueue defaultQueue] addPayment:payment];
}
- (IBAction)restoreButton:(id)sender {
[[SKPaymentQueue defaultQueue] addTransactionObserver:self];
[[SKPaymentQueue defaultQueue] restoreCompletedTransactions];
}
-(void)paymentQueueRestoreCompletedTransactionsFinished:(SKPaymentQueue *)queue {
[self UnlockPurchase];
}
- (void)Purchased {
NSLog(@"Item has been purchased");
}
#pragma mark _
#pragma mark SKProductsRequestDelegate
-(void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response {
NSArray *products = response.products;
if (products.count != 0) {
self.product = products[0];
self.buyButton.enabled = YES;
self.productTitle.text = self.product.localizedTitle;
self.productDescription.text = self.product.localizedDescription;
} else {
self.productTitle.text = @"404: Product Not Found";
}
products = response.invalidProductIdentifiers;
for (SKProduct *product in products) {
NSLog(@"404: Product Not Found: %@", product);
}
}
-(void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions {
for (SKPaymentTransaction *transaction in transactions) {
switch (transaction.transactionState) {
case SKPaymentTransactionStatePurchased: [self UnlockPurchase];
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
break;
case SKPaymentTransactionStateFailed:NSLog(@"Transaction Failed");
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
default:
break;
}
}
}
-(void)UnlockPurchase {
self.buyButton.enabled = NO;
[self.buyButton setTitle:@"Purchased" forState:UIControlStateDisabled];
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
[prefs setBool:TRUE forKey:@"RemoveAds"];
[prefs synchronize];
[self Purchased];
}
答案 0 :(得分:0)
首先,要回答您的问题,您的广告在广告失败时隐藏的原因是因为您没有隐藏它。无论是否[prefs boolForKey:@"RemoveAds"] == TRUE
,如果您不想在其位置使用空白条,您就会想要隐藏该横幅。要以最简单的方式(没有任何动画)这样做,请简化didFailToReceiveAdWithError:
方法,如下所示:
- (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error
{
NSLog(@"Failed to retreive ad");
// Check for Remove Ads IAP
self.adBanner.hidden = YES;
_bannerIsVisible = NO;
}
并将其隐藏在viewDidLoad
中,以便在广告加载之前隐藏起来:
- (void)viewDidLoad {
//Ads
self.adBanner.delegate = self;
self.adBanner.hidden = YES;
}
这样,只有在广告成功加载后,您的广告才会在bannerViewDidLoadAd:
中取消隐藏。
其次,您的_bannerIsVisible
布尔值是不必要的。您可以使用if (self.adBanner.hidden == YES)
或if (self.adBanner.hidden == NO)
或if (self.adBanner.hidden)
或if (!self.adBanner.hidden)
来检查它是否隐藏,而不是使用单独的布尔值来检查横幅是否可见。消除不必要的布尔值将减少潜在的错误。
所以,总而言之,我的建议是关于bannerViewDidLoadAd:
和didFailToReceiveAdWithError:
方法的外观:
- (void)bannerViewDidLoadAd:(ADBannerView *)banner {
// Check for Remove Ads IAP
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
if ([prefs boolForKey:@"RemoveAds"] == TRUE) {
self.adBanner.hidden = YES;
} else if (self.adBanner.hidden) {
self.adBanner.hidden = NO;
}
}
- (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error
{
NSLog(@"Failed to retreive ad");
self.adBanner.hidden = YES;
}