How do I position AdMob ad above the Tabbar? - iphone

I've gotten the latest version of the iOS code for AdMob. It is working fine, but the ad shows up under the tabbar I have. How do I move the ad up above the bar? Here's my relevant code:
#import "InformationViewController.h"
#implementation InformationViewController
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
// Create a view of the standard size at the bottom of the screen.
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
/* run something specific for the iPad */
bannerView_ = [[GADBannerView alloc]
initWithFrame:CGRectMake(0.0,
self.view.frame.size.height -
GAD_SIZE_300x250.height,
GAD_SIZE_300x250.width,
GAD_SIZE_300x250.height)];
}
else
{
/* run something specific for the iPhone */
bannerView_ = [[GADBannerView alloc]
initWithFrame:CGRectMake(0.0,
self.view.frame.size.height -
GAD_SIZE_320x50.height,
GAD_SIZE_320x50.width,
GAD_SIZE_320x50.height)];
}
// Specify the ad's "unit identifier." This is your AdMob Publisher ID.
bannerView_.adUnitID = MY_BANNER_UNIT_ID;
// Let the runtime know which UIViewController to restore after taking
// the user wherever the ad goes and add it to the view hierarchy.
bannerView_.rootViewController = self;
[self.view addSubview:bannerView_];
// Initiate a generic request to load it with an ad.
[bannerView_ loadRequest:[GADRequest request]];
}
And my header file -
#import "GADBannerView.h"
#import <UIKit/UIKit.h>
#interface InformationViewController : UIViewController {
GADBannerView *bannerView_;
}
#end

You can try this;
bannerView_ = [[GADBannerView alloc]
initWithFrame:CGRectMake(0.0,
100.0 -
GAD_SIZE_320x50.height,
GAD_SIZE_320x50.width,
GAD_SIZE_320x50.height)];
use any other number below of 100 to move the banner up. Hope this work for you!

Related

How to show loading animation in a view - IOS

