Hide keyboard in applicationDidEnterBackground: - screenshot problem - cocoa-touch

When the home button gets pressed I want to hide the keyboard and restore my view to a sane state, so that when the app is started/foregrounded again, there is no textfield selected and the keyboard is hidden.
My app delegate has this implementation of the method:
- (void)applicationDidEnterBackground:(UIApplication *)application {
[tabBarController.view endEditing:YES];
}
The keyboard does indeed get removed, but what I don't get is this: Apple's docs say that a snapshot of the app is taken after the method returns. However this poses a problem with the keyboard slide-down animation. Sometimes when the app is started again for a short moment it shows the keyboard half-way down. I assume the screenshot is taken before the animation was finished.
What would I do to solve this, introduce a short timer in the applicationDidEnterBackground: method? I wonder if there is a "cleaner" solution.

I've not tried this but what about turning animations off just before you resign the keyboard:
- (void)applicationDidEnterBackground:(UIApplication *)application {
[UIView setAnimationsEnabled:NO];
[tabBarController.view endEditing:YES];
}
If this works you need to turn them back on in applicationWillEnterForeground

Related

How to call viewWillAppear with storyboard iOS7

I'm using storyboard.
As I remeber (I worked with ios 4, long time ago=)) everytime, when View appears, calls
-(void)viewWillAppear:(BOOL)Animated {}
method.
Now this method doesn't call, if I press Home button and run app again.
How to fix it?
I need to update one UIView if it appears after home pressing.
The function viewWillAppear is not part of UIView. It is part of UIViewController.
It is called after the view controller's view has been loaded and just before it starts to transition onto screen.
If you create a subclass of UIView and put this function in it then it will never be called because it isn't supposed to be.
Edit
You are correct that viewWillAppear does not get called when the app is coming back from the background.
If you want to update a part of your app when this happens then you can do something in the AppDelegate.
I'd recommend not trying to store properties etc... in the AppDelegate though. You should do something like this...
- (void)applicationWillEnterForeground:(UIApplication *)application
{
// Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
// just pass the message on. In your view you will need to add an observer for this notification
[[NSNotificationCenter defaultCenter] postNotificationName:#"UpdateViewNotification" object:nil];
}
try this, call the super,
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
}

Menu Bar App Never Becomes Reactivated

I'm building a Mac app that only sits in the menu bar with no dock item and no key window and no main menu (it's LSUIElement in the info.plist is set to YES). When I first launch the app, applicationDidBecomeActive: is called, as I expect. However, once another app gains focus, applicationDidBecomeActive: is never called again.
This prevents a text field I have within my app from becoming the first responder. When I first open the app, the text field is editable:
But after another app comes to the foreground, the text field is not editable:
What I've tried:
When the menu is opened, menuWillOpen: is called on the NSMenu's delegate. I've tried placing the following with no success:
[NSApp unhide];
[NSApp arrangeInFront:self];
[NSApp activateIgnoringOtherApps:YES];
[NSApp requestUserAttention:NSCriticalRequest];
[[NSRunningApplication currentApplication] activateWithOptions:NSApplicationActivateIgnoringOtherApps];
[[NSRunningApplication currentApplication] unhide];
I think the issue is probably related to not having any windows to bring to the front. I feel like I'm grasping at straws here. Any help would be greatly appreciated.
I think the issue is with that how the runloop operates when a NSMenu is open, so you should try activating the app before you display the menu. If you're having the NSStatusItem display it, I'd suggest doing it yourself like this:
- (void)toggleMenu:(id)sender
{
// App might already be active
if ([NSApp isActive]) {
[self.statusItem popUpStatusItemMenu:self.menu];
} else {
[NSApp activateIgnoringOtherApps:YES];
}
}
- (void)applicationDidBecomeActive:(NSNotification *)notification
{
[self.statusItem popUpStatusItemMenu:self.menu];
}
That should work, but I think though in general you'll have better luck with an actual window instead of a menu.
You probably need to allow your input to -becomeFirstResponder, maybe by overriding -canBecomeFirstResponder or by calling the become method yourself.
You'd likely have to implement/call these methods for whatever view is housing your text input, or maybe tell your input view to become the first responder.
Either way, it smells like a responder chain issue.
Try calling -makeFirstResponder: on your window. NSWindow is usually the start of the NSResponder chain.
- (void)menuWillOpen:(NSMenu *)menu {
[[NSApp mainWindow] makeFirstResponder:yourTextInputField];
}
I'm assuming your text field already accepts first responder since you said your app launches initially with it as the first responder. If not, make sure your text field overrides -acceptsFirstResponder: to return YES
- (BOOL)acceptsFirstResponder {
return YES;
}
Edit: Ah, see that you don't have a key window. It looks like NSMenu actually has a window associated with it though, and it's safe to call -makeFirstResponder:. Some discussion here suggests overriding -viewDidMoveToWindow: on your view containing your text field in the NSMenu like so:
- (void)viewDidMoveToWindow {
[super viewDidMoveToWindow];
[[self window] makeFirstResponder:yourTextInputField];
}

