Implementations on AdMob (iOS) - iphone

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

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;
}

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.

App crashes at [UIWebView webView:didReceiveTitle:forFrame:]

I am implementing a simple in-app browser. In my home view (UITableViewController), I have something like:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
WebViewController *webViewController = [[WebViewController alloc] init];
switch (indexPath.row) {
case 0:
webViewController.stringURL = #"http://www.google.com";
break;
case 1:
webViewController.stringURL = #"http://www.bing.com";
break;
default:
webViewController.stringURL = #"http://stackoverflow.com";
break;
}
[self.navigationController pushViewController:webViewController animated:YES];
[webViewController release];
}
The app crashed after I repetitively navigated back and forth between my home view and webViewControllera few times.
Inside WebViewController class, I have nothing but a [UIWebView *webView] and a [UIActivityIndicatorView *activityIndicator]. Both are with attributes nonatomic, retain. Here is the implementation.
#import "WebViewController.h"
#implementation WebViewController
#synthesize webView, activityIndicator, stringURL;
- (void)dealloc
{
[self.webView release];
self.webView.delegate = nil;
[self.activityIndicator release];
[super dealloc];
}
-(void)loadView {
UIView *contentView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
self.view = contentView;
CGRect webFrame = [[UIScreen mainScreen] applicationFrame];
webFrame.origin.y = 0.0f;
self.webView = [[UIWebView alloc] initWithFrame:webFrame];
self.webView.backgroundColor = [UIColor blueColor];
self.webView.scalesPageToFit = YES;
self.webView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
self.webView.delegate = self;
[self.view addSubview: self.webView];
[self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:self.stringURL]]];
self.activityIndicator = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
self.activityIndicator.frame = CGRectMake(0.0, 0.0, 30.0, 30.0);
self.activityIndicator.center = self.view.center;
[self.view addSubview: self.activityIndicator];
}
- (void)viewDidLoad
{
[super viewDidLoad];
[self loadView];
}
- (void)webViewDidStartLoad:(UIWebView *)webView
{
// starting the load, show the activity indicator in the status bar
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
[activityIndicator startAnimating];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
// finished loading, hide the activity indicator in the status bar
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
[activityIndicator stopAnimating];
}
#end
I just ran my app in Instruments using the Zombies template, which shows -[UIWebView webView:didReceiveTitle:forFrame:] is the Zombie call. But I still can’t figure out what is actually the problem.
(Please download trace if needed)
Any help is greatly appreciated!
[Update]:
As #7KV7 and #David pointed out, there is an obvious bug in my dealloc function. I should call self.webView.delegate=nil; first before I release self.webView. Sorry about that. Unfortunately, after I fix it, the app still crashes in the same way.
If I delete [webViewController release]; from the first code block, the crash actually is gone. But obviously, there will be memory leak.
First of all, remove that call to loadView in viewDidLoad. The framework will the call the method when it doesn't find a view provided in XIB file. Second, your loadView is filled with memory leaks. You are allocating, initializing and retaining an object every time the method is called. So you are taking ownership twice and releasing it only once in the dealloc.
The objects are not being properly deallocated. You should do something like alloc-init-autorelease to solve this. Next thing is the that every time the controller gets loaded, because of your call to loadView, you end up creating two web view objects and two requests. You lose reference to one of them as you reassign. Herein, lies the problem mentioned in the title. You aren't able to reset the delegate of a web view object that has your controller as a delegate. Imagine a request being completed soon after you leave. Here the message will go to a zombie object. This is a pretty good example for why you need to nil out your delegates.
- (void)dealloc
{
self.webView.delegate = nil;
[self.webView release];
[self.activityIndicator release];
[super dealloc];
}
Try this dealloc. You were releasing the webview and then setting the delegate as nil. You should first set the delegate as nil and then release it. Hope this solves the issue.
I think what's happening is that you are going back while the page is still loading so the controller gets deallocated and then the webview finishes loading.
Try calling [webView stopLoading] in your viewDidUnload method to make sure this isn't happening.
Don't know if it's the cause of your problem, but this is definitely wrong:
[self.webView release];
self.webView.delegate = nil;
You cannot (safely) refer to self.webView after you release it!
Instead of pushing webViewController,add its view to self.view .
Dont call [self loadView]; in viewDidLoad.

How do I position AdMob ad above the Tabbar?

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!

Problem with the scrollbar of a UIWebView

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;
}
}