I wanted to run a instance method from my BPGameController class called "pauseGame" in my app delegate when my application enters background mode and when it resigns, and a instance method called "resumeGame" when the application becomes active again, I've tried a few different things but none have worked for me so far
In BPGameController's viewDidLoad method sign up for the notifications as follows:
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(pauseGame)
name:UIApplicationDidEnterBackgroundNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(resumeGame)
name:UIApplicationDidBecomeActiveNotification object:nil];
When you are done remove yourself from observing.
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationDidBecomeActiveNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationDidEnterBackgroundNotification object:nil];
Related
I have use below for for register notification:-
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(AllSearch:) name:filter object:nil];
-(void)AllSearch:(NSNotification *)notification
{
NSLog(#"abc");
}
and i post notification with this code:-
[[NSNotificationCenter defaultCenter] postNotificationName:filter object:nil];
But "AllSearch" Method not calling.
plz check you code you have to addObserver first
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(AllSearch:) name:filter object:nil];
befor postNotification
[[NSNotificationCenter defaultCenter] postNotificationName:filter object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(youTubeStarted:) name:#"UIMoviePlayerControllerDidEnterFullscreenNotification" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(youTubeFinished:) name:#"UIMoviePlayerControllerDidExitFullscreenNotification" object:nil];
UIMoviePlayerControllerDidExitFullscreenNotification NSNotification not working in iOs 8
i am also faced the same problem but finally ended with using UIWindowDidBecomeVisibleNotification and UIWindowDidBecomeHiddenNotification, for embedded youtube videos(assuming by looking at your selector method names)
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(enteredFullScreen:) name:UIWindowDidBecomeVisibleNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(exitedFullScreen:) name:UIWindowDidBecomeHiddenNotification object:nil];
above is a patch work around,(hope this helps)
I use MPMoviePlayerController and it works fine. I guess that depending what you are using it for, you don't need UIMoviePlayerController.
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(youTubeFinished)
name:MPMoviePlayerDidExitFullscreenNotification
object:self.player.moviePlayer];
In my application I am loading some webpages embedded with Photos and Videos. Also I am using the following notifications to manage the player,
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(embeddedVideoStarted:) name:#"UIMoviePlayerControllerDidEnterFullscreenNotification" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(embeddedVideoEnded:) name:#"UIMoviePlayerControllerWillExitFullscreenNotification" object:nil];
This is working fine in iOS7, but in iOS8 its not working. Any workarounds? Thanks in advance.
This is one option, I have found.. The problem is that is not Will is DID become hidden..
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(embeddedVideoStarted:)
name:UIWindowDidBecomeVisibleNotification
object:self.view.window];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(embeddedVideoEnded:)
name:UIWindowDidBecomeHiddenNotification
object:self.view.window];
If, I find a fix for the second notification I will posted it.. :)
Is it possible to have two different playback notifications in a MPMoviePlayerController?
I want to use these:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlayBackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:self.moviePlayer];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(MPMoviePlayerPlaybackDidChange:)
name:MPMoviePlayerPlaybackStateDidChangeNotification
object:self.moviePlayer];
i did manage to get them working but when i use them together only MPMoviePlayerPlaybackStateDidChangeNotification is called.
Please advise.
I'm adding a view controller as an observer for UIKeyboardWillShowNotification notification.
I have this code in my viewDidLoad:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification
object:nil];
And in my dealloc:
[[NSNotificationCenter defaultCenter] removeObserver:self];
The observer is not being removed even though dealloc is called when the view controller is closed. So when I open it for the second time, NSNotificationCenter will attempt to notify the old object, which is released, and the app crashes.
I have seen several questions here on StackOverflow about this particular problem, but non of the answers works for me.
I've tried removing the observer in viewWillDisappear and viewDidDisappear but the same problem happens.
I'm using ARC.
Have you tried this exact chunk of code in viewWillDisappear?
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
From your explanation I don't think the problem is with the removal of the observer.
Try to trigger the Observer from another viewcontroller. If it's not triggered you'll know the removal is successful and the problem occurs when you are adding the observer the second time.
Maybe try by specifying the parameter name that you have set before like this :
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
Looks like the observer is getting set multiple times. Is your controller inherited from a class which also registers for same notification? That could lead to controller instance getting registered as observer more than once. As a workaround try this, in your controller class where you add the observer,
// Remove as observer first
[[NSNotificationCenter defaultCenter] removeObserver:self];
name:UIKeyboardWillShowNotification
object:nil];
// Then add
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification
object:nil];
This will ensure that the observer is getting added only once.
Hope that helps!
[[NSNotificationCenter defaultCenter] removeObserver:self name:#"name" object:nil];
it works fine with me