Hiding the KeyWindow to cause a blank screenshot to be taken

I am trying to prevent the Apple implemmntation of taking a screenshot of the current screen contents when an app suspends into the background. I have found a piece of code that sort of works but it comes with a catch. What it does is that it clears the keywindow on the screen so when the snapshot is taken, it is of a blank screen. This is the code snippet for the functionality:
- (void)applicationWillResignActive:(UIApplication *)application
{
[ UIApplication sharedApplication ].keyWindow.hidden = YES;
}
- (void)applicationDidEnterBackground:(UIApplication *)application
{
[ UIApplication sharedApplication ].keyWindow.hidden = YES;
}
The trouble with the above code is that when the app returns to the foreground, the view is gone as it has become hidden and i cannot unhide it with a simple
[ UIApplication sharedApplication ].keyWindow.hidden = NO;
in the applicationWillEnterForeground method of the app delegate. Does anyone know of a way to regain back the hidden view once i have hidden it in the background methods? Right now it is a black screen as the view has been hidden. What exactly happens when you hide a keywindow before going to background and then coming back. is that keywindow you hid before no longer the keywindow? Can anyone point me in the correct direction?
Thanks
Just made a demo project and was able to reproduce your issue. Indeed, the keyWindow property of the application is nil when applicationWillEnterForeground: is called.
Many times, your application's delegate will have a reference to its window - this is usually the Xcode default template for many applications. I was able to resolve the issue by calling
self.window.hidden = NO;
Instead of [UIApplication sharedApplication.keyWindow.hidden = NO;. Assuming that, like most of the templates, your application delegate has a window reference.
Another alternative that worked for me is to call [self.window makeKeyAndVisible];.
All this was done on the iOS 6 simulator.
Hope this helps!

Suspending keyboard when user press on home button?

I am developing an application were everything is working fine, except one i.e. when user press on home while keyboard is in active and again opens my application the view frame bounds are changing and moving out of bounds. My expected result is keyboard should get suspended or the view should stay in the same position when it is come back from background to foreground with keyboard in-active state.
I hope people understand my scenario and reply ASAP.
Thanks.
I have found the solution to my question, i hope people can use my solution. Below is the code what I have done,
Add the below line of code in your RootViewController file (i.e. which view is coming at first when you open your APP).
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(receivedNotification:) name:UIApplicationDidEnterBackgroundNotification object:nil];
And then add a private method as below
- (void) receivedNotification:(NSNotification *) notification
{
if ([username isFirstResponder])
{
[username resignFirstResponder];
}
else if ([password isFirstResponder])
{
[password resignFirstResponder];
}
}
I hope it help some body,Thank u.
Further assistance please see the mentioned link,
there is a method in the app delegate
- (void)applicationDidEnterBackground:(UIApplication *)application
this method is fired when you press the home button.
do the necessary changes(textField resignFirstResponder) in this method and it should work fine i guess.
EDIT here's the code
in the class where you have your textfield create a method
-(void)performWhenHomeBtnprssed
{
[MytextField resignFirstResponder];
}
then in
- (void)applicationDidEnterBackground:(UIApplication *)application
{
[myClassObj performWhenHomeBtnprssed];
}
also i agree with #valexa you should find the root cause of the problem
In software development it is always better to address the root causes than to patch the effect, in your case there are problems with the positioning of your views and you should address that, foreground/background cycling should not affect the views positioning.

Get rid of the keyboard from applicationDidEnterBackground in the AppDelegate.m

I have an application with Textfields in my MainViewController.m file. There is also a scrollview in that file, so when the keyboard comes up, the view scrolls so the user can see the textfield. The keyboard is dismissed when the user taps on the screen. Everything is working well EXCEPT in the case that the user hits the home button to put the app in the background and then comes back to it. In this case, the keyboard is still up, but my scrollview is down with textfields hidden. Ideally I would like to have the keyboard be dismissed as well.
Having looked into it, the methods that are called are all in the AppDelegate.m file (unfortunately it does not go into ViewDidLoad or any of the View lifecycle methods). How do I dismiss the keyboard from applicationDidEnterBackground in the AppDelegate.m file?
I am kind of a newbie - I have tried making a +dismisskeyboard function in my MainViewController file and calling it from the Appdelegate, but my Textfields are all instance variables and that does not work. I have also tried to create a textfield in my AppDelegate file and then do this -
[_someField becomeFirstResponder];
[_someField resignFirstResponder];
but this also does not work... I can't figure out how to link anything on my storyboard to the AppDelegate property of someField.
Can anyone suggest the correct approach to tackle this problem?
Just register a method for UIApplicationDidEnterBackgroundNotification in your MainViewController class and dismiss your keyboard there. e.g.
Register for the notification
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(receivedNotification:) name:UIApplicationDidEnterBackgroundNotification object:nil];
then add this method
- (void) receivedNotification:(NSNotification *) notification
{
[txtFld resignFirstResponder];
}