iAd issue:--> Every time empty iAd banners is coming instead of AdMobView - objective-c

#pragma mark - iAd method.
/*Starts:iAd*/
-(void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
Class cls = NSClassFromString(#"ADBannerView");
if(cls!=nil)
{
[self layoutForCurrentOrientation:YES];
}
}
-(void)createADBannerView
{
NSString *contentSize = UIInterfaceOrientationIsPortrait(self.interfaceOrientation) ? ADBannerContentSizeIdentifierPortrait : ADBannerContentSizeIdentifierPortrait;
CGRect frame;
frame.size = [ADBannerView sizeFromBannerContentSizeIdentifier:contentSize];
frame.origin = CGPointMake(0.0, CGRectGetMaxY(self.view.bounds));
// Now to create and configure the banner view
ADBannerView *bannerView = [[ADBannerView alloc] initWithFrame:frame];
// Set the delegate to self, so that we are notified of ad responses.
bannerView.delegate = self;
// Set the autoresizing mask so that the banner is pinned to the bottom
bannerView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleTopMargin;
bannerView.requiredContentSizeIdentifiers = [NSSet setWithObjects:ADBannerContentSizeIdentifierPortrait, ADBannerContentSizeIdentifierPortrait, nil];
// At this point the ad banner is now be visible and looking for an ad.
[self.view addSubview:bannerView];
self.banner = bannerView;
[bannerView release];
}
-(void)layoutForCurrentOrientation:(BOOL)animated
{
CGFloat animationDuration = animated ? 0.2 : 0.0;
// by default content consumes the entire view area
CGRect contentFrame = self.view.bounds;
CGPoint bannerOrigin = CGPointMake(CGRectGetMinX(contentFrame), CGRectGetMaxY(contentFrame));
CGFloat bannerHeight = 0.0;
banner.currentContentSizeIdentifier = ADBannerContentSizeIdentifierPortrait;
bannerHeight = 55.0;
if(banner.bannerLoaded)
{
contentFrame.size.height -= bannerHeight;
bannerOrigin.y -= bannerHeight;
}
else
{
bannerOrigin.y += bannerHeight;
}
// And finally animate the changes, running layout for the content view if required.
NSLog(#"[UIApplication sharedApplication].statusBarOrientation = %d",[UIApplication sharedApplication].statusBarOrientation);
[UIView animateWithDuration:animationDuration
animations:^{
contentView.frame = contentFrame;
[contentView layoutIfNeeded];
banner.frame = CGRectMake(0,361, banner.frame.size.width, banner.frame.size.height);
}];
}
/*Ends:iAd*/
/*Starts:Added By:CP.Date:26-Dec-2010.
AdMob methods*/
#pragma mark - AdMobDelegate methods
-(NSString *) publisherIdForAd:(AdMobView *)adView
{
return #"a14f91029b8c719"; // this should be prefilled; if not, get it from www.admob.com
}
-(UIViewController *) currentViewControllerForAd:(AdMobView *)adView
{
return self;
}
-(UIColor *)adBackgroundColorForAd:(AdMobView *)adView
{
return [UIColor colorWithRed:0 green:0 blue:0 alpha:1]; // this should be prefilled; if not, provide a UIColor
}
-(UIColor *)primaryTextColorForAd:(AdMobView *)adView
{
return [UIColor colorWithRed:1 green:1 blue:1 alpha:1]; // this should be prefilled; if not, provide a UIColor
}
-(UIColor *)secondaryTextColorForAd:(AdMobView *)adView
{
return [UIColor colorWithRed:1 green:1 blue:1 alpha:1]; // this should be prefilled; if not, provide a UIColor
}
-(void) didReceiveAd:(AdMobView *)adView
{
adMobAd.frame = CGRectMake(0,361,320,55);
//adMobAd = [AdMobView requestAdOfSize:ADMOB_SIZE_748x110 withDelegate:self];
[self.view addSubview:adMobAd];
}
// Sent when an ad request failed to load an ad
-(void)didFailToReceiveAd:(AdMobView *)adView
{
Class cls = NSClassFromString(#"ADBannerView");
if(cls!=nil)
{
if(banner == nil)
{
[self createADBannerView];
}
[self layoutForCurrentOrientation:NO];
}
[adMobAd removeFromSuperview]; // Not necessary since never added to a view, but doesn't hurt and is good practice
[adMobAd release];
adMobAd = nil;
// we could start a new ad request here, but in the interests of the user's battery life, let's not
}
/*Ends:Added By:CP.Date:26-Dec-2010.
AdMob methods*/

first of all be clear about iad or admob , that are two different plateform for displaing adv in your app.
i think you are try to integrate admob adv. you need to following steps for it.
http://www.edumobile.org/iphone/iphone-programming-tutorials/how-to-admob-integrate-in-your-application-in-iphone/

Related

Adding non editable NSTextField as a label into another NSTextField

I am trying to implement a custom NSTextField that a) changes color when active and b) has a label in the top left hand corner.
I have the following implementation:
In TestTextField.h
#interface TestTextField : NSTextField
- (id)initFullWidthWithLabel:(NSString *)label andPreset:(NSString *)preset;
...
#end
In TestTextField.m
#interface TestTextField() {
BOOL _thisFieldIsActive;
}
#property (nonatomic, strong) NSTextField *label;
#end
#implementation TestTextField
- (id)initFullWidthWithLabel:(NSString *)label andPreset:(NSString *)preset {
self = [super initWithFrame:NSZeroRect];
if (self) {
_thisFieldIsActive = NO;
[self setFocusRingType:NSFocusRingTypeNone];
// small label top left
_label = [[NSTextField alloc] initWithFrame:NSZeroRect];
_label.stringValue = label;
if (preset) {
self.stringValue = preset;
}
else {
self.stringValue = #"0";
}
[self layoutUI];
}
return self;
}
- (void)turnActiveOff {
[self toggleActive:NO];
}
- (void)toggleActive:(BOOL)active {
_thisFieldIsActive = active;
if (_thisFieldIsActive) {
self.backgroundColor = [NSColor blueColor];
self.textColor = [NSColor whiteColor];
_label.textColor = [NSColor whiteColor];
}
else {
self.backgroundColor = [NSColor clearColor];
self.textColor = [NSColor blackColor];
_label.textColor = [NSColor grayColor];
}
}
- (BOOL)becomeFirstResponder {
NSLog(#"BecomeFirstResponder");
[self selectText:self];
[self toggleActive:YES];
return [super becomeFirstResponder];
}
- (void)textDidEndEditing:(NSNotification *)notification {
NSLog(#"DidEndEditing");
[self toggleActive:NO];
[super textDidEndEditing:notification];
}
- (void)layoutUI {
self.alignment = NSRightTextAlignment;
self.font = [NSFont fontWithName:#"HelveticaNeue-Light" size:32.0f];
self.layer.borderColor = [NSColor whiteColor].CGColor;
self.layer.borderWidth = 1.0f;
[self.layer setCornerRadius:4.0f];
// small label top left
_label.font = [NSFont fontWithName:#"HelveticaNeue-Light" size:12.0f];
_label.alignment = NSLeftTextAlignment;
_label.textColor = [NSColor grayColor];
_label.stringValue = [_label.stringValue uppercaseString];
_label.selectable = NO;
_label.editable = NO;
_label.drawsBackground = NO;
_label.bezeled = NO;
[self addSubview:_label];
NSDictionary *metrics = #{#"borderPadding": #5};
_label.translatesAutoresizingMaskIntoConstraints = NO;
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"H:|-(borderPadding)-[_label(100)]" options:0 metrics:metrics views:#{ #"_label" : _label }]];
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"V:|-(borderPadding)-[_label(30)]" options:0 metrics:metrics views:#{ #"_label" : _label }]];
}
In my ViewController I implement these TestTextFields by simply calling the custom initFullWidthWithLabel:andPreset: method and adding them as a subview to the VCs view
I can see that the label gets positioned correctly, however as soon as the the field becomes active and the backgroundColor is set, it seems to cover up the label. How can I make sure the label stays on top?
Even when the backgorund coloring is turned off, the label remains hidden.
Thanks
The core of the solution was to use a custom subclassed NSTextFieldCell with one method:
- (NSRect)drawingRectForBounds:(NSRect)rect {
NSRect rectInset = NSMakeRect(rect.origin.x + 100.0f, rect.origin.y, rect.size.width - 100.0f, rect.size.height);
return [super drawingRectForBounds:rectInset];
}

iOS 7 iAd cocos2d deprecated

Im trying to get iAd implemented to a iOS 7 game I made with cocos2d, I added iad framework in xcode and have the setup below
in #implementation I have set
ADBannerView *_bannerView;
then
-(id)init
{
if( (self= [super init]) )
{
// On iOS 6 ADBannerView introduces a new initializer, use it when available.
if ([ADBannerView instancesRespondToSelector:#selector(initWithAdType:)]) {
ADBannerView *_adView = [[ADBannerView alloc] initWithAdType:ADAdTypeBanner];
} else {
_adView = [[ADBannerView alloc] init];
}
_adView.requiredContentSizeIdentifiers = [NSSet setWithObject:ADBannerContentSizeIdentifierPortrait];
_adView.currentContentSizeIdentifier = ADBannerContentSizeIdentifierPortrait;
[[[CCDirector sharedDirector]view]addSubview:_adView];
[_adView setBackgroundColor:[UIColor clearColor]];
[[[CCDirector sharedDirector]view]addSubview:_adView];
_adView.delegate = self;
}
[self layoutAnimated:YES];
return self;
}
- (void)layoutAnimated:(BOOL)animated
{
// As of iOS 6.0, the banner will automatically resize itself based on its width.
// To support iOS 5.0 however, we continue to set the currentContentSizeIdentifier appropriately.
CGRect contentFrame = [CCDirector sharedDirector].view.bounds;
if (contentFrame.size.width < contentFrame.size.height) {
//_bannerView.currentContentSizeIdentifier = ADBannerContentSizeIdentifierPortrait;
[adView setAutoresizingMask:UIViewAutoresizingFlexibleWidth];
} else {
_bannerView.currentContentSizeIdentifier = ADBannerContentSizeIdentifierLandscape;
}
CGRect bannerFrame = _bannerView.frame;
if (_bannerView.bannerLoaded) {
contentFrame.size.height -= _bannerView.frame.size.height;
bannerFrame.origin.y = contentFrame.size.height;
} else {
bannerFrame.origin.y = contentFrame.size.height;
}
[UIView animateWithDuration:animated ? 0.25 : 0.0 animations:^{
_bannerView.frame = bannerFrame;
}];
}
- (void)bannerViewDidLoadAd:(ADBannerView *)banner {
[self layoutAnimated:YES];
}
- (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error {
[self layoutAnimated:YES];
}
I get an error about '_adview' and as well as 'currentContentSizeIdentifier' deprecated, can anyone help me get this working correctly? Ive checked online all day and couldnt get any of the code working.
Thanks in advance!
Try this New Code:
- (void)createAdBannerView
{
_adBannerView = [[ADBannerView alloc] initWithFrame:CGRectZero];
_adBannerView.delegate = self;
[_adBannerView setAutoresizingMask:UIViewAutoresizingFlexibleWidth];
CGRect frame = _adBannerView.frame;
frame.origin.y = -frame.size.height;
frame.origin.x = 0.0f;
_adBannerView.frame = frame;
AppDelegate * app = (((AppDelegate*) [UIApplication sharedApplication].delegate));
[app.navController.view addSubview:_adBannerView];
}
Here new iAd Code for Cocos2d 3.0.

Admob banner not getting remove from superview

I am developing one 2d game using cocos2d framework, in this game i am using admob for advertising, in some classes not in all classes but admob banner is visible in every class and after some time game getting crash also.
I am not getting how admob banner is comes in every class in fact i have not declare in Rootviewcontroller class. can any one suggest me how to integrate Admob in cocos2d game, i want Admob banner in particular classes not in every class,
I am using latest google admob sdk, my code is below:
Thanks in advance
`
-(void)AdMob{
NSLog(#"ADMOB");
CGSize winSize = [[CCDirector sharedDirector]winSize];
// Create a view of the standard size at the bottom of the screen.
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad){
bannerView_ = [[GADBannerView alloc]
initWithFrame:CGRectMake(size.width/2-364,
size.height -
GAD_SIZE_728x90.height,
GAD_SIZE_728x90.width,
GAD_SIZE_728x90.height)];
}
else { // It's an iPhone
bannerView_ = [[GADBannerView alloc]
initWithFrame:CGRectMake(size.width/2-160,
size.height -
GAD_SIZE_320x50.height,
GAD_SIZE_320x50.width,
GAD_SIZE_320x50.height)];
}
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
bannerView_.adUnitID =#"a15062384653c9e";
}
else {
bannerView_.adUnitID =#"a15062392a0aa0a";
}
bannerView_.rootViewController = self;
[[[CCDirector sharedDirector]openGLView]addSubview:bannerView_];
[bannerView_ loadRequest:[GADRequest request]];
GADRequest *request = [[GADRequest alloc] init];
request.testing = [NSArray arrayWithObjects:
GAD_SIMULATOR_ID, nil]; // Simulator
[bannerView_ loadRequest:request];
}
//best practice for removing the barnnerView_
-(void)removeSubviews{
NSArray* subviews = [[CCDirector sharedDirector]openGLView].subviews;
for (id SUB in subviews){
[(UIView*)SUB removeFromSuperview];
[SUB release];
}
NSLog(#"remove from view");
}
//this makes the refreshTimer count
-(void)targetMethod:(NSTimer *)theTimer{
//INCREASE OF THE TIMER AND SECONDS
elapsedTime++;
seconds++;
//INCREASE OF THE MINUTOS EACH 60 SECONDS
if (seconds>=60) {
seconds=0; minutes++;
[self removeSubviews];
[self AdMob];
}
NSLog(#"TIME: %02d:%02d", minutes, seconds);
}
`
UPDATES: Refer generalised answer here: Admob-banner-integration-in-cocos2d
I hope already u got solution. If not then here is all code for Admob integration in cocos2D game.
#define ENABLE_ADMOB 1
//#define COCOS2D_2_0 1
#interface MyMainMenu : CCLayer
{
#ifdef ENABLE_ADMOB
GADBannerView *mBannerView;
#endif
}
#implementation MyMainMenu
-(void)onEnter
{
[super onEnter];
#ifdef ENABLE_ADMOB
#ifdef COCOS2D_2_0
AppController *app = (AppController*)[[UIApplication sharedApplication] delegate];
#else
AppDelegate* app = (AppDelegate*)[[UIApplication sharedApplication] delegate];
#endif
mBannerView = [[GADBannerView alloc] initWithAdSize:kGADAdSizeBanner];
// Specify the ad's "unit identifier." This is your AdMob Publisher ID.
mBannerView.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.
//size
#ifdef COCOS2D_2_0
mBannerView.rootViewController = app.navController;
[app.navController.view addSubview:mBannerView];
#else
mBannerView.rootViewController = app.viewController;
[app.viewController.view addSubview:mBannerView];
#endif
// Initiate a generic request to load it with an ad.
[mBannerView loadRequest:[GADRequest request]];
CGSize AdSize = kGADAdSizeBanner.size;
CGRect frame = mBannerView.frame;
frame.origin.y = -50.0f;
#ifdef COCOS2D_2_0
frame.origin.x = (app.navController.view.bounds.size.width - AdSize.width) / 2 ;
#else
frame.origin.x = (app.viewController.view.bounds.size.width - AdSize.width) / 2 ;
#endif
mBannerView.frame = frame;
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
frame = mBannerView.frame;
frame.origin.y = 0.0f;
mBannerView.frame = frame;
[UIView commitAnimations];
#endif
}
-(void)showBannerView
{
if (mBannerView)
{
[UIView animateWithDuration:0.5
delay:0.1
options: UIViewAnimationCurveEaseOut
animations:^
{
CGRect frame = mBannerView.frame;
frame.origin.y = 0.0f;
mBannerView.frame = frame;
}
completion:^(BOOL finished)
{
}];
}
}
-(void)hideBannerView
{
if (mBannerView)
{
[UIView animateWithDuration:0.5
delay:0.1
options: UIViewAnimationCurveEaseOut
animations:^
{
CGRect frame = mBannerView.frame;
frame.origin.y = -50.0f;
mBannerView.frame = frame;
}
completion:^(BOOL finished)
{
}];
}
}
-(void)dismissAdView
{
#ifdef ENABLE_ADMOB
if (mBannerView)
{
[UIView animateWithDuration:0.5
delay:0.1
options: UIViewAnimationCurveEaseOut
animations:^
{
CGRect frame = mBannerView.frame;
frame.origin.y = -50.0f;
mBannerView.frame = frame;
}
completion:^(BOOL finished)
{
[mBannerView setDelegate:nil];
[mBannerView removeFromSuperview];
mBannerView = nil;
}];
}
#endif
}
Full fix (tested with iOS 8.1 and Admob 6.12.0)
-(void)RemoveAds
{
if (adBanner != nil)
{
[adBanner setRootViewController:nil];
[adBanner removeFromSuperview];
adBanner = nil;
}
}
Since in cocos2d, you will have different classes for scenes.
My suggestion would be to create a separate class for the add banner and have a static method do the job for you. You will have to save a reference of the add banner in that class and by using the static methods you can add/remove it to/from the openglview.
For removing you will only do: [bannerView removeFromSuperview];

How to display imagepickercontroller through popoverview controller?

I have used the following code to get photos from album in iphone but it displays the error "UIImagePickerController must be presented via UIPopoverController"
- (void)showImagePicker:(UIImagePickerControllerSourceType)sourceType
{
if ([UIImagePickerController isSourceTypeAvailable:sourceType])
{
[self setupImagePicker:sourceType];
[self presentModalViewController:imagePickerController animated:YES];
}
}
- (void)setupImagePicker:(UIImagePickerControllerSourceType)sourceType
{
imagePickerController.sourceType = sourceType;
if (sourceType == UIImagePickerControllerSourceTypeCamera)
{
// user wants to use the camera interface
//
imagePickerController.showsCameraControls = NO;
if (imagePickerController.cameraOverlayView != self.view)
{
// setup our custom overlay view for the camera
//
// ensure that our custom view's frame fits within the parent frame
CGRect overlayViewFrame = imagePickerController.cameraOverlayView.frame;
CGRect newFrame = CGRectMake(0.0,
CGRectGetHeight(overlayViewFrame) -
self.view.frame.size.height - 9.0,
CGRectGetWidth(overlayViewFrame),
self.view.frame.size.height + 9.0);
self.view.frame = newFrame;
imagePickerController.cameraOverlayView = self.view;
}
}
}
-(IBAction)getPhoto:(id)sender {
imagePickerController = [[UIImagePickerController alloc] init];
[self showImagePicker:UIImagePickerControllerSourceTypePhotoLibrary];
}
Can any one please alter my code to work on iPad.
Thanks in advance.
-(IBAction)Click_event
{
UIImagePickerController *imagePickerController_=[[UIImagePickerController alloc] init];
UIPopoverController *popover_=[[UIPopoverController alloc] initWithContentViewController:imagePickerController_];
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
imagePickerController_.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[popover_ presentPopoverFromRect:CGRectMake(400, 400, 0, 0) inView:self.Mybutton
permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}
}

Why is my iAd banner not showing?

- (void)viewDidLoad
{
adView = [[ADBannerView alloc] initWithFrame:CGRectZero];
adView.requiredContentSizeIdentifiers = [NSSet setWithObject:ADBannerContentSizeIdentifierPortrait];
adView.currentContentSizeIdentifier = ADBannerContentSizeIdentifierPortrait;
CGRect adFrame = adView.frame;
adFrame.origin.y = self.view.frame.size.height-adView.frame.size.height;
adView.frame = adFrame;
[self.view addSubview:adView];
}
- (void)bannerViewDidLoadAd:(ADBannerView *)banner
{
if (!self.bannerIsVisible)
{
self.bannerIsVisible = YES;
}
}
- (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error
{
if (self.bannerIsVisible)
{
self.bannerIsVisible = NO;
}
There are a four things. First, you should be positioning the banner off screen in your viewDidLoad method because it will just show an empty frame when you first launch and will more than likely get rejected because of it.
Secondly, you are setting up your banner view incorrectly. I think the frame is still CGZero. Thirdly you are not setting the bannerView's delegate. Try the following:
-(void)viewDidLoad{
CGRect frame=CGRectZero;
frame.size = [ADBannerView sizeFromBannerContentSizeIdentifier:ADBannerContentSizeIdentifierPortrait];
// Place frame at the bottom edge of the screen out of sight
frame.origin = CGPointMake(0.0, CGRectGetMaxY(self.view.bounds));
// Now to create and configure the banner view
ADBannerView *adView = [[ADBannerView alloc] initWithFrame:frame];
adView.requiredContentSizeIdentifiers = [NSSet setWithObject:ADBannerContentSizeIdentifierPortrait];
// Set the delegate to self, so that we are notified of ad responses
adView.delegate = self;
[self.view addSubview: adView];
}
Fourth, in your bannerViewDidLoadAd: method you are not animating the banner ad into place. Try this:
- (void)bannerViewDidLoadAd:(ADBannerView *)banner
{
// Get a brand new frame
CGRect newFrame=CGRectZero;
CGPoint frameOrigin=CGPointZero;
// Set the origin
frameOrigin=CGPointMake(0.0, CGRectGetMaxY(self.view.bounds));
newFrame.origin=frameOrigin;
// Set the size
newFrame.size=[ADBannerView sizeFromBannerContentSizeIdentifier:ADBannerContentSizeIdentifierPortrait];
CGFloat bannerHeight = newFrame.size.height;
CGFloat bannerOffset=0.0;
// Determine where the new frame should be
if (!self.bannerIsVisible)
{
// It should be visible, raise it up
bannerOffset=-bannerHeight;
}
CGRect offSetRect=CGRectOffset(newFrame,0.0f,bannerOffset);
[UIView animateWithDuration:0.2
animations:^{banner.view.frame=offSetRect}
completion:^(BOOL finished){
if (bannerOffSet<0){
self.bannerIsVisible=YES;
}else{
self.bannerIsVisible=NO;
}
}
];
}
of course if the banner is supposed to be positioned at the top of the screen, you can probably figure out how things need to be modified, but this gets you in going in the right direction.
Good luck
Not sure by reading your question, but do be aware that Apple simulates iAds not being available. Sometimes you need to try multiple times before the sample ad comes through.