ios - iAd showing a white banner -
i implemented in-app purchase remove ads in app. worked first time tried after ran app on phone few more times started show white ad banner instead of hidding ad banner used to.
here code startscreen.m of app purchaseviewcontroller.m buy iap remove ads. got warning saying using 10 instances of adbanner though have them removed whenever view disappers. thank , help.
// //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 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 retreive ad"); // check 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 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; (skproduct *product in products) { nslog(@"404: product not found: %@", product); } } -(void)paymentqueue:(skpaymentqueue *)queue updatedtransactions:(nsarray *)transactions { (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]; }
first off, answer question, reason banner isn't hiding when ad fails because you're not hiding it. whether or not [prefs boolforkey:@"removeads"] == true
, you're going want hide banner if don't want blank white bar in place. in simplest possible way (without animation), simplify didfailtoreceiveadwitherror:
method, so:
- (void)bannerview:(adbannerview *)banner didfailtoreceiveadwitherror:(nserror *)error { nslog(@"failed retreive ad"); // check remove ads iap self.adbanner.hidden = yes; _bannerisvisible = no; }
and hide in viewdidload
it's hidden beginning before ad has loaded:
- (void)viewdidload { //ads self.adbanner.delegate = self; self.adbanner.hidden = yes; }
that way, ad unhide in bannerviewdidloadad:
once ad has been loaded.
secondly, _bannerisvisible
boolean unnecessary. instead of using separate boolean check whether banner visible, can check whether it's hidden, if (self.adbanner.hidden == yes)
or if (self.adbanner.hidden == no)
or if (self.adbanner.hidden)
or if (!self.adbanner.hidden)
. eliminating unnecessary boolean cut down on potential error.
so, summarize, here's suggestion how bannerviewdidloadad:
, didfailtoreceiveadwitherror:
methods should look:
- (void)bannerviewdidloadad:(adbannerview *)banner { // check 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 retreive ad"); self.adbanner.hidden = yes; }
Comments
Post a Comment