问题
I am trying to display a passcode/pincode (modal view controller) upon launching the app. You may see the code in AppDelegate.h :
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
if (![[NSUserDefaults standardUserDefaults] boolForKey:@"passcode_in"]) {
//display passcode screen
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"PasscodeViewController"];
[vc setModalPresentationStyle:UIModalPresentationFullScreen];
[self presentModalViewController:vc animated:NO];
} else {
NSLog(@"No Passcode Screen");
}
return YES;
}
The problem is that AppDelegate doesn't support to display a modal view controller (presentModalViewController). I am not going to use .xib files, only Storyboard for my app. Does anybody know what is wrong with it? Any suggestion appreciated.
RESOLVED
I followed the instruction given to one of my previous posted questions https://stackoverflow.com/a/10303870/1344459 I resolved the issue by only adding some code into two methods applicationDidEnterBackground and applicationWillTerminate in AppDelegate.m for PinCodeViewController(modal) upon launching app. Now it is working so smooth.
回答1:
My solution to the same issue was to create another view controller in storyboard, link it to my initial view controller via a custom segue, and call the segue in the ViewController's viewDidLoad method. LoginSegue.h
#import <UIKit/UIKit.h>
@interface LoginSegue : UIStoryboardSegue
@end
LoginSegue.m
#import "LoginSegue.h"
@implementation LoginSegue
- (void)perform {
UIViewController *src = (UIViewController *) self.sourceViewController;
UIViewController *dst = (UIViewController *) self.destinationViewController;
[UIView transitionWithView:src.navigationController.view duration:0.0
options:UIViewAnimationTransitionNone
animations:^{
[src.navigationController presentViewController:dst animated:NO completion:nil];
}
completion:NULL];
}
@end
Then in storyboard, select your segue and set the segue class to LoginSegue and the identifier to whatever you like. Mine is @"toLogin". And include the following in your viewDidLoad:
[self performSegueWithIdentifier:@"toLogin" sender:self];
回答2:
presentModalViewController is a method of UIViewController class. Your AppDelegate is a NSObject or UIResponder so won't recognize it.
You should present your passcode/pincode view non-modally, putting it in the first UIViewController of your Storyboard.
If you need to display it modally, even if not necessary, then present your modal view from the first UIViewController of your Storyboard and not from AppDelegate.
In your UIViewController you should write something like this:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"PasscodeViewController"];
[vc setModalPresentationStyle:UIModalPresentationFullScreen];
[self performSelector:@selector(presentModal)
withObject:nil
afterDelay:0.0];
}
- (void)presentModal {
[self presentViewController:vc animated:NO completion:NULL];
}
N.B. You need performSelector. If you do not use it your view won't be shown.
Please, note that presentModalViewController is now deprecated, use presentViewController instead.
回答3:
If the passcode is a precondition for login, then it might make sense to make it part of the login path.
To do this in storyboard, paint a navigation controller, delete the UITableViewController root that you get by default, and set up your PasscodeViewController as the root. Then add a push segue from there to the LoginViewController.
The logic in PasscodeViewController is similar to what's been discussed here: On viewWillAppear: it can check whether a passcode requirement is fulfilled or not. If it's needed, let the passcode view appear and do it's work. If you already have the passcode perform the segue to the LoginViewController. If neither are needed, dismiss.
Lastly, once the passcode is collected by PasscodeViewController, it can decide whether to require login (perform push segue to LoginViewController), or just start the app (dismiss).
Hope that's helpful.
来源:https://stackoverflow.com/questions/10307120/display-passcode-pincode-upon-launching-the-app-storyboard