I am trying to create 2 iAds but I have only one on simulator. I can not now check it with device. Why I have only one? I have only second iAd
My code is:
adView1 = [[ADBannerView alloc] initWithAdType:ADAdTypeBanner];
adView1.frame = CGRectOffset(adView1.frame, 0, 50);
adView1.delegate = self;
[self.backgroundView addSubview:adView1];
adView2 = [[ADBannerView alloc] initWithAdType:ADAdTypeBanner];
adView2.frame = CGRectOffset(adView2.frame, 0, 200);
adView2.delegate = self;
[self.backgroundView addSubview:adView2];
- (void)bannerViewDidLoadAd:(ADBannerView *)banner
{
if (!self.bannerIsVisible)
{
[UIView beginAnimations:#"animateAdBannerOn" context:NULL];
// banner is invisible now and moved out of the screen on 50 px
if (banner == adView1)
{
banner.frame = CGRectOffset(banner.frame, 0, 50);
}
if (banner == adView2)
{
banner.frame = CGRectOffset(banner.frame, 0, 200);
}
[UIView commitAnimations];
self.bannerIsVisible = YES;
}
}
(void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error
{
if (self.bannerIsVisible)
{
[UIView beginAnimations:#"animateAdBannerOff" context:NULL];
// banner is visible and we move it out of the screen, due to connection issue
banner.frame = CGRectOffset(banner.frame, 0, -50);
[UIView commitAnimations];
self.bannerIsVisible = NO;
}
}
(BOOL)bannerViewActionShouldBegin:(ADBannerView *)banner willLeaveApplication:(BOOL)willLeave
{
NSLog(#"Banner view is beginning an ad action");
BOOL shouldExecuteAction = YES;
if (!willLeave && shouldExecuteAction)
{
[audio pause];
}
return shouldExecuteAction;
}
(void)bannerViewActionDidFinish:(ADBannerView *)banner
{
[audio resume];
}
adView1.frame = CGRectOffset(adView2.frame, 0, 50);
It must be CGRectOffset(adView1.frame, 0, 50); since adView2 is allocation in the next line only???
When you are creating first view, you are using second view's frame:
adView1 = [[ADBannerView alloc] initWithAdType:ADAdTypeBanner];
adView1.frame = CGRectOffset(adView2.frame, 0, 50);
adView1.delegate = self;
[self.backgroundView addSubview:adView1];
As adView2 is not created yet - it is nil and frame is (0,0,0,0)
You have to use adView1 frame for offset as you do for second view.
Also, keep in mind that displaying 2 banners is going against Apple guideline and not recommend. Take a look here (Best practices section)
https://developer.apple.com/library/ios/documentation/userexperience/conceptual/iAd_Guide/WorkingwithBannerViews/WorkingwithBannerViews.html
Related
I want to implement an iAd at the bottom of my screen. My app runs in landscape mode only, but the iAd is in portrait mode, its too big and I want it correct.
I instantiate it with this :
ADBannerView *adView = [[ADBannerView alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height - 50, 480, 32)];
[self.view addSubview:adView];
So i give it 480 x 32 but it is in 360x 50.
That makes no sense to me and I don't know what to do. In the other topic I read I have to disable Auto Layout, but it is disabled.
thanks for any help!
edit: even if I add the iAd via storyboard to the view in landscape format, it shows it in portrait format when i run the app.
edit: I think I don't have a ADBannerView Appdelegate method.
All I have for my iAd is :
GameViewcontroller.h
#interface GameViewController : UIViewController<ADBannerViewDelegate>
GameViewcontroller.m
#interface GameViewController ()
{
BOOL _bannerIsVisible;
ADBannerView *_adBanner;
}
-(void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
_adBanner = [[ADBannerView alloc] initWithFrame:CGRectMake((self.view.frame.size.width /2) - 180, self.view.frame.size.height, 480, 32)];
_adBanner.delegate = self;
[self.view addSubview:_adBanner];
}
- (void)bannerViewDidLoadAd:(ADBannerView *)banner
{
if (!_bannerIsVisible)
{
// If banner isn't part of view hierarchy, add it
if (_adBanner.superview == nil)
{
[self.view addSubview:_adBanner];
}
[UIView beginAnimations:#"animateAdBannerOn" context:NULL];
// Assumes the banner view is just off the bottom of the screen.
banner.frame = CGRectOffset(banner.frame, 0, - banner.frame.size.height);
[UIView commitAnimations];
_bannerIsVisible = YES;
}
}
- (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError: (NSError *)error
{
NSLog(#"Failed to retrieve ad");
if (_bannerIsVisible)
{
[UIView beginAnimations:#"animateAdBannerOff" context:NULL];
// Assumes the banner view is placed at the bottom of the screen.
banner.frame = CGRectOffset(banner.frame, 0, banner.frame.size.height);
[UIView commitAnimations];
_bannerIsVisible = NO;
}
}
In my app, I had added the ADBannerView into the UIViewController by code
in .h file
#property (strong, nonatomic) ADBannerView *adBannerView;
in .m file
- (void)viewDidLoad
{
[super viewDidLoad];
self.adBannerView = [[ADBannerView alloc] initWithFrame:CGRectMake(0, 518, 320, 50);];
[self.view addSubview:self.adBannerView];
}
#pragma mark - ADBannerViewDelegate
- (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error {
NSLog(#"bannerview did not receive any banner due to %#", error);
}
- (void)bannerViewActionDidFinish:(ADBannerView *)banner {
NSLog(#"bannerview was selected");
}
- (BOOL)bannerViewActionShouldBegin:(ADBannerView *)banner willLeaveApplication:(BOOL)willLeave {
NSLog(#"banner action should begin");
return YES;
}
- (void)bannerViewWillLoadAd:(ADBannerView *)banner {
NSLog(#"banner will loaded");
}
- (void)bannerViewDidLoadAd:(ADBannerView *)banner {
NSLog(#"banner was loaded");
}
My app is normally, it can show the ADBannerView. But now, I want to remove the ADBannerView out of the UIViewController, then I removed all the lines of code about the ADBannerView. I can build my app, but it cannot run, the error msg is:
Could not instantiate class named ADBannerView
Please help me to remove the ADBannerView. Thank you.
- (void)bannerViewDidLoadAd:(ADBannerView *)banner{
if (!bannerIsVisible){
//LOG_TYPE(#"AD show");
[UIView beginAnimations:#"animateAdBannerOn" context:NULL];
// banner is invisible now and moved out of the screen on 50 px
// banner.frame = CGRectOffset(banner.frame, 0, -50);
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone){
if (IS_IPHONE_5) {
//LOG_TYPE(#"view height: %f",self.view.bounds.size.height);
self.adView.frame = CGRectMake(0,568-49-50, self.view.frame.size.width, 50);
}
else{
//LOG_TYPE(#"view height: %f",self.view.bounds.size.height);
self.adView.frame = CGRectMake(0,480-49-50, self.view.frame.size.width, 50);
}
}
else{
self.adView.frame = CGRectMake(0,1024-56-50, self.view.frame.size.width, 50);
}
[UIView commitAnimations];
bannerIsVisible = YES;
[self performSelector:#selector(hideAD) withObject:nil afterDelay:6];
}
}
- (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error{
if (bannerIsVisible){
//LOG_TYPE(#"AD error");
[UIView beginAnimations:#"animateAdBannerOff" context:NULL];
// banner is visible and we move it out of the screen, due to connection issue
// banner.frame = CGRectOffset(banner.frame, 0, 50);
bannerIsVisible = NO;
[self performSelector:#selector(hideAD) withObject:nil afterDelay:0];
// [self performSelector:#selector(hideAD) withObject:nil afterDelay:5];
}
}
- (BOOL)bannerViewActionShouldBegin:(ADBannerView *)banner willLeaveApplication:(BOOL)willLeave{
//LOG_TYPE(#"Banner view is beginning an ad action");
BOOL shouldExecuteAction = YES;
if (!willLeave && shouldExecuteAction){
// stop all interactive processes in the app
// [video pause];
// [audio pause];
}
return shouldExecuteAction;
}
- (void)bannerViewActionDidFinish:(ADBannerView *)banner{
// resume everything you've stopped
// [video resume];
// [audio resume];
}
- (void)hideAD{
//LOG_TYPE(#"Hide AD");
[UIView beginAnimations:#"animateAdBannerOff" context:NULL];
// banner is visible and we move it out of the screen, due to connection issue
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone){
if (IS_IPHONE_5) {
//LOG_TYPE(#"view height: %f",self.view.bounds.size.height);
self.adView.frame = CGRectMake(0,568-49+50, self.view.frame.size.width, 50);
}
else{
//LOG_TYPE(#"view height: %f",self.view.bounds.size.height);
self.adView.frame = CGRectMake(0,480-49+50, self.view.frame.size.width, 50);
}
}
else{
self.adView.frame = CGRectMake(0,1024-56+50, self.view.frame.size.width, 50);
}
[UIView commitAnimations];
bannerIsVisible = NO;
[self performSelector:#selector(bannerViewDidLoadAd:) withObject:self.adView afterDelay:60];
}
-(void)viewDidLoad{
...
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone){
if (IS_IPHONE_5) {
self.adView = [[ADBannerView alloc] initWithFrame:CGRectMake(0,568-49-50, self.view.frame.size.width, 50)];
}
else{
self.adView = [[ADBannerView alloc] initWithFrame:CGRectMake(0,480-49-50, self.view.frame.size.width, 50)];
}
}
else{
self.adView = [[ADBannerView alloc] initWithFrame:CGRectMake(0,1024-56-50, self.view.frame.size.width, 50)];
}
[self.view addSubview:self.adView];
self.adView.delegate=self;
bannerIsVisible=NO;
self.adView.backgroundColor = [UIColor clearColor];
....
}
if you add ADBannerView in storyboard then remove the IBoutlet of ADBannerView and its delegate.
break connection of below figure for ADBannerView
remove ADBannerViewfrom storyboard and your above code is perfect.
Your error as below.
[self.adView removeFromSuperView];
I have an iAd that I created following this tutorial and for some reason when the iAd shows-up any animation going on at that time stops. In other words the animations work fine until the iAd apears and as soon as the iAd apears any animation going at that moment stops or it goes back to its original position without finishing the animation.
This is the code I have for the iAd
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
_adBanner = [[ADBannerView alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height, 320, 50)];
_adBanner.delegate = self;
}
- (void)bannerViewDidLoadAd:(ADBannerView *)banner
{
if (!_bannerIsVisible)
{
if (_adBanner.superview == nil)
{
[self.view addSubview:_adBanner];
}
[UIView beginAnimations:#"animateAdBannerOn" context:NULL];
banner.frame = CGRectOffset(banner.frame, 0, -banner.frame.size.height);
[UIView commitAnimations];
_bannerIsVisible = YES;
}
}
- (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error
{
if (_bannerIsVisible)
{
[UIView beginAnimations:#"animateAdBannerOff" context:NULL];
banner.frame = CGRectOffset(banner.frame, 0, banner.frame.size.height);
[UIView commitAnimations];
_bannerIsVisible = NO;
}
}
And this is one of the animations I have
- (IBAction)showHideBlackboard:(id)sender
{
[UIView animateWithDuration:0.6 delay: 0.0 options: UIViewAnimationOptionCurveEaseIn animations:^
{
self.controlsView.frame= CGRectMake(10, 10, 250, 250);
}completion:^(BOOL finished)
{
[UIView animateWithDuration:0.6 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^
{
self.drawingView.alpha = 1.0;
self.drawingView.frame= CGRectMake(10, 10, 350, 350);
}completion:nil ];
}];
}
Any idea why does the iAd interferes with UIView animations?
I recently had a similar problem. It turned out that avoiding the option
self.translatesAutoresizingMaskIntoConstraints = NO;
in my UIView subclass solved the problem.
currently I'm trying to implement iAd in my universal iOS7 app.
This is working absolutly fine on iphone but on the ipad i get this screwed up ads but the bannersize is correct.
is this a known issue? or is there a ipad special implementation I didn't knew?
[edit!]
I'm pretty sure there is only one banner! its really just showing the normal iad ad in that weird way.
If I click on the ad, the fullscreen one is also not right scaled but its shown on the hole screen. Here is my implementation:
- (void)viewDidLoad
{
_adBannerView = [[ADBannerView alloc] initWithAdType:ADAdTypeBanner];
_adBannerView.delegate = self;
_bannerIsVisible = NO;
[self.view addSubview:self.adBannerView];
...
}
- (void)viewDidLayoutSubviews {
CGRect contentFrame = _mainNavigation.frame, bannerFrame = CGRectZero;
bannerFrame.size = [_adBannerView sizeThatFits:contentFrame.size];
if (_adBannerView.bannerLoaded && !_bannerIsVisible) {
contentFrame.size.height -= bannerFrame.size.height;
bannerFrame.origin.y = contentFrame.origin.y + contentFrame.size.height;
_bannerIsVisible = YES;
} else {
bannerFrame.origin.y = contentFrame.origin.y + contentFrame.size.height;
}
_mainNavigation.frame = contentFrame;
_adBannerView.frame = bannerFrame;
}
- (void)bannerViewDidLoadAd:(ADBannerView *)banner {
[UIView animateWithDuration:0.25 animations:^{
[self.view setNeedsLayout];
[self.view layoutIfNeeded];
}];
}
- (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error {
NSLog(#"didFailToReceiveAdWithError %#", error);
[UIView animateWithDuration:0.25 animations:^{
[self.view setNeedsLayout];
[self.view layoutIfNeeded];
}];
}
And is there a way to show the test ads on a real device?
because I always get "Ad inventory unavailable"!
I have a strange problem. iAds work. As soon as I initialize a UIButton (using "[self initCloseAdButtonWithFrame:_frame];") The iAds stop working, while the button is initialized in the correct place. This code slides an ad banner in and out depending on the availability of the ad.
Why is the initialization of closeButton breaking the ads functionality?
ViewController.m
#pragma mark - Ads
-(void)initiAdBanner
{
DLog(#"");
if (!self.iAdBannerView)
{
CGRect rect = CGRectMake(0, self.view.frame.size.height, 0, 0);
self.iAdBannerView = [[ADBannerView alloc]initWithFrame:rect];
self.iAdBannerView.delegate = self;
self.iAdBannerView.hidden = TRUE;
[self.view addSubview:self.iAdBannerView];
}
}
#pragma mark - ADBanner delegate methods -
// Called before the add is shown, time to move the view
- (void)bannerViewWillLoadAd:(ADBannerView *)banner
{
DLog(#"iAd load");
[self hideBanner:self.gAdBannerView];
[self showBanner:self.iAdBannerView];
}
// Called when an error occured
- (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error
{
DLog(#"iAd error: %#", error);
[self hideBanner:self.iAdBannerView];
GADRequest *request = [GADRequest request];
request.testDevices = [NSArray arrayWithObjects:GAD_SIMULATOR_ID, nil];
[self.gAdBannerView loadRequest:request];
}
#pragma mark - Banner hide and show -
// Hide the banner by sliding down
-(void)hideBanner:(UIView*)banner
{
if (banner && ![banner isHidden])
{
[UIView beginAnimations:#"hideBanner" context:nil];
banner.frame = CGRectOffset(banner.frame, 0, banner.frame.size.height);
[UIView commitAnimations];
banner.hidden = TRUE;
if(closeButton){
[closeButton setHidden:TRUE];
}
}
}
// Show the banner by sliding up
-(void)showBanner:(UIView*)banner
{
if (banner && [banner isHidden])
{
CGRect _frame2 = CGRectOffset(banner.frame, 0, -banner.frame.size.height);
CGRect _frame = CGRectOffset(banner.frame, 0, -banner.frame.size.height);
[self initCloseAdButtonWithFrame:_frame];
[UIView beginAnimations:#"showBanner" context:nil];
banner.frame = _frame2;
[UIView commitAnimations];
banner.hidden = FALSE;
}
}
//button initialize
-(void)initCloseAdButtonWithFrame:(CGRect)frame{
DLog(#"");
closeButton = [UIButton buttonWithType:UIButtonTypeCustom];
[closeButton addTarget:self
action:#selector(inAppPurchase)
forControlEvents:UIControlEventTouchUpInside];
UIImage *_closeImage = [UIImage imageNamed:#"closeButton.png"];
UIImage *closeImage = [UIImage imageWithCGImage:_closeImage.CGImage scale:_closeImage.scale orientation:UIImageOrientationDown];
[closeButton setBackgroundImage:closeImage forState:UIControlStateNormal];
[closeButton setBackgroundImage:closeImage forState:UIControlStateHighlighted];
closeButton.frame = CGRectMake(frame.size.width - closeImage.size.width, -5 + self.view.frame.size.height -frame.size.height - closeImage.size.height, closeImage.size.width, closeImage.size.height);
[self.view insertSubview:closeButton atIndex:2];
[closeButton setHidden:FALSE];
}
-(void)inAppPurchase{
DLog(#"");
}
ShowBanner is called via a delegate of iAd. In Show banner, the button is initialized. The code works (ads show up) only if the button is not initialized.
In particular, everything works fine until the button is added to the subview.
The issue is - you are using the same frame to show button and the banner. As a result, the button is covering it fully. Reduce _frame.size.height inside showBanner function and you will see the banner.