Problem with the scrollbar of a UIWebView - iphone

Im using a UIWebView to access a website, when i rotate the phone (landscape) the UIWebView is properly resized and the scrollbar are on the right place (on the right edge...) but when i acess any of input fields to fill the information required and exit it the UIWebView scrollbar jumps to the middle of screen (looks like it get back to 320, the width of the screen on portrait). Some useful info, this program was created using IB, have lots of outlets, im thinking about in do (redo) everything programmatically cause i was not the author of the first version... If anyone have seen this before plz let me know..
Thanks in advance!

Dont add your webview into self.view just assign it.Simple thing works like awesome
wv = [[UIWebView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
[self.view addSubview:wv];
instead do
self.view = wv;

In my test case I found that I can replicate this problem by going to a web site that accepts input, invoking the keyboard, dismissing the keyboard (with or without entering any input) and now the vertical scroll bar's display is in the Portrait position.
I tested with and without a NIB and the results are consistent. Once the keyboard is invoked, the vertical scroll bar will remain at the Portrait coordinates when the phone (or simulator) is in Landscape mode.
I submitted case# 7428286.

I received a response from Apple about the possible bug reported, they asked for a sample code that can reproduce the problem, i sent to them and im posting it here so you can test too:
//
// viewController.m
// TesteWebview
//
// Created by GecanMobile01 on 13/11/09.
// Copyright xxx All rights reserved.
//
#import "viewController.h"
#implementation viewController
#synthesize vw, wv;
/*
// The designated initializer. Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
// Custom initialization
}
return self;
}
*/
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
vw = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
[vw setBackgroundColor:[UIColor grayColor]];
vw.autoresizesSubviews = YES;
self.view = vw;
wv = [[UIWebView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
[self.view addSubview:wv];
}
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
NSURL *urlId = [NSURL URLWithString:#"https://www.google.com"];
NSURLRequest *theRequest = [NSURLRequest requestWithURL:urlId cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:(NSTimeInterval)10.0];
[wv loadRequest:theRequest];
[super viewDidLoad];
}
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
CGRect cgrWebView;
BOOL bChangeOrientation = NO;
if (interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight){
if (interfaceOrientation == UIInterfaceOrientationPortrait) {
cgrWebView = CGRectMake(0.0, 0.0, 320.0, 460.0);
bChangeOrientation = YES;
} else {
cgrWebView = CGRectMake(0.0, 0.0, 480.0, 320.0);
bChangeOrientation = YES;
}
[wv setFrame:cgrWebView];
}
return bChangeOrientation;
}
- (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.
}
- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[super dealloc];
}
#end

I reported a Bug on begreport.apple.com about this issue.
And today (04/21/2010) got an answer!
"Hello Paulo,
This is a follow-up to Bug ID# 7392537. Engineering believes this issue has been addressed in iPhone OS 4.0 beta seed 2. (8A248c)
After installing the iPhone OS 4.0b2 software, please update this bug report with your results.
Thank you for your time. We truly appreciate your assistance in helping us discover and isolate bugs.
Best Regards,
Patrick Collins
Apple Developer Connection
Worldwide Developer Relations"

I was facing the same problem but I set the frame of web view in
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
{
BOOL orientation = NO;
if(interfaceOrientation == UIInterfaceOrientationPortrait)
{
[webView setFrame:CGRectMake(0, 0, 320, 480)];
orientation = YES;
return orientation;
}else if(interfaceOrientation == UIInterfaceOrientationLandscapeRight)
{
[webView setFrame:CGRectMake(0, 0, 480, 320)];
orientation = YES;
return orientation;
}else {
orientation = NO;
return orientation;
}
}

Related

UIImagePickerControllerCameraDeviceFront works every other time

This question is very similar to an existing question asked here UIImagePickerControllerCameraDeviceFront only works every other time I tried the solution presented but it didn't work for me
I have a simplest of a project with two view controllers. In the blue one I am displaying a small UIView with a UIImagePickerController in it. NOTE: I am displaying front facing camera when app is launched.
I hit the next button and go to orange view controller and when I hit the back button and come back to blue view controller the UIImagePickerController flips from Front to rear. I guess the reason is that it thinks its busy and moves to the rear cam. If I keep moving back and forth between the view controllers the camera keeps flipping front, back, front, back, front, back...
Here is my code and screenshots, what am I doing wrong?
In my *.h
#import <UIKit/UIKit.h>
#interface v1ViewController : UIViewController <UIImagePickerControllerDelegate>
{
UIImagePickerController *picpicker;
UIView *controllerView;
}
#property (nonatomic, retain) UIImagePickerController *picpicker;
#property (nonatomic, retain) UIView *controllerView;
#end
In my *.m file (This code is only used when blue colored view controller is displayed)
#import "v1ViewController.h"
#import <MobileCoreServices/UTCoreTypes.h>
#implementation v1ViewController
#synthesize picpicker;
#synthesize controllerView;
- (void)didReceiveMemoryWarning
{
[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, typically from a nib.
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
picpicker = [[UIImagePickerController alloc] init];
picpicker.delegate = self;
picpicker.mediaTypes = [NSArray arrayWithObjects:(NSString *)kUTTypeImage, nil];
picpicker.sourceType = UIImagePickerControllerSourceTypeCamera;
picpicker.cameraDevice = UIImagePickerControllerCameraDeviceFront;
picpicker.cameraCaptureMode = UIImagePickerControllerCameraCaptureModePhoto;
picpicker.showsCameraControls = NO;
picpicker.navigationBarHidden = NO;
picpicker.wantsFullScreenLayout = NO;
controllerView = picpicker.view;
[controllerView setFrame:CGRectMake(35, 31, 250, 250)];
controllerView.alpha = 0.0;
controllerView.transform = CGAffineTransformMakeScale(1.0, 1.0);
[self.view addSubview:controllerView];
[UIView animateWithDuration:0.3
delay:0.0
options:UIViewAnimationOptionCurveLinear
animations:^{
controllerView.alpha = 1.0;
}
completion:nil
];
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
[picpicker dismissModalViewControllerAnimated:YES];
}
- (void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
[picpicker dismissModalViewControllerAnimated:YES];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
} else {
return YES;
}
}
#end
You are dismissing the controller in both the viewDidDisappear and viewWillDisappear methods.
That could be the cause of your problem.
Although I do not have a device with a camera available right now to verify this, it seems that you're not dismissing the pickerview controller correctly. The documentation states that you should call dismissModalViewControllerAnimated: on the parent controller in order to dismiss the picker (though, calls to presented controllers will propagate to presenters - so this is not the problem), but in your case you're not displaying the controller modally in the first place so it will not work.
What I would try in this case is to release the picker instead (if not under ARC) and set it to nil (instead of calling [picpicker dismissModalViewControllerAnimated:YES];).
PS. In fact, it seems that there is a bigger problem with your design. Since each button is set to present the other party modally you are not dismissing any of the controllers ever. The controllers just keep stacking on each other. You should either consider to embed them in a navigation controller and have it handle the hierarchy or just set dismissModalViewControllerAnimated: (dismissViewControllerAnimated:completion: on iOS5+) as the action of the second controller's button instead of a modal segue.
This is a very simple issue. I don't know why this happens exactly, but it seems that UIImagePickerController was designed to recreated each time it's needed instead of keeping any reference to it, which seems logical if you think about it. Basically, you need to recreate and reconfigure your picker each time. Below I've pasted some code to give an image of what I mean.
Simple solution:
- (UIImagePickerController *)loadImagePicker {
UIImagePickerController *picpicker = [[UIImagePickerController alloc] init];
picpicker.delegate = self;
picpicker.mediaTypes = [NSArray arrayWithObjects:(NSString *)kUTTypeImage, nil];
picpicker.sourceType = UIImagePickerControllerSourceTypeCamera;
picpicker.cameraDevice = UIImagePickerControllerCameraDeviceFront;
picpicker.cameraCaptureMode = UIImagePickerControllerCameraCaptureModePhoto;
picpicker.showsCameraControls = NO;
picpicker.navigationBarHidden = NO;
picpicker.wantsFullScreenLayout = NO;
return picpicker;
}
and in:
-(void)viewWillAppear:(BOOL)animated{
if(!self.picpicker){
self.picpicker = [self loadImagePicker];
[self.view addSubview: self.picpicker];
}
}
-(void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
[self.picpicker removeFromSuperview];
self.picpicker = 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.

Manually rotate viewcontroller

My app must NOT auto-rotate at all. But it includes a screen which tells the user to rotate his phone (and not the opposite!).
To do that, the ViewController must make an animated rotation (without any rotation event) when the screen is displaying.
So I used
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeLeft animated:animated];
}
- (void)viewWillDisappear:(BOOL)animated {
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait animated:animated];
[super viewWillDisappear:animated];
}
And
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
return YES;
}
to make my screen rotate, as every website and documentation recommend.
But only the StatusBar rotates: my NavigationBar remains stuck at the top.
I would use a CGAffineTransform perhaps on the navigationcontroller view? Simply rotate it using an animation block 90 degrees?
this code is helpful for you to resize the navigation bar automatically you can use it in where you create the navigationController & navigation bar
self.navigationController.navigationBar.autoresizesSubviews = YES;
self.navigationController.navigationBar.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
above code will work automatically if it is not then you try this will work in all delegates methods of your view controller where you need the change
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeLeft animated:animated];
[self.navigationController shouldAutorotateToInterfaceOrientation:[UIApplication sharedApplication].statusBarOrientation];
}
- (void) didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
CGRect frame = self.navViewController.navigationBar.frame;
frame.size = self.view.frame.size;
if (toInterfaceOrientation == UIInterfaceOrientationPortrait || toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) {
frame.size.height = 44;
} else {
frame.size.height = 32;
}
self.navViewController.navigationBar.frame = frame;
if navigation controller is rootview controller then check it enables the all orientation supports
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
[super shouldAutorotateToInterfaceOrientation:toInterfaceOrientation];
[self.navigationController shouldAutorotateToInterfaceOrientation:toInterfaceOrientation];
return YES;
}
you can use this code in viewcontroller delegates listed below according to your requirment
- (void)viewDidAppear:(BOOL)animated
- (void)viewWillAppear:(BOOL)animated
– willRotateToInterfaceOrientation:duration:
– willAnimateRotationToInterfaceOrientation:duration:
– didRotateFromInterfaceOrientation:

