When notification is dragged and released, ApplicationBecomeActive gets called in iOS - ios

I have password option in my app, so if user sets a password, then I show a password page first when app becomes active and user should enter the password to use the app
I have written the code this way
- (void)applicationDidBecomeActive:(UIApplication *)application
{
if([password length])
{
EnterPasswordViewController *passwordView = [[EnterPasswordViewController alloc] initWithNibName:#"EnterPasswordViewController" bundle:nil];
[self presentViewController:passwordView animated:YES completion:NULL];
}
}
This works but the problem is that whenever I am inside the app and when I just drag the notifications from the top and leave it, what I have seen is the applicationDidBecomeActive gets called and because of this, password page is shown again, So I am not understanding how to solve this
Regards
Ranjit.

Consider presenting your password controller inside the -applicationWillEnterForeground: method instead.
willEnterForeground is called only when your app is closed completely (into the background or the device is locked) and then comes back to the foreground
The one you're currently using will also get called when the system presents an alert for you, such as asking the user to allow push notifications even for your app.
- (void)applicationWillEnterForeground:(UIApplication *)application
{
// Present password controller
}
Alternatively you could reverse the logic and present it when your app enters the background, as opposed to entering the foreground.

Related

How to show alert when the app is being closed in ios

When my app is running in foreground, If user clicks on home button i want to show some alert like
Do u want to exit the app with Yes/No buttons. Is there any delegate methods called when home button is clicked…
Thank you….
There is no way to do this on iOS. I think you want a similar behavior to Android back button.
Think of iOS home button as Android home button, which doesn't let you prompt user either.
You can not stop the app to go to background, even if you show the alert message, it will go to the background anyways when you hit the home button and you won't see the alert message either.
There is no delegate method that would trigger when the app is going to the background, but there is a delegate method that will trigger when your app has totally gone to background.
Just to show you visually-
In the AppDelegate implementation file(.m), I added an alert message at the end of applicationDidEnterBackground method and put a break point just to check if it is entering that block as I expected. See the result--
You can use these methods in AppDelegate.m
- (void)applicationDidEnterBackground:(UIApplication *)application {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}
- (void)applicationWillEnterForeground:(UIApplication *)application {
// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

May I change the behavior of the close button on MAC with Xcode

I'm developing a software on Mac on which a login screen appears straight after the software has been opened.
This is a typical login screen, but, I'd like that when the users closes this pop-up, not only the login screen closes, but also the software himself.
Is that possible ?
If you're using storyboard to manage your view hierarchies, then, in your login view controller, you can use the following logic for closing the app:
- (void)viewDidDisappear {
[super viewDidDisappear];
if (shouldTerminalApp) { // you should have this iv to decide if user has entered the login credential for login or if the user closed the login view without entering anything.
[[NSApplication sharedApplication] terminate:nil];
} else {
// perform login procedures
}
}
You can also observe NSWindowWillCloseNotification and in the notification handler, simply call [[NSApplication sharedApplication] terminate:nil] in the correct case.

What exactly happens with app when user presses home button?

What exactly happens with an app when the user presses the home button? Does iOS make a snapshot of current app's state?
If I'm right, how to "slip in" another snapshot? I need it for showing the main iOS screen after user presses home button twice.
In order to achieve that, you could add a subview (i.e. ImageView with an image of your main screen) to your window in - (void)applicationDidEnterBackground:(UIApplication *)application.
When coming back (in - (void)applicationWillEnterForeground:(UIApplication *)application) you have to remove the subview.
However, Apple recommends to always show the latest state of the app (best user experience).
Example of AppDelegate implementation:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
/* other code */
self.overlayView = [[UIImageView alloc] initWithFrame:self.window.bounds];
self.overlayView.image = [UIImage imageNamed:#"mainscreen_screenshot.png"];
/* other code */
}
- (void)applicationDidEnterBackground:(UIApplication *)application
{
[self.window addSubview:self.overlayView];
}
- (void)applicationWillEnterForeground:(UIApplication *)application
{
[self.overlayView removeFromSuperview];
}
iOS takes screenshot before your application goes to background.
I got it from Apple's official documentation.
Remove sensitive information from views before moving to the background: When an app transitions to the background, the system takes a snapshot of the app’s main window, which it then presents briefly when transitioning your app back to the foreground. Before returning from your applicationDidEnterBackground: method, you should hide or obscure passwords and other sensitive personal information that might be captured as part of the snapshot.
So, Here We can hide our "sensitive personal information" and the system takes a snapshot of the app’s main window and we are not able to change its feature.
# sensitive personal information
Set a blurry screen overlay before the app goes in the background and once the app becomes active remove this overlay (check #cweinberger's answer).
If it is iOS 7 or later you can use the function ignoreSnapshotOnNextApplicationLaunch
Also, allowScreenShot flag can be explored in Restrictions Payload.
For deeply read about it Then this is the best documentation.

Changing rootViewController in applicaitonWillEnterForeground

Long story short, I'm trying to change my iOS app's rootViewController on applicationWillEnterForeground:, like so:
- (void)applicationWillEnterForeground:(UIApplication *)application
{
MyViewController *controller = [[MyViewController alloc] init];
self.window.rootViewController = controller;
}
However, when iOS performs the "zoom in" animation that is performed when an app is moved from the background to the foreground, it still shows the previous rootViewController's view. Then, as soon as the animation is complete, the app blasts the new rootViewController's view onto the screen.
One way to solve this is to simply move that code to - (void)applicationDidEnterBackground:, but the problem with this solution is that, in my app, there is no way to tell if a new rootViewController will be assigned until - (void)applicationWillEnterForeground:(UIApplication *)application (it is based on time passed since leaving the app).
How can I force the app to redraw before iOS performs the animation taking the app from the background to the foreground?
I believe this is not possible. The screen that iOS shows of your app when it comes into the foreground is actually a screenshot the system took when the app went into the background. There is no way to manipulate or replace that image at the time the app comes back into the foreground.
This behavior is partly documented in the Moving to the Background section of the iOS Application Programming Guide:
Apps can use their applicationDidEnterBackground: method to prepare for moving to the background state. When moving to the background, all apps should do the following:
Prepare to have their picture taken. When the applicationDidEnterBackground: method returns, the system takes a picture of your app’s user interface and uses the resulting image for transition animations. If any views in your interface contain sensitive information, you should hide or modify those views before the applicationDidEnterBackground: method returns.
Apple does not explicitly document that you cannot modify or replace this screenshot at a later time but neither do they say the opposite anywhere I know of.

go to another page in applicationDidEnterBackground in iOS in device

In my application, When the application goes to background I am calling a Passcode page (Passcode Page which does authentication).
My requirement is when the user launch the app from the foreground he will see the passcode page. If he enters the correct passcode then only he can see the rest pages.
In delegate.m file
- (void)applicationDidEnterBackground:(UIApplication *)application
{
PasscodeViewController *passcodeController = [[PasscodeViewController alloc] initWithNibName:#"PasscodeViewController" bundle:nil];
[navController pushViewController:passcodeController animated:YES];
}
When I am launching the application from the background then It is showing me the previous page( from which page I came to background ) for a fraction of second and after that Passcode page comes.
But I want to hide my confidential information from others (who doesn't know the passcode) that are shown in the previous page.
It is correctly working in Simulator but in Device it is not working properly.
Can you please guide me in that ?
OR
Is it the normal behavior of the iOS device ? What ever the page transition it will do, it will perform while the application is running in foreground.
I am not sure about that. Please tell me where I went wrong.
Thank you.
Every app I've used with a similar feature has operated as you describe, with the fractional-second flash before the lock view appears.
I think it's a matter of when UIKit thinks it needs to re-render... We had a similar case with a splash screen, but using applicationDidEnterBackground for adding the splash helped.
My idea is to avoid the animating, using
[navController pushViewController:passcodeController animated:NO];
Whenever your app goes background add a UIView with white background.
Whenever your app comes up push your PasscodeViewController view on top
Please add observers for UIApplicationDidEnterBackgroundNotification and UIApplicationWillEnterForegroundNotification to do the above functionality
Also be sure to remove the Observers when your view disappears
When user enters correct passcode remove the UIView.
Try applicationWillResignActive:
- (void)applicationWillResignActive:(UIApplication *)application
{
PasscodeViewController* passcodeController = [[PasscodeViewController alloc] initWithNibName:#"PasscodeViewController" bundle:nil];
[navController pushViewController:passcodeController animated:YES];
}
When Application goes to background push the passcode viewcontroller to navigationcontroller in the delegate applicationDidEnterBackground because there will be that fractional flash almost all time u can have the passcodecontroller pushed before entering background.

Resources