I'am trying to call a function when the app com back to Foreground by applicationDidBecomeActiveNotification. I have it in iOS 8 and iOS 7 and it works well. But not in iOS 9 ? It docent call first time you open the app
-(void) viewWillAppear:(BOOL)animated {
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:#selector(applicationDidBecomeActiveNotification:)
name:UIApplicationDidBecomeActiveNotification
object:[UIApplication sharedApplication]];
}
- (void)applicationDidBecomeActiveNotification:(NSNotification *)notification {
//stuff
}
It is every notification center that is not calling at startup...
I hade the same problem and for me it worked to move the addObserver code to awakeFromNib. Another solution could be to add a delay to the addObserver as in the example below:
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(applicationDidBecomeActive) name:UIApplicationDidBecomeActiveNotification object:nil];
});
See Apple Foundation update for NotificationCenter
This is what you need,
NSNotificationCenter.defaultCenter().addObserverForName(UIApplicationDidBecomeActiveNotification, object: nil, queue: nil) { note in
self.applicationDidBecomeActiveNotification(note)
}
Related
Is it possible to receive a notification in a Cocoa app for when Mission Control is launched?
I tried the following with no success:
- (void)applicationDidFinishLaunching:(NSNotification *)notification {
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(notify:) name:NSWorkspaceActiveSpaceDidChangeNotification object:nil];
}
- (void)notify:(NSNotification *)notifcation {
NSLog(#"mission control called");
}
I was wondering how to swipe the ViewController with a visible keyboard?
in iOS 7 I can swipe the ViewController from side to side, but the keyboard stays put.
in short, I would like to get to the following state:
Thanks!
Update:
I can't recommend the original solution. While it performed well (when it performed at all), it was an unreliable hack, and could easily break the pop gesture recognizer.
My colleague Dave Lyon came up with a great solution using iOS 7 view controller transitions and packaged it up into a pod:
https://github.com/cotap/TAPKeyboardPop
Once installed, just import the main file and you should be good to go.
Original:
I'd love to know if there's a better way of doing this, but I was able to achieve the behavior by adding the keyboard's view as a subview of the view controller's main view:
- (void)viewDidLoad
{
[super viewDidLoad];
self.textView.inputAccessoryView = [UIView new];
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
[center addObserver:self
selector:#selector(keyboardWillHide:)
name:UIKeyboardWillHideNotification
object:nil];
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)keyboardWillHide:(NSNotification *)note
{
if (self.textView.isFirstResponder) {
UIView *keyboardView = self.textView.inputAccessoryView.superview;
if (keyboardView) {
dispatch_async(dispatch_get_main_queue(), ^{
[self.view addSubview:keyboardView];
});
}
}
}
I've found you can also animate the keyboard with the gesture (via addTarget:action:), but the performance is abysmal and doesn't cleanly animate if the gesture is prematurely canceled.
I have done the following - once played a QTMovie (on Mountain Lion) and wanted to get notification when a movie ended. But the notification never got called! Can anybody tell me what have I done wrong?
- (void)playMovie:(QTMovie *)movie {
[self.movieView.movie stop];
if (movie) {
self.movieView.movie = movie;
[movie gotoBeginning];
[movie play];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(stopMovie:) name:QTMovieDidEndNotification object:self];
}
}
- (void)stopMovie:(NSNotification *)notification {
NSLog(#"stop movie!");
[[NSNotificationCenter defaultCenter] removeObserver:self name:QTMovieDidEndNotification object:nil];
}
Try changing to the following (note the object parameter):
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(stopMovie:)
name:QTMovieDidEndNotification
object:movie];
I have a alertview that starts in a view A and must stop in view B. How can I stop the alertview on B?
thks
How are you moving from A to B while a UIAlertView is displayed? Maybe post some code.
This scenario does not seem user-friendly, but there is a way you can dismiss the alert from A in B.
View A
Create an NSNotificationCenter and point it to a method that dismisses the alert:
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(dismissAlert) name:#"dismissAlert" object:nil];
And the notification should call something like the following:
- (void) dismissAlert:(NSNotification *)notification
{
[alertView dismissWithClickedButtonIndex: 0 animated: YES];
}
View B
Now when you want to dismiss the alert, call the notification you created in View A:
[[NSNotificationCenter defaultCenter] postNotificationName:#"dismissAlert" object:nil];
Use dismissWithClickedButtonIndex:animated:
I want to write a simple menubar app for Mac OS X. The user will only want to use that app when Safari is opened. To not clutter the menubar unnecessarily, I want to hide and show the menubar icon depending on whether Safari is open or not.
Is there maybe some notification that my app could register for? The only workaround I can imagine is poll the running processes and see if Safari is launched, but that doesn't seem to be an elegant way to solve my problem...
NSWorkspaceDidLaunchApplicationNotification and NSWorkspaceDidTerminateApplicationNotification. (There are equivalent Carbon Events.)
Use kEventAppFrontSwitched in Carbon Event Manager to get notifications when another application becomes active.
Use this code: http://cl.ly/2LbB
// usleep(40500);
ProcessNotif * x = [[ProcessNotif new] autorelease];
[x setProcessName: #"Safari"];
[x setTarget: self];
[x setAction: #selector(doStuff)];
[x start];
This will run the selector -doStuff when Safari runs. If you get an error, uncomment the usleep() line.
Got same problem, but thanks to JWWalker, documentation and google wrote this code:
// i need to register on button event, you can do it even in applicationDidFinishLaunching
- (IBAction)Btn_LoginAction:(id)sender {
...
NSNotificationCenter *center = [[NSWorkspace sharedWorkspace] notificationCenter];
[center addObserver:self selector:#selector(appLaunched:) name:NSWorkspaceDidLaunchApplicationNotification object:nil];
[center addObserver:self selector:#selector(appTerminated:) name:NSWorkspaceDidTerminateApplicationNotification object:nil];
}
// remember to unregister
- (void)ManageLogout:(NSInteger)aResult {
...
NSNotificationCenter *center = [[NSWorkspace sharedWorkspace] notificationCenter];
[center removeObserver:self name:NSWorkspaceDidLaunchApplicationNotification object:nil];
[center removeObserver:self name:NSWorkspaceDidTerminateApplicationNotification object:nil];
}
- (void)appLaunched:(NSNotification *)note {
[GTMLogger myLog:kGTMLoggerLevelDebug fmt:#"MainWinDelegate::appLaunched: %# (%#)\n", [[note userInfo] objectForKey:#"NSApplicationBundleIdentifier"], [[note userInfo] objectForKey:#"NSApplicationProcessIdentifier"]];
if ( [[[note userInfo] objectForKey:#"NSApplicationBundleIdentifier"] isEqualToString:#"app.you.monitor.bundle.identifier"] ) {
// do stuff
}
}
- (void)appTerminated:(NSNotification *)note {
[GTMLogger myLog:kGTMLoggerLevelDebug fmt:#"MainWinDelegate::appTerminated: %# (%#)\n", [[note userInfo] objectForKey:#"NSApplicationBundleIdentifier"], [[note userInfo] objectForKey:#"NSApplicationProcessIdentifier"]];
if ( [[[note userInfo] objectForKey:#"NSApplicationBundleIdentifier"] isEqualToString:#"app.you.monitor.bundle.identifier"] ) {
// do stuff
}
}