iOS switching view controllers depending on the device orientation

I'm developing an Augmented Reality application, everything worked properly till now that I need two different kind of visualization (AR and Map) depending on the device orientation. In particular the application should use the landscapeViewController when the device is in landscape mode while it should use another controller (named faceUpViewController ) when the device's orientation is "face up". I tried doing it with two simple view controllers and it works fine. The problem happens when the landscapeViewController uses the AR controller. The view is completely white and I don't understand why. Both the two controllers are "contained" by a Root View Controller. I'm doing everything by coding so without nib files. Here is the code:
RootViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(deviceOrientationDidChange:) name:UIDeviceOrientationDidChangeNotification object:nil];
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
}
- (void)deviceOrientationDidChange:(NSNotification *)notification{
UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
if (orientation == UIDeviceOrientationLandscapeLeft) {
if (self.landscapeViewController.view.superview == nil) {
if (self.landscapeViewController == nil) {
LandscapeViewController *lvc = [[LandscapeViewController alloc] init];
self.landscapeViewController = lvc;
[lvc release];
}
[self.faceUpViewController.view removeFromSuperview];
[self.view addSubview:self.landscapeViewController.view];
}
}
if (orientation == UIDeviceOrientationFaceUp) {
if (self.faceUpViewController.view.superview == nil) {
if (self.faceUpViewController == nil) {
FaceUpViewController *fvc = [[FaceUpViewController alloc] init];
self.faceUpViewController = fvc;
[fvc release];
}
[self.landscapeViewController.view removeFromSuperview];
[self.view addSubview:self.faceUpViewController.view];
}
}
}
#end
LandscapeViewController.m
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView
{
UIView *landscapeView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1024, 768)];
landscapeView.backgroundColor = [UIColor yellowColor];
self.view = landscapeView;
[landscapeView release];
ARController *arC = [[ARController alloc] initWithViewController:self];
arC.landscapeViewController = self;
self.arController = arC;
[arC release];
}
//When the view appear present the camera feed
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[_arController presentModalARControllerAnimated:NO];
}
FaceUpViewController.m
- (void)loadView
{
UIView *faceUpView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1024, 768)];
faceUpView.backgroundColor = [UIColor blueColor];
self.view = faceUpView;
[faceUpView release];
}
ARController.m Very simple version
- (id) initWithViewController:(UIViewController *)theView{
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
self.rootController = theView;
//Retrieve screen bounds
CGRect screenBounds = [[UIScreen mainScreen] bounds];
UIView *overlaidView = [[UIView alloc] initWithFrame: screenBounds];
self.overlayView = overlaidView;
[overlaidView release];
self.rootController.view = overlayView;
// Initialise the UIImagePickerController
UIImagePickerController *picker= [[UIImagePickerController alloc] init];
self.pickerController = picker;
[picker release];
self.pickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
self.pickerController.cameraViewTransform = CGAffineTransformScale(
self.pickerController.cameraViewTransform, 1.0f, 1.12412f);
self.pickerController.showsCameraControls = NO;
self.pickerController.navigationBarHidden = YES;
self.pickerController.cameraOverlayView = _overlayView;
}
return self;
}
- (void)presentModalARControllerAnimated:(BOOL)animated{
[self.rootController presentModalViewController:[self pickerController] animated:animated];
self.overlayView.frame = self.pickerController.view.bounds;
}
#end
I say again that I'm doing everything by coding thereby without nib files.
I really appreciate any advice!
Thanks
The primary problem with adding and removing your "child" view controllers' views as you've done here is that the view controller life cycle methods (viewWillAppear:, viewDidAppear:, etc.) won't ever get called on your child view controllers. Containers like UINavigationController and UITabBarController have always known how to delegate methods like these appropriately to their children, but UIViewController didn't officially support the ability to nest view controllers under your own custom container before iOS 5. It was possible, but it took a lot more work to do it right.
If you want to stick with the approach of adding and removing subviews, you have two options:
Require iOS 5+, and call addChildViewController:, removeFromParentViewController,
transitionFromViewController:toViewController:duration:options:animations:completion:,
willMoveToParentViewController:, and
didMoveToParentViewController: as described in the Implementing a Container View Controller section of the UIViewController Class Reference.
To support older iOS versions, you'll have to override many of the methods of the UIViewController class and delegate those calls manually to your child view controllers to make them behave as expected. I'd pay particular attention to the sections titled, "Responding to View Events", and "Responding to View Rotation Events" in the UIViewController Class Reference.
A different approach for pre-iOS 5 support is to present your child view controllers using presentModalViewController:animated: rather than adding their views as subviews to a container. Apple describes this approach in the View Controller Programming Guide for iOS under the section, Creating an Alternate Landscape Interface. The advantage of this approach is that your child view controllers are officially supported as first-class members of the view controller hierarchy, so UIKit will automatically manage their life cycles appropriately. You won't have to override and delegate all those methods manually.
You might want to try getting your acceptance rate up a little bit - more people would be willing to help you.
Anyway, wild guess: in your root controller, try putting the contents of
deviceOrientationDidChange
into
deviceOrientationWillChange.

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)];