Reputation: 3701
I have a PIN view controller which needs to be presented every time the - (void)applicationWillEnterForeground:(UIApplication *)application
fires. I have multiple view controllers and when the app enters background the user could be on any of them.
The problem is I don't know how to present the PIN view controller over any view controller that is currently active. Here's how my implementation looks:
- (void)applicationWillEnterForeground:(UIApplication *)application
{
ResourceSingleton *resource = [ResourceSingleton sharedSingleton];
if ([resource checkIfPINIsEnabled])
{
PinViewController *pinView = [[PinViewController alloc] initWithMode:kPINViewControllerModeEnter];
pinView.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self.window.rootViewController presentViewController:pinView animated:YES completion:NULL];
}
}
But the PIN view appears only if I'm at the first view controller (the root one). How to pop it up on any view controller?
I have seen Using applicationwillenterforeground for a passcode screen but there has to be a better way or am I wrong? This will be for iOS 7 so if only 7 has such a functionality its ok but I am pretty sure it can be done on 6 as well.
Upvotes: 1
Views: 4762
Reputation: 966
Convert to the objective-C codes to Swift 4, put the codes into func applicationWillEnterForeground
let pres = self.window?.rootViewController?.storyboard?.instantiateViewController(withIdentifier: "StartVC")
let navi: UINavigationController = self.window?.rootViewController as! UINavigationController
if ((navi.presentedViewController) != nil) {
navi.dismiss(animated: true) {
navi.present(pres!, animated: false, completion: nil)
}
} else {
navi.present(pres!, animated: false, completion: nil)
}
Upvotes: 0
Reputation: 16790
If your root view controller is a NavigationController, then pushing or presenting should work in most cases. You already have all the code in place, just create a navigation controller. The only case this would not work is if there is a modal view controller already presented. In that case that needs to be dismissed first.
Here is a little messy implementation that takes care of this case too.
AKPresentedViewController *pres = [self.window.rootViewController.storyboard instantiateViewControllerWithIdentifier:@"pres"];
UINavigationController *navi = ((UINavigationController*)self.window.rootViewController);
if (navi.presentedViewController) {
[navi.presentedViewController dismissViewControllerAnimated:YES completion:^{
[navi presentViewController:pres animated:NO completion:nil];
}];
} else {
[navi presentViewController:pres animated:NO completion:nil];
}
Upvotes: 3
Reputation: 6432
You could present your PIN view controller the way they mention here.
And for popping the PIN view controller I'm guessing that the user has to enter the correct PIN so the PIN view controller goes away. In that case it can pop itself:
[self.presentingViewController dismissViewControllerAnimated:YES completion:nil];
Hope this helps!
Upvotes: 0
Reputation: 104082
You could have the app delegate handle the logic for the PIN view, and have that be a view, rather than a view controller. Just add the view as a subview of the window, and it will be shown over anything else.
- (void)applicationWillEnterForeground:(UIApplication *)application {
UINib *pinNib = [UINib nibWithNibName:@"PINView" bundle:nil];
UIView *pinView = [pinNib instantiateWithOwner:self options:nil][0];
[self.window addSubview:pinView];
}
If you make the app delegate the File's Owner of the xib, then you can hook up any outlets you need in the view to the app delegate.
Upvotes: 1