I am making a some kind of password security in my app, so nobody except the iphone owner can open an app. And I'd like to load my password view which I created every time when user clicks on the app icon.
I know there is a nice method in AppDelegate called -(void)applicationWillResignActive:(UIApplication *)application. However, in AppDelegate you are not able to use presentModalViewController: animated: which I like so much.
How can I load a password view every time, when user opens the app ?
Thanks.
In the AppDelegate, you will certainly load a view controller somewhere. E.g. a navigationcontroller:
-(void)applicationWillResignActive:(UIApplication *)application {
// probably more code....
[window addSubview:navigationController.view];
[window makeKeyAndVisible];
return YES;
}
now, in this navigationController, you could write a method like
(void) lockScreen {
// code to for presentModalViewController: animated:
}
I'd use applicationDidEnterForeground
- (void)applicationDidEnterForeground:(UIApplication *)application {
[navigationController lockScreen];
}
use nsnotification to do that. add an observer with a method which perform presentation of model view and in didBecomeActive. post the notification.
Related
I know there are quite a few threads about that topic, and I tried every single option. But nothing works.
So, here what I have, a loginviewcontroller and a tabbarviewcontroller. If the device is already registered, the tabbar should appear, if not the loginview. I have the tabbarviewcontoller as initial view controller in storyboard. This works if the device is registered.
This is what I basically do:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
//define viewcontroller
LoginViewController *loginviewcontroller = [[LoginViewController alloc]init];
//check if device id in coredata
NSString *deviceId = [self retrieveFromUserDefaults:cKey_DeviceId];
if(deviceId == nil){
NSLog(#"device not registered");
[self.window setRootViewController:loginviewcontroller];
[self.window addSubview:loginviewcontroller.view];
}
//show them
[self.window makeKeyAndVisible];
return YES;
}
I have a NSlog output in my loginview viewdidload method, so I know, that the loginview is loaded. But the screen is black!!! I dont know why. The viewcontroller runs the viewdidload method, but there is just no screen output.
What do I do wrong???
thanks
dominik
If LoginViewController has a .xib file, you're calling the wrong init method. You want the initWithNibName:bundle: method.
Also, you do not need to call [self.window addSubview:loginviewcontroller.view]; after you set the root view controller.
And if the deviceId is NOT null, then you will get a blank screen.
And loginviewcontroller is leaking memory. You should release it after setting it to the root view controller.
In most cases using a password, I've found it's best to load the initial (already logged in) screen, then check to see if a login is needed. If it is, immediately put up a login screen as a modal view. Launch the login screen from viewDidLoad in your initial screen.
I have a UIViewController in which it should pop up a LoginViewController if a user is not yet login. The question is where should I call this:
LoginViewController* lvc = [[LoginViewController alloc] init];
lvc.delegate = self;
//[lvc setModalPresentationStyle:UIModalPresentationFullScreen];
[self presentModalViewController:lvc animated:NO];
[lvc release];
should it be in the viewDidLoad or in the viewWillAppear? I guess it makes sense to put it in the viewWillAppear? I tried to put it inside the viewDidLoad and it gives me an extra border to the left and right of the view. Why is this?
UPDATE:
What I am trying to do here is to call presentModalViewController on the DetailViewController of a UISplitViewApplication. However nothing happens when I do so. I tried creating a new fresh project of a UISplitViewApplication and still it didn't work.
The question is why? and how do I present a modal view in the viewWillAppear of a UISplitViewApplication
The modal window tries to initialize itself with respect to the view controller that called it (resizing the nib, for example). Creating and displaying it in its parent's viewDidLoad can sometimes give it wrong information since the parent is still itself loading. This is why you are seeing discrepancies. Presenting the modal controller in viewDidAppear is better in this case since all the parameters are ready to pass to the modal controller so it can load its own view properly. Though sometimes if you have a lot to load, even that isn't enough and you will need to wait longer before you can present your modal view (which doesn't sound like your case at all, so there should be nothing to worry about there). I hope this helps, though
I would place something like this in the AppDelegate.
- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions
{
[self.window addSubview:self.viewController.view];
[self.window makeKeyAndVisible];
// Show the login screen if the user hasn't logged in yet
if (... login check here...)
{
LoginViewController* loginController = [[LoginViewController alloc] init];
[self.viewController presentModalViewController:loginController animated:NO];
[loginController release];
}
}
Your login screen will be placed on top of your normal viewcontroller. After a succesfull login dismiss the LoginViewController and your user can start using your app.
So let's say I have a viewController named homeViewController, and another view controller named listViewController
I display listViewController on top of homeViewController as a modal.
If the user clicks the off button, and then comes back to the app the modalViewController is gone.
ListViewController *listViewController = [[ListViewController alloc] init];
[self presentModalViewController:listViewController animated:NO];
[listViewController release];
Note: Application doesn't startup from scratch when this occures and the previous state is still visible
I'm assuming that by "off button" you mean the user locks the iDevice.
I just tried this in one of my apps and the modal view controller is still there after unlocking. My guess would be that it's something unrelated to the code you have posted. I would check your - (void)applicationWillResignActive:(UIApplication *)application method in your app delegate class and see if there's anything there that would dismiss the modal view controller.
Here is what the problem was.
When the user locks the screen I remove homeViewController from window
[homeViewController removeFromSuperview];
When user starts the app again I do
[windows addSubview:homeViewController];
that brings homeViewController on top of its modeal
I'm trying to change view upon receive a push notification while the app is still running. I tried using this in the AppDelegate.m
-(void)application:(UIApplication *)application didRecieveNotification:(NSDictionary *)userInfo
{
TestClass *aTestClassViewController = [[TestClass alloc]initWithNibName:#"TestClass" bundle:nil];
[self presentModalViewController:aTestClassViewController animated:YES];
[aTestClassViewController release];
}
But it didn't work. I can't even start up the app again. so I'm guessing this is the wrong way to do it.
Any idea guys? I would appreciate it.
Solved***
I did it this way ->
I showed an alert view first (Which i needed anyways)
then used the method of
-(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex{
TestClass *aSelectionScreenViewController = [[TestClass alloc] initWithNibName:#"TestClass" bundle:nil];
[viewController presentModalViewController:aSelectionScreenViewController animated: YES];
[aSelectionScreenViewController release]; }
We're missing some context about your application, but your basic problem is that it's the application delegate object which is receiving the notification, not a view controller. That's why you can't just do [self presentModalViewController:someViewController];
I think it's the snippet from your own answer that gives what you need: your app delegate (presumably) has a 'viewController' member, which is the root view controller for the application. It is that viewController object you need to prod into doing whatever it is you need. In the app I'm looking at right now, I have a tabBarController member in the app delegate, and I show an alert view and/or change the selected tab index when a notification comes in.
I would have your app delegate call a function on your main view controller when a message comes in, and have that function show the alert view, then do whatever state changes you need to do to make the main view controller reflect the received notification.
This is a total noob question.
I have a starting view -- it's very simple: just some text and a button. When the user clicks the button, I want to go to the real "meat" of the application, which is a Navigation/Table View. How do I connect the button on the IntroViewController to a transition to the RootViewController? I don't want to make the IntroViewController a full Navigation controller and push the new view because that lets the user go back. I'm looking for some combination of code snippets and Interface Builder instructions.
If you're using a UINavigationController you could just use setViewControllers:animated:
You could even fake your "back" history if you wanted to go to someplace you've never been before.
For the comments below, here's what I have in my application delegate:
- (void)applicationDidFinishLaunching:(UIApplication *)application {
// Override point for customization after app launch
navController = [[UINavigationController alloc] initWithRootViewController: viewController];
[window addSubview:navController.view];
[window makeKeyAndVisible];
}