In my project , i have implemented Google-Mobile-Ads SDK. I have followed all the steps with are written in the google site. I am able to access the google ad in all my view controllers as i have coded it in my AppDelegate.
Now , i want it to hide it from the first view controller (like we can hide navigationbar), how to implement this ?
In have coded this for google ads in AppDelegate`s DidFinishLaunchingMethod:-
bannerView=[[GADBannerView alloc] initWithFrame:CGRectMake(0, self.window.frame.size.height-50, self.window.frame.size.width, 50)];
[self.window addSubview:bannerView];
bannerView.adUnitID = #"ca-app-pub-8809802355107737/4999307809";
bannerView.rootViewController = self.window.rootViewController;
bannerView.delegate=self;
GADRequest *request = [GADRequest request];
request.testDevices = #[ #"eba07768136b615eee7c1f8acde25c1b",kGADSimulatorID ];
[bannerView loadRequest:request];
[self.window makeKeyAndVisible];
return YES;
I have this method also in appDelegate class :-
- (void)adViewDidReceiveAd:(GADBannerView *)view;
{
if (container.view.frame.size.height==self.window.frame.size.height-50) {
}
else
{
CGRect navFrame = container.view.frame;
navFrame.size.height -= 50;
container.view.frame = navFrame;
}
NSLog(#"asdhaskda");
}
in firstViewController:
-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
[[NSNotificationCenter defaultCenter] postNotificationName:#"HIDEBANNER" object:nil];
}
-(void)viewDidDisappear:(BOOL)animated{
[super viewDidDisappear:animated];
[[NSNotificationCenter defaultCenter] postNotificationName:#"SHOWBANNER" object:nil];
}
In AppDelegate.m file, in didFinishLaunchingWithOptions:
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(hideBanner) name:#"HIDEBANNER" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(showBanner) name:#"SHOWBANNER" object:nil];
Also implement those 2 functions. I'm not sure what exactly must be in those functions. Maybe something like this:
-(void)showBanner{
[bannerView setHidden:NO];
//this is from your code
if (container.view.frame.size.height==self.window.frame.size.height-50) {
}
else
{
CGRect navFrame = container.view.frame;
navFrame.size.height -= 50;
container.view.frame = navFrame;
}
}
-(void)hideBanner{
[bannerView setHidden:YES];
CGRect navFrame = container.view.frame;
navFrame.size.height = self.window.frame.size.height;
container.view.frame = navFrame;
}
There are many methods to do that. You can use, for example, NSNotificationCenter.
In your AppDelegate.m file in didFinishLaunchingWithOptions function:
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(hideBanner) name:#"HIDEBANNER" object:nil];
Also in AppDelegate.m add the following function:
-(void)hideBanner{
[bannerView setHidden:YES];
}
And somewhere in you project, when you need to hide banner just call this:
[[NSNotificationCenter defaultCenter] postNotificationName:#"HIDEBANNER" object:nil];
AdMob Integration Full Code.
AppDelegate.h
// Write This Code in AppDelegate.h File Start
#import GoogleMobileAds;
#pragma mark - Admob
#property (strong, nonatomic) GADBannerView *bannerView;
-(void)loadAdmob:(UIViewController *)vc;
// End
AppDelegate.m
// Write This Code in AppDelegate.m File Start
#pragma mark - Admob Methods
-(void)loadAdmob:(UIViewController *)vc{
self.bannerView = [[GADBannerView alloc] initWithFrame:CGRectMake(0, vc.view.frame.size.height-kBannerHeight, vc.view.frame.size.width, kBannerHeight)];
[vc.view addSubview:self.bannerView];
self.bannerView.hidden = YES;
//### Load Banner Ad view #####
// Replace this ad unit ID with your own ad unit ID.
self.bannerView.adUnitID = #"ca-app-pub-####"; // Botttom banner
self.bannerView.rootViewController = vc;
GADRequest *request = [GADRequest request];
// Requests test ads on devices you specify. Your test device ID is printed to the console when
// an ad request is made.
#ifdef DEBUG
request.testDevices = #[kGADSimulatorID];
//[Note: Comment this line for release build]
#endif
[self.bannerView loadRequest:request];
}
// Add this code in Viewcontroller.m file
[APP_DELEGATE loadAdmob:self];
// Add this line in PrefixHeader.pch
#define APP_DELEGATE (AppDelegate*)[[UIApplication sharedApplication] delegate]
#define kBannerHeight (IS_IPAD ? 65.0 : 49.0)
Related
I am pretty sure this question has been asked and answered, but I am not sure what I am doing wrong that I am not being able to get the method called from NSNotificationCenter. I have two classes :
Sign In Class And Calendar View Class
I want to pass single user credential from SignIn to Calendar. I have put the Calendar in SWRevealController. NSNotification is being called but the trigger method is not called for some reason.
Sign In
- (IBAction)signIn:(id)sender {
_passWord = _password.text;
NSLog(#"%# %#",_userName,_passWord);
[[NSNotificationCenter defaultCenter] postNotificationName: #"Password" object: _passWord];
[self performSegueWithIdentifier:#"signIn" sender:self];
}
Calendar
- (void)viewDidLoad {
[super viewDidLoad];
UIImage *ppnImage = [UIImage imageNamed:#"PPNImage.png"];
NSLog(#"came here");
[[NSNotificationCenter defaultCenter]
addObserver:self selector:#selector(triggerAction:) name:#"Password" object:nil];
SWRevealViewController *revealViewController = self.revealViewController;
if ( revealViewController )
{
[self.sidebarButton setTarget: self.revealViewController];
[self.sidebarButton setAction: #selector( revealToggle: )];
[self.view addGestureRecognizer:self.revealViewController.panGestureRecognizer];
}
events = [[NSMutableArray alloc]init];
[_indicator startAnimating];
self.tableView.delegate = self;
//Creating a background queue
web = [WebRequests sharedInstance];
web.delegate = self;
[web loginView];
}
-(void) triggerAction: (NSNotification *) notification {
NSLog(#"Does not call this");
}
Here
[[NSNotificationCenter defaultCenter]addObserver:self selector:#selector(triggerAction:) name:#"Password" object:nil];
is called
BUT the below method is not called
-(void) triggerAction: (NSNotification *) notification
{
NSLog(#"Does not call this");
}
Really appreciate if somebody can suggest me what am I doing wrong
I'm using a 2d game engine called Sprite kit within Xcode and i want to hide my ad banner in specific areas such as the game scene and then show it once it's game over for the player. But i'm having trouble trying to access the hidden property of the banner within other scenes/classes.
GameViewController.h
#import <UIKit/UIKit.h>
#import <SpriteKit/SpriteKit.h>
#import <GoogleMobileAds/GoogleMobileAds.h>
#import <AVFoundation/AVFoundation.h>
#interface GameViewController : UIViewController
-(void) hideBanner;
#end
GameViewController.m
#implementation GameViewController
-(void) hideBanner {
self.bannerView.hidden = YES;
}
- (void)viewDidLoad {
[super viewDidLoad];
// Create a banner ad and add it to the view hierarchy.
self.bannerView = [[GADBannerView alloc] initWithAdSize:kGADAdSizeSmartBannerPortrait];
//TEST UNIT ID
self.bannerView.adUnitID = #"ca-app-pub-3940256099942544/2934735716";
self.bannerView.rootViewController = self;
[self.view addSubview:self.bannerView];
GADRequest *request = [GADRequest request];
request.testDevices = #[ #"*log id*" ];
[self.bannerView loadRequest:request];
}
GameScene.h
#class GameViewController;
#interface GameScene : SKScene <SKPhysicsContactDelegate>
#property (strong, nonatomic) GameViewController *gameViewController;
#end
GameScene.m
//This line of code will be executed in the "performGameOver" method but it does not work and the banner is still shown?
[self.gameViewController hideBanner];
You should use NSNotification
In viewController.m
- (void)handleNotification:(NSNotification *)notification {
if ([notification.name isEqualToString:#"hideAd"]) {
[self hidesBanner];
}else if ([notification.name isEqualToString:#"showAd"]) {
[self showBanner];
}}
-(void)hidesBanner {
NSLog(#"HIDING BANNER");
[adView setAlpha:0];
self.bannerIsVisible = NO;
}
-(void)showsBanner {
NSLog(#"SHOWING BANNER");
[adView setAlpha:1];
self.bannerIsVisible = YES;
}
In your scene:
Sends message to viewcontroller to show ad.
[[NSNotificationCenter defaultCenter] postNotificationName:#"showAd" object:nil];
Sends message to viewcontroller to hide ad.
[[NSNotificationCenter defaultCenter] postNotificationName:#"hideAd" object:nil];
More info:
https://stackoverflow.com/a/21967530/4078517
I have a split view controller set up as a video player. The master view is a list of available videos and the detail view should play the selected video from the list.
I'm able to send the required properties to the detail view from the master (url, start time, end time...) but I'm only getting audio.
When a selection is made from the master view a delegate -(void)didselectVid: method gets fired in the detail view.
- (void)viewDidLoad
{
[super viewDidLoad];
if (selectedBool) {
timer = [NSTimer
scheduledTimerWithTimeInterval:1.0
target:self
selector:#selector(timedJob)
userInfo:nil
repeats:YES];
[timer fire];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(moviePlayerPlaybackStateDidChange:) name:MPMoviePlayerPlaybackStateDidChangeNotification object:nil];
NSString * videoStr = [NSString stringWithFormat:#"%#/%#", gameVideoURL , videoUrlStr];
NSString * urlStrEscaped = [videoStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL * dataURL = [NSURL URLWithString:urlStrEscaped];
moviePlayerCrl.movieSourceType = MPMovieSourceTypeStreaming;
moviePlayerCrl = [[MPMoviePlayerController alloc] initWithContentURL:dataURL];
[self registerForMovieNotifications];
}
}
- (void)registerForMovieNotifications
{
//movie is ready to play notifications
if ([self.moviePlayerCrl respondsToSelector:#selector(loadState)])
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(moviePlayerLoadStateChanged:) name:MPMoviePlayerLoadStateDidChangeNotification object:nil];
[self.moviePlayerCrl prepareToPlay];
}
//movie has finished notification
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(moviePlayBackDidFinish:) name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
}
- (void)moviePlayerLoadStateChanged:(NSNotification*)notification
{
NSLog(#"load state changed");
//unless state is unknown, start playback
if ([self.moviePlayerCrl loadState] != MPMovieLoadStateUnknown)
{
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerLoadStateDidChangeNotification object:nil];
[self.moviePlayerCrl.view setFrame: CGRectMake(0,
[UIApplication sharedApplication].statusBarFrame.size.height
+ self.navigationController.navigationBar.frame.size.height
+ self.navigationController.toolbar.frame.size.height,
self.view.bounds.size.width,
self.view.bounds.size.height
- [UIApplication sharedApplication].statusBarFrame.size.height
- self.navigationController.navigationBar.frame.size.height
- self.navigationController.toolbar.frame.size.height
- self.tabBarController.tabBar.frame.size.height
)]; // x, y, width, height
[self.view addSubview:self.moviePlayerCrl.view];
[self.view bringSubviewToFront:self.moviePlayerCrl.view];
[self.moviePlayerCrl setFullscreen:NO animated:YES];
[self.moviePlayerCrl play];
[moviePlayerCrl setCurrentPlaybackTime:startPlaybackTime];
}
}
-(void)didselectVid:(VideoClipData *)vid
{
videoUrlStr = vid.evtUrlStr;
startTime = vid.evtStartStr;
endTime = vid.evtEndTimeStr;
gameNamesStr = vid.evtNameStr;
double startTimeInterval = [startTime doubleValue];
double endTimeInterval = [endTime doubleValue];
startPlaybackTime = startTimeInterval;
endPlaybackTime = endTimeInterval;
selectedBool = YES;
[self viewDidLoad];
}
The parent view frame and the MoviePlayerController view's frame have to be same before making MoviePlayerController.view a subview.
i.e Have one more view inside your master view say playerContainerView. Before adding the moviePlayerCrl.view as the subview of playerContainerView, set the playerContainerView and moviePlayerCrl.view to the same frame.
This should work.
I was able to fix this by calling didselectVid differently. With what I assume is proper split view delegation:
in master view .h :
#import "VideoClipData.h"
#protocol DetailDelegate <NSObject>
-(void)didselectEvtVid:(VideoClipData *) vid;
#end
#import <UIKit/UIKit.h>
#class DetailView;
#interface MasterView : UIViewController
#property (strong, nonatomic) DetailView *detailViewController;
#property (nonatomic, unsafe_unretained) id<DetailDelegate> delegate;
master view .m:
- (void)viewDidLoad {
[super viewDidLoad];
self.detailViewController = (DetailView *)[[self.splitViewController.viewControllers lastObject] topViewController];
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[self.detailViewController didselectEvtVid:gameVideos];
}
And instead of calling viewDidLoad again I called [self configureView]; as given in apple's split view example.
I create a MPMoviePlayerController in detailView(UIVIew), now i want to force MPMoviePlayerController to landscape view when user click on FullScreen button. Can i do that? Please give me any suggestion. Thanks in advance.And this is my code to create :
NSURL *movieURL = [NSURL URLWithString:previewString];
movieController = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
[movieController.view setFrame:CGRectMake(10,130, 275 , 150)];
movieController.view.backgroundColor = [UIColor grayColor];
[detailview addSubview:movieController.view];
[movieController prepareToPlay];
movieController.shouldAutoplay = NO;
and willEnterFullscreen () function:
- (void)willEnterFullscreen:(NSNotification*)notification {
NSLog(#"willEnterFullscreen");
donepress = YES;
// nothing
}
I tried search but still do not have any good answer. Please help me. thanks so much
YES, you can do that with two notification observer to change the full orientation.
First, Add two notification observer to your AppDelegate didFinishLaunchingWithOptions method:
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(moviePlayerWillEnterFullscreenNotification:) name:MPMoviePlayerWillEnterFullscreenNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(moviePlayerWillExitFullscreenNotification:) name:MPMoviePlayerWillExitFullscreenNotification object:nil];
Second, Add the method and property
- (void) moviePlayerWillEnterFullscreenNotification:(NSNotification*)notification {
self.allowRotation = YES;
}
- (void) moviePlayerWillExitFullscreenNotification:(NSNotification*)notification {
self.allowRotation = NO;
}
Third, override the supportedInterfaceOrientationsForWindow method, you can return whatever orientation you want
-(NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
if (self.allowRotation) {
return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight;
}
return UIInterfaceOrientationMaskPortrait;
}
I do notifications like this: I have code in mainView:
- (void)viewDidLoad
{
CGRect view1Hrame;
view1Hrame.origin.x = 160;
view1Hrame.origin.y = 215;
view1Hrame.size = self.currenWeatherView.frame.size;
weatherViewController *view1 = [[weatherViewController alloc] initWithForecast:[[Forecast alloc] initWithForecastInCurrentLocation]];
[[NSNotificationCenter defaultCenter] addObserver:view1 selector:#selector(forecastChanged) name:#"forecastChanged" object:nil];
view1.view.backgroundColor = [UIColor colorWithRed:0 green:255 blue:0 alpha:0.06];
[currenWeatherView addSubview: view1.view];
}
in Forecast class:
[[NSNotificationCenter defaultCenter] postNotificationName:#"forecastChanged" object:nil];
in weatherViewController
- (void)viewDidLoad
{
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(forecastChanged) name:#"forecastChanged" object:nil];
// Do any additional setup after loading the view from its nib.
}
It does not work. What did I do wrong?
if you want to call notification in weatherViewController then you need to define this class's viewDidLoad method.
Make sure you are calling viewDidLoad of this viewController.