iAd doesn't shows correct - ios

i'm developing dict for iOS, and try to use iAd for monetization. I done step by step with examples, except creating ad view, but ad doesn't show correct.
my code:
- (void)viewDidLoad
{
adView.frame = CGRectOffset(adView.frame, 0, -50); //don't work
adView.delegate=self;
self.bannerIsVisible=NO;
[super viewDidLoad];
}
-(void)bannerViewDidLoadAd:(ADBannerView *)banner{
if (!self.bannerIsVisible){
[UIView beginAnimations:#"animateAdBannerOn" context:NULL];
banner.frame = CGRectOffset(banner.frame, 0, 50); //works well
[UIView commitAnimations];
self.bannerIsVisible = YES;
}
}
-(void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error{
if (self.bannerIsVisible){
[UIView beginAnimations:#"animateAdBannerOff" context:NULL];
banner.frame = CGRectOffset(banner.frame, 0, -50);
[UIView commitAnimations];
self.bannerIsVisible = NO;
}
}
I work with storyboard, using autolayout, tried to set
self.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
Nothing helped.

Is it showing in the wrong position, or just not showing the ad when built in release mode? If it is in the wrong position, don't use the autoresizingMask and use the autolayout constraints in the storyboard instead. If it is not showing up after building in release/submitting to app store, then you most likely still need to register for the iAd network through iTunesConnect.
It looks like you are using iOS 7. There is a really easy way to add iAds if you don't mind it showing up on the bottom of the view:
#import <iAd/iAd.h>
yourViewController.canDisplayBannerAds = YES;
You do not need to implement any other show/hide methods if you use this property. Apple's WWDC 2013 video titled "iAd Integration and Best Practices" explains this in detail:
https://developer.apple.com/wwdc/videos/

Related

How to ads are displayed on iAds Banner in iOS programmatically

This is my first experience to integrate iAds. I used the following link
http://codewithchris.com/iad-tutorial/
I implemented it perfectly. My application showing AddBannerView perfect but ads hides and showing not working. And I added adBannerView by using storyboard and connect its delegates and IBOutlets.
-(void)bannerViewDidLoadAd:(ADBannerView *)banner {
if (! bannerIsVisible) {
// If banner isn't part of view hierarchy, add it
if (advertiseView.superview == nil) {
[self.view addSubview:advertiseView];
}
[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;
}
}
The tutorial you followed is dated. The reason your ADBannerView is ending up in the middle of your view over a period of time is because of your CGRectOffset in your ADBannerView's delegate methods. I'd guess its a problem with your bannerIsVisible BOOL.
Also, don't use the beginAnimations:context: method. From UIView Class Reference:
Use of this method is discouraged in iOS 4.0 and later. You should use
the block-based animation methods to specify your animations instead.
Here's an example of how to implement an ADBannerView programmatically. This example animates the alpha property of the ADBannerView to show or hide it. There's no need to set the ADBannerView's frame either. It will know which device it is on and size itself appropriately. Just setting it's position is sufficient.
#import "ViewController.h"
#import iAd; // Import iAd
#interface ViewController () <ADBannerViewDelegate> { // Include delegate
ADBannerView *adView; // Create globally
}
#end
#implementation ViewController
-(void)viewDidLoad {
[super viewDidLoad];
adView = [[ADBannerView alloc]init]; // Alloc/init
// Position
adView.center = CGPointMake(self.view.frame.size.width / 2,
self.view.frame.size.height - adView.frame.size.height / 2);
adView.delegate = self; // Set delegate
adView.alpha = 0.0; // Hide banner initially
[self.view addSubview:adView]; // Add to view
}
-(void)bannerViewDidLoadAd:(ADBannerView *)banner {
// Delegate method called when the ADBannerView receives an ad
NSLog(#"bannerViewDidLoadAd");
// Animate alpha change to show ADBannerView
[UIView animateWithDuration:1.0 animations:^{
adView.alpha = 1.0;
}];
}
-(void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error {
// Delegate method called when the ADBannerView fails
// Can fail for multiple reasons so lets print why its failing in our NSLog
NSLog(#"didFailToReceiveAdWithError: %#", error);
// Animate alpha change to hide ADBannerView
[UIView animateWithDuration:1.0 animations:^{
adView.alpha = 0.0;
}];
}

iAd banner not working on iOS 9

I'm getting didFailToReceiveAdWithError message in the console while running on the simulator and device.
iAd banners are displayed successfully when running on iOS 8. When running on iOS 9, iAd banners fail to receive an ad.
.h
#import <iAd/iAd.h>
#interface ViewController : UIViewController <ADBannerViewDelegate>
#property (retain, nonatomic) IBOutlet ADBannerView *adBanner;
.m
-(void)viewDidLoad {
self.adBanner = [[ADBannerView alloc]initWithFrame:CGRectMake(0,[UIScreen mainScreen].bounds.size.height-100, [UIScreen mainScreen].bounds.size.width, 50)];
self.adBanner.delegate=self;
[self.adBanner setBackgroundColor:[UIColor clearColor]];
[self.view addSubview:self.adBanner];
}
-(void)bannerViewWillLoadAd:(ADBannerView *)banner {
NSLog(#"bannerViewWillLoadAd");
}
-(void)bannerViewDidLoadAd:(ADBannerView *)banner {
// Show the ad banner.
NSLog(#"bannerViewDidLoadAd");
[UIView animateWithDuration:0.5 animations:^{
self.adBanner.alpha = 1.0;
}];
}
-(void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error {
NSLog(#"didFailToReceiveAdWithError");
// Hide the ad banner.
[UIView animateWithDuration:0.5 animations:^{
self.adBanner.alpha = 0.0;
}];
}
-(void)bannerViewActionDidFinish:(ADBannerView *)banner {
NSLog(#"Ad did finish");
}
When running on iOS 9, the console prints didFailToReceiveAdWithError every time.
I'm unable to recreate your issue. The iAd network may have been down for your country when testing this, you may be in a country that iAd does not support, or it may be that you've set your iAd Testing Fill Rate to 0% on your development device/simulator. Go to Settings>Developer>Fill Rate> and check that Fill Rate is set to 100% on your development device/simulator.
I'd suggest printing the error you're receiving in didFailToReceiveAdWithError so you can find out why the ADBannerView is failing.
-(void)viewDidLoad {
// The ADBannerView will size itself based on the device it is being displayed on
// Only setting the position is sufficient
self.adBanner = [[ADBannerView alloc]initWithFrame:CGRectMake(0, [UIScreen mainScreen].bounds.size.height-100, 0, 0)];
self.adBanner.delegate=self;
// Removed setBackgroundColor
// Set alpha to 0.0 initially
self.adBanner.alpha = 0.0;
[self.view addSubview:self.adBanner];
}
-(void)bannerViewWillLoadAd:(ADBannerView *)banner {
NSLog(#"bannerViewWillLoadAd");
}
-(void)bannerViewDidLoadAd:(ADBannerView *)banner {
NSLog(#"bannerViewDidLoadAd");
[UIView animateWithDuration:0.5 animations:^{
self.adBanner.alpha = 1.0;
}];
}
-(void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error {
// Changed NSLog to print the error that is received
NSLog(#"didFailToReceiveAdWithError: %#", error);
[UIView animateWithDuration:0.5 animations:^{
self.adBanner.alpha = 0.0;
}];
}
-(void)bannerViewActionDidFinish:(ADBannerView *)banner{
NSLog(#"bannerViewActionDidFinish");
}
If you're still having this issue you should contact iAd directly and update your question based on their response, or post an answer if they're able to solve it for you.
Try adding app transport security in your project's plist file.
I found this here :
Checking my storyboard I noticed, that a height constraint for 32 was set for the ADBannerView - the 32 was not a valid height in that orientation. Removing that height constraint removed the error "Ad inventory unavailable" and it worked beautifully from then on.
Check if that works for you.
Also check with the iAD Changelog to see if there's anything you might need to worry about.

iAD View Freezes On Ad Close

Ok, so after testing on the simulators it turns out that this issue is only occurring on actual devices... How would I fix this?
I am making an iOS app with SpriteKit and am implementing iAD. Everything works more or less like I expect it too, except for one thing. When I tap on the ad, it brings me to a full screen ad, as expected, but when I close that ad the current view freezes, as in nothing happens visually. I know the app is still running because when I click the banner ad again and close out of it again the game returns to normal and the game had progressed while visually frozen. This is how the banner is initialized in my view controller class (the iAD delegate):
self.canDisplayBannerAds = YES;
CGRect bannerFrame = CGRectMake(0, 20, scene.size.width, 50);
banner = [[ADBannerView alloc] initWithAdType:ADAdTypeBanner];
banner.delegate = self;
banner.frame = bannerFrame;
[banner setAlpha:0];
[self.view addSubview:banner];
And these are the loading methods, also in the view controller class:
- (void) bannerViewDidLoadAd:(ADBannerView *) bannerM
{
NSLog(#"Ad Loaded");
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1];
[bannerM setAlpha:1];
[UIView commitAnimations];
}
- (void) bannerView:(ADBannerView *)bannerM didFailToReceiveAdWithError:(NSError *)error
{
NSLog(#"Ad Failed");
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1];
[bannerM setAlpha:0];
[UIView commitAnimations];
}
I don't really understand the problem or why it is happening... Any help would be appreciated!
Thanks,
StrongJoshua
EDIT Here are the two methods that are called when the banner ads open and close:
- (BOOL) bannerViewActionShouldBegin:(ADBannerView *)banner willLeaveApplication:(BOOL)willLeave
{
if(playing)
{
NSLog(#"Ad Pause");
SKView *v = (SKView *)self.view;
NSLog(#"2");
SKScene *s = (SKScene *)v.scene;
NSLog(#"3");
WBPlayScene *p = (WBPlayScene *) s;
NSLog(#"4");
[p.logic pause:YES];
NSLog(#"Done");
}
return YES;
}
- (void) bannerViewActionDidFinish:(ADBannerView *)banner
{
if(playing)
{
NSLog(#"Ad Unpause");
[((WBPlayScene *)((SKView *)self.view).scene).logic pause:NO];
}
}
FIXED
The reason for all those NSLogs is because the game crashes when I try to pause it. The game crashes after "2" is reached, so at SKScene *s = (SKScene *)v.scene;. It gives the error [UIView scene]: unrecognized selector sent to instance and I don't understand why...
Solution: To fix this side issue I changed self.view to self.originalContentView and it got the SKView instead of the ad banner's view.
Thanks very much for your help!
SOLVED: I had to remove the call to enable ad display self.canDisplayBannerAds because it interfered with my self-created banner.

Is this the right way to supplement AdMob when iAd is unavailable?

I want to fill the iAd space with AdMob advertisements when iAd is unavailable and was wondering if this was the right way to code it to do so. Every time I've run my simulator it has displayed iAd, and when it doesn't it shows a white strip, so I'm wondering if I've coded it wrong. Could someone help me out, and fix the code if necessary, or add suggestions. Thanks.
//iAd Advertising
#pragma mark iAd Delegate Methods
- (void) bannerViewDidLoadAd:(ADBannerView *)banner {
advertisement.hidden = NO;
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1];
[banner setAlpha:1];
[UIView commitAnimations];
//bannerView_.hidden = YES;
} // if there is an internet connection, load the iAd with a 1 second fade in effect
- (void) bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error {
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1];
[banner setAlpha:1];
[UIView commitAnimations];
advertisement.hidden = YES;
bannerView_.hidden = NO;
bannerView_ = [[GADBannerView alloc]initWithFrame:CGRectMake(0, 20, 320, 50)];
bannerView_.adUnitID = #"//pubname";
bannerView_.rootViewController = self;
[self.view addSubview:bannerView_];
[bannerView_ loadRequest:[GADRequest request]];
}
#end
Your code is right. I'm guessing the error is in the storyboard. On the storyboard click the iAd and make sure the Alpha is set to 0. That should solve it.
Sorry for the late answer.

UIView Will Not Resize

My storyboard elements are subviews of containerView and containerView is a subview of the main view. I am trying to resize the height of my container view when an ad is available to show but I cannot get that to work. I am able to offset the view up, but I am not able to resize it. I have seen quite a bit a posts about this, but all suggestions I've read basically say to confirm Autolayout is not checked. I have confirmed that Autolayout is not checked in each element of my storyboard but still am not having success with this. The ad banner (placed just off screen at [0, 480] pops up nicely from the bottom just like I want it to, but it covers up my storyboard elements which is just plain UNACCEPTABLE. I will not stand for this! I need some help guys and gals…Please see code below:
-(void)bannerViewDidLoadAd:(ADBannerView *)banner
{
if (_bannerIsVisible == NO)
{
NSLog(#"Ad Loaded");
[UIView beginAnimations:#"animateAdbannerOn" context:nil];
//_containerView.frame = CGRectOffset(_containerView.frame, 0, -50);
_containerView.frame = CGRectMake(_containerView.frame.origin.x,
_containerView.frame.origin.y,
_containerView.frame.size.width,
_containerView.frame.size.height-50);
banner.frame = CGRectOffset(banner.frame, 0, -50);
[UIView commitAnimations];
_bannerIsVisible = YES;
}
}
Try it like this:
-(void)bannerViewDidLoadAd:(ADBannerView *)banner
{
if (_bannerIsVisible == NO)
{
NSLog(#"Ad Loaded");
[UIView beginAnimations:#"animateAdbannerOn" context:nil];
//_containerView.frame = CGRectOffset(_containerView.frame, 0, -50);
CGRect frame = CGRectMake(_containerView.frame.origin.x,
_containerView.frame.origin.y,
_containerView.frame.size.width,
_containerView.frame.size.height-50);
[_containerView setFrame:frame];
CGRect frame2 = banner.frame;
frame2.origin.x = 0;
frame2.origin.y = -50;
[banner setFrame:frame2];
[UIView commitAnimations];
_bannerIsVisible = YES;
}
}
You have just changed the frame of your containerView. This does not in any way change the frames of the subviews of containerView. You either have to make containerView a scrollview or change the frames of every component.

Resources