I have a view in which I want to show loading animation. I have seen some application they are showing circular image to show loading, and the action will happen on background, Same thing I want to achieve here, Any inbuilt animation is available in IOS?
TIA
You can use the built in activity indicator.
UIActivityIndicatorView *indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
indicator.center = CGPointMake(alert.bounds.size.width / 2 , (alert.bounds.size.height) /2);
[indicator startAnimating];
simply add it as a subview in to your view.
You may use the UIActivityIndicator if you want to keep things simple. Or there are plenty of open source activity indicators that do a lot of fancy stuff in addition to just showing a spinning wheel. MBProgressHUD and SVProgressHUD are two neat implementations.
Create YourViewController, and then add the the MBProgressHUB library to your project (you can get the library from here); download the project and move the library to your project.
Then you can use the following code to achieve your task:
YourViewController.h
#import <UIKit/UIKit.h>
#import "MBProgressHUD.h"
#interface YourViewController : UITableViewController <MBProgressHUDDelegate>
{
MBProgressHUD *hud;
}
YourViewController.m
#import "YourViewController.h"
#interface YourViewController ()
#end
#implementation YourViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self initializeProgressLoading];
[self getObjects];
[hud hide:YES afterDelay:1];
}
-(void) initializeProgressLoading {
hud = [[MBProgressHUD alloc] initWithView:self.navigationController.view];
[self.navigationController.view addSubview:hud];
hud.delegate = self;
hud.labelText = #"Loading";
[hud showWhileExecuting:#selector(sleep) onTarget:self withObject:nil animated:YES];
}
- (void)sleep {
sleep(50000);
}
- (void) getObjects {
// connect to db and get all objects
//you can write any thing here
}
- (void)hudWasHidden:(MBProgressHUD *)hud1 {
// Remove HUD from screen when the HUD was hidded
[hud removeFromSuperview];
hud = nil;
}

Declaring custom UINavigationController in xcode 4.2

I was wondering how can declare a custom UINavigationController in Xcode 4.2 ? I have created a project which uses an API and needs UINavigationController my project does not use story boards and is viewBased application. thanks
I wrote my own. The key is to subclass UIViewController AND remember to set self.title and the icon to its first "contained" class's otherwise nothing will show up on the tabBarIcons. UINavigationController is only one level deep from UIViewController, so you can view the header and easily see what it implements, but those were the only real keys to "copy over".
In Interface Builder, assuming you have a nib to go along with it, make a main view that's the size of the screen (311 if you have a tabBar and Status Bar), then create a top view that's IB-outletted to be the navigation bar, and a lower view that's outletted as the container. Then do something like this:
note: I messed with the center point as I ran into many issues regarding trying to move the views without having them offset by the pixel height, even though I'm aware of relative positioning of subviews, it just didnt' work for some reason, even if only moving sideways
I'm posting this code because nobody seems to have this type of thing up. This might help someone, or more.
Stephen Johnson.
import "CustomNavigationController.h"
#implementation CustomNavigationController
#synthesize backgroundImg, title1, title2, title3;
- (id) initWithRootViewController:(UIViewController*)c;
{
self = [super initWithNibName:nil bundle:nil];
if (self) {
containedControllers = [[NSMutableArray alloc] initWithObjects:c, nil];
self.title1.text = c.title; //a custom outlet for text of title, resembling the NavigationController's title basically
[container addSubview:c.view];
c.view.frame = container.bounds;
back.hidden = YES; //backbutton
c.customNavigationController = self;
self.title = c.title;
self.tabBarItem.image = c.tabBarItem.image;
}
return self;
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (void) dealloc;
{
[containedControllers removeAllObjects];
[containedControllers release];
[super dealloc];
}
- (void) pushViewController:(UIViewController*)v animated:(BOOL)a;
{
float w = container.frame.size.width;
float h = container.frame.size.height;
[containedControllers addObject:v];
[self.view addSubview:v.view];
// v.view.frame = CGRectMake(w,0,w,h);
v.view.frame = container.bounds;
v.view.center = CGPointMake(v.view.center.x + w, v.view.center.y + container.frame.origin.y);
v.customNavigationController = self;
float time = a ? 0.31 : 0;
UIViewController * lastViewController = nil;
lastViewController = (UIViewController*)[containedControllers lastObject];
[UIView animateWithDuration:time animations:^{
for (UIViewController * c in containedControllers) {
// c.view.frame = CGRectMake(c.view.frame.origin.x + w*direction, 0, w, h);
c.view.center = CGPointMake(c.view.center.x + w*-1, c.view.center.y);
}
} completion:^(BOOL finished) {
self.title1.text = v.title;
back.hidden = NO;
}];
}
- (void) popViewControllerAnimated:(BOOL)a;
{
float w = container.frame.size.width;
float h = container.frame.size.height;
float time = a ? 0.31 : 0;
float direction = 1;
[UIView animateWithDuration:time animations:^{
for (UIViewController * c in containedControllers) {
// c.view.frame = CGRectMake(c.view.frame.origin.x + w*direction, 0, w, h);
c.view.center = CGPointMake(c.view.center.x + w*direction, c.view.center.y);
}
} completion:^(BOOL finished) {
// lastViewController = (UIViewController*)[containedControllers lastObject];
[containedControllers removeLastObject];
self.title1.text = ((UIViewController*)[containedControllers lastObject]).title;
if ([containedControllers count] > 1) {
back.hidden = NO;
}
else
back.hidden = YES;
}];
}
- (IBAction) popLastVC;
{
[self popViewControllerAnimated:YES];
}
#end
It's quite simple to subclass a UINavigationController through inheritance. It's a key concept of OOP.
//YourClass.h
#interface YourClass : UINavigationController
#end
//YourClass.m
#implementation YourClass
#end
But
This class is generally used as-is but may be subclassed in iOS 6 and later.
as written in the Overview of UINavigationController. So you may not be able to subclass a UINavigationController if you are supporting iOS 5 or earlier. Maybe your subclass could not work correctly. You can find a good discussion on this stackoverflow topic.

Implementations on AdMob (iOS)

How going, dudes?
So today it's a really ridiculous question, but i just can't get it to work the way i want.
I'm implementing the AdMob stuff but i want to get some additional information.
They gave the instructions on the GoogleCodes/AdMob
but i can't make it give me the logs i want.
This is on the .h
#import <UIKit/UIKit.h>
#import "GADBannerView.h"
#import "GADBannerViewDelegate.h"
#interface AppForAllTestsViewController : UIViewController<GADBannerViewDelegate>
This is on the .m
-(void)adViewDidReceiveAd:(GADBannerView *)bannerView{
NSLog(#"RECEIVED");
[UIView beginAnimations:#"BannerSlide" context:nil];
bannerView.frame = CGRectMake(0.0, self.view.frame.size.height - bannerView.frame.size.height,
bannerView.frame.size.width,
bannerView.frame.size.height);
[UIView commitAnimations];
}
-(void)adView:(GADBannerView *)view didFailToReceiveAdWithError:(GADRequestError *)error{
NSLog(#"FAILURE RECEIVING AD: %#", [error localizedDescription]);
}
-(void)adMobProcess{
adMobBanner = [[GADBannerView alloc]initWithFrame:CGRectMake(0.0,
self.view.frame.size.height-GAD_SIZE_320x50.height,
GAD_SIZE_320x50.width,
GAD_SIZE_320x50.height)];
adMobBanner.adUnitID = my_id;
adMobBanner.rootViewController = self;
[self.view addSubview:adMobBanner];
[adMobBanner loadRequest:[GADRequest request]];
}
-(void)awakeFromNib{
NSLog(#"GOOD MORNING");
[adMobBanner setDelegate:self];
}
- (void)viewDidLoad{
[self adMobProcess];
[super viewDidLoad];
}
You can see that i'm setting the delegate to "self" but nothing happens, none of the changes, can any one, please, help me? (:
The AdMob iOS documentation states:
Remember to set the delegate using the setDelegate: before making the request for an ad.
Because you set the delegate in awakeFromNib, you are either setting the delegate before initializing the adMobBanner (NPE?), or you are setting it after the adRequest. You should instead set the delegate in adMobProcess before calling loadRequest.
Download the Google Admob Library https://developers.google.com/mobile-ads-sdk/download#downloadios
Except the Add-ons and readme.txt file drag the all file to your project
Build Settings -> Search path -> Library Search Path and Header Search Path add the Google Admob File Link as RECURSIVE $(SRCROOT)/Folder_Name Folder_Name Specifies where the Library Placed
Dont Use self.view.frame.size.height for iPhone and iPad this differs the banner height...
Use [[UIScreen mainScreen] bounds].size.height this will automatically identify the iPhone and iPad display Screen Bounds
Adjust the Height place the banner in Correct Position
adViewDidReceiveAd its a delegate method so in .h file u must call the delegate method
<GADBannerViewDelegate>
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
bannerView_ = [[GADBannerView alloc]
initWithFrame:CGRectMake(0.0,
[[UIScreen mainScreen] bounds].size.height - 150,
GAD_SIZE_728x90.width + 50,
GAD_SIZE_728x90.height)];
}
else if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
{
bannerView_ = [[GADBannerView alloc]
initWithFrame:CGRectMake(0.0,
[[UIScreen mainScreen] bounds].size.height - 113,
GAD_SIZE_320x50.width,
GAD_SIZE_320x50.height)];
}
// Specify the ad unit ID.
bannerView_.adUnitID = #"****************";
[bannerView_ setDelegate:self];
bannerView_.rootViewController = self;
[bannerView_ loadRequest:[GADRequest request]];
}
- (void)adViewDidReceiveAd:(GADBannerView *)view
{
[self.view addSubview:bannerView_];
}
I found that I had to set the frame when initializing the banner view in order for it to make use of the delegate.
// Delegate methods NOT CALLED
self.bannerView = [[DFPBannerView alloc] init];
// Delegate methods ARE CALLED
self.bannerView = [[DFPBannerView alloc] initWithFrame:CGRectMake(0, 0, 1, 1)];

AdBannerView shared across multiple views, including the rootviewcontroller, how?

No answers yet, but i've got an update that seems to have fixed the problem, any ideas why it works now though?!
I've got it to work in the way I intended by doing the following to every class that needs to display an adBanner:
1. In the layoutForCurrentOrientation method I added the following:
adBanner.delegate = self;
[self.view addSubview:adBanner];
2. In the deAlloc method on each class, I removed the following:
[adBanner removeFromSuperview];
Original question:
I'm attempting to use the iAdSuite sample code from Apple to use a single adBanner instance shared across all my views.
The sample implementation is designed to show the adbanner on each view that is called by the rootViewController, however, I would like my app to have ads on the rootViewController view also.
In my amended code:-
When I fire up the app, no banner is shown on the rootView, even though a method is called to request an ad banner. The class is set as the delegate for the ad and the delegate methods are available. These are called and the log for (adBanner.bannerLoaded) is NO.
As it is a shared object, if I switch views to from the rootView the ad is displayed in the other view.
When I return back to the rootView, the delegate method log shows that a banner is loaded, and it is positioned in a visible portion of the view. But the banner isn't visible.
In summary, i'm using the iAdSuite sample code for the AdBannerNavigation project, and trying to use it so that ad banners show on all views, including the rootViewController.
Any help appreciated!
The code i'm using is available here:
http://developer.apple.com/library/ios/#samplecode/iAdSuite/Introduction/Intro.html
My amended rootViewController.h:
#import <UIKit/UIKit.h>
#import <iAd/iAd.h>
#interface RootViewController : UITableViewController <ADBannerViewDelegate>
#end
My amended rootViewController.m
#import "RootViewController.h"
#import "TextViewController.h"
#import "MapViewController.h"
#import "AdBannerNavigationAppDelegate.h"
// for SharedAdBannerView macro
// #define SharedAdBannerView ((AdBannerNavigationAppDelegate *)[[UIApplication sharedApplication] delegate]).adBanner
#import <iAd/iAd.h>
#interface RootViewController()
// Layout the Ad Banner and Content View to match the current orientation.
// The ADBannerView always animates its changes, so generally you should
// pass YES for animated, but it makes sense to pass NO in certain circumstances
// such as inside of -viewDidLoad.
- (void)layoutForCurrentOrientation:(BOOL)animated;
// A simple method that creates an ADBannerView
// Useful if you need to create the banner view in code
// such as when designing a universal binary for iPad
- (void)createADBannerView;
#end
- (void)viewDidLoad
{
[super viewDidLoad];
[self createADBannerView];
[self layoutForCurrentOrientation:NO];
}
- (void)viewDidUnload
{
ADBannerView *adBanner = SharedAdBannerView;
adBanner.delegate = nil;
[adBanner removeFromSuperview];
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self layoutForCurrentOrientation:NO];
}
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
[self layoutForCurrentOrientation:YES];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}
- (void)dealloc
{
ADBannerView *adBanner = SharedAdBannerView;
adBanner.delegate = nil;
[adBanner removeFromSuperview];
[super dealloc];
}
- (void)createADBannerView
{
ADBannerView *adBanner = SharedAdBannerView;
NSString *contentSize;
if (&ADBannerContentSizeIdentifierPortrait != nil)
{
contentSize = UIInterfaceOrientationIsPortrait(self.interfaceOrientation) ? ADBannerContentSizeIdentifierPortrait : ADBannerContentSizeIdentifierLandscape;
}
else
{
contentSize = UIInterfaceOrientationIsPortrait(self.interfaceOrientation) ? ADBannerContentSizeIdentifier320x50 : ADBannerContentSizeIdentifier480x32;
}
CGRect frame;
frame.size = [ADBannerView sizeFromBannerContentSizeIdentifier:contentSize];
frame.origin = CGPointMake(0.0f, CGRectGetMaxY(self.view.bounds));
adBanner.frame = frame;
adBanner.delegate = self;
adBanner.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleTopMargin;
adBanner.requiredContentSizeIdentifiers =
(&ADBannerContentSizeIdentifierPortrait != nil) ?
[NSSet setWithObjects:ADBannerContentSizeIdentifierPortrait, ADBannerContentSizeIdentifierLandscape, nil] :
[NSSet setWithObjects:ADBannerContentSizeIdentifier320x50, ADBannerContentSizeIdentifier480x32, nil];
[self.view addSubview:adBanner];
}
- (void)layoutForCurrentOrientation:(BOOL)animated
{
ADBannerView *adBanner = SharedAdBannerView;
CGFloat animationDuration = animated ? 0.2f : 0.0f;
CGRect contentFrame = self.view.bounds;
CGPoint bannerOrigin = CGPointMake(CGRectGetMinX(contentFrame), CGRectGetMaxY(contentFrame));
CGFloat bannerHeight = 0.0f;
if (UIInterfaceOrientationIsLandscape(self.interfaceOrientation))
adBanner.currentContentSizeIdentifier = (&ADBannerContentSizeIdentifierLandscape != nil) ? ADBannerContentSizeIdentifierLandscape : ADBannerContentSizeIdentifier480x32;
else
adBanner.currentContentSizeIdentifier = (&ADBannerContentSizeIdentifierPortrait != nil) ? ADBannerContentSizeIdentifierPortrait : ADBannerContentSizeIdentifier320x50;
bannerHeight = adBanner.bounds.size.height;
if (adBanner.bannerLoaded)
{
contentFrame.size.height -= bannerHeight;
bannerOrigin.y -= bannerHeight;
}
else
{
bannerOrigin.y += bannerHeight;
}
[UIView animateWithDuration:animationDuration
animations:^{
adBanner.frame = CGRectMake(bannerOrigin.x, bannerOrigin.y, adBanner.frame.size.width, adBanner.frame.size.height);
}];
NSLog(#"%f is y pos, height=%f, is it loaded...%#", adBanner.frame.origin.y, adBanner.frame.size.height, adBanner.bannerLoaded?#"YES":#"NO");
}
- (void)bannerViewDidLoadAd:(ADBannerView *)banner
{
[self layoutForCurrentOrientation:YES];
}
- (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error
{
[self layoutForCurrentOrientation:YES];
}
- (BOOL)bannerViewActionShouldBegin:(ADBannerView *)banner willLeaveApplication:(BOOL)willLeave
{
return YES;
}
- (void)bannerViewActionDidFinish:(ADBannerView *)banner
{
}
#end

viewDidLoad method not being called using presentModalDialog method

I am using presentModalDialog to show a viewcontroller's view however the viewDidLoad never gets called?!
Is there any method that is called where I can place my logic that fills and configures the view?
EDIT:
It's a bit difficult to put a small amount of code, you kind of need to see it all, but here goes:
I have 2 nibs and 2 view controllers (portrait-mainvc/landscape) which both inherit from 1 baseclass which has the logic and the iboutlets. This is to allow me to re-use code. When the orientation changes in the main controller, it switches between the 2 controllers (modal dialog) which in turn use their respective nib's however they all use the same base code to configure the UI items.
#implementation HomeViewControllerBase
- (void)configureBestSellItems
{
[self startRetrievingRegions];
// load all the images from our bundle and add them to the scroll view
// NSUInteger i;
for (int i = 0; i <= 150; i++)
{
NSString *imageName = #"tempImage.jpg";
UIImage *image = [UIImage imageNamed:imageName];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
// setup each frame to a default height and width, it will be properly placed when we call "updateScrollList"
CGRect rect = imageView.frame;
rect.size.height = kScrollObjHeight;
rect.size.width = kScrollObjWidth;
imageView.frame = rect;
imageView.tag = i; // tag our images for later use when we place them in serial fashion
[self.bestSellScrollView addSubview:imageView];
[imageView release];
}
[self layoutScrollImages]; // now place the photos in serial layout within the scrollview
}
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
[super viewDidLoad];
[self configureBestSellItems];
}
#end
#interface HomeViewController : HomeViewControllerBase
{
}
#interface HomeViewController_Landscape : HomeViewControllerBase
{
}
#implementation HomeViewController
//This works for portrait
- (void)viewDidLoad
{
[super viewDidLoad];
}
#end
#implementation HomeViewController_Landscape
//This does not work
- (void)viewDidLoad
{
[super viewDidLoad];
}
//This works as suggested
- (void)awakeFromNib
{
[super configureBestSellItems];
}
#end
You can try these in your viewcontroller:
-(void)awakeFromNib
{
[super awakeFromNib];
// your code here
}
or
-(void) viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
// your code here
}
However, you should try to find why viewDidLoad is never called. You can also check this
link.
You may have a custom function in there named presentModalDialog:, as searching apple's docs doesn't have that function. You're calling a custom function that loads a Xib.
I'm doing the same thing.
You want this:
[self presentModalViewController:controller...
ViewDidLoad is called by Controllers, if the awakeFromNib is being called, you're probably getting, by itself, the view.
It's a common problem in objective c:
you should review the flowing:
1-initiation of the view controller you should initialize it in the right way in app delegate.
2-review the file owner of the view it must be the right class which usually the same name of uiview.