سؤال

I want to launch the login screen when first time app launches other wise app work simple but problem is that again again it goes to login screen.

here is the code which i am using in didFininsh

I want the user show go to login screen first time only and next time it should show splitViewController

     [[NSUserDefaults standardUserDefaults] registerDefaults:[NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:YES],@"firstLaunch",nil]];

if ([[NSUserDefaults standardUserDefaults] boolForKey:@"firstLaunch"]) {



    [self.window addSubview:[splitViewController view]];

    LoginViewController *targetController = [[LoginViewController alloc] initWithNibName:@"LoginViewController" bundle:nil];
    targetController.modalPresentationStyle = UIModalPresentationFullScreen;


    [self.splitViewController presentViewController:targetController animated:YES completion:nil];

    }

else {


    [self.window addSubview:[splitViewController view]];

}


// my comment[window addSubview:splitViewController.view];
[window makeKeyAndVisible];


return YES;
هل كانت مفيدة؟

المحلول 4

Try this...

if ([[NSUserDefaults standardUserDefaults] boolForKey:@"firstLaunch"]) {

[[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"firstLaunch"];

    [self.window addSubview:[splitViewController view]];

    LoginViewController *targetController = [[LoginViewController alloc] initWithNibName:@"LoginViewController" bundle:nil];
    targetController.modalPresentationStyle = UIModalPresentationFullScreen;


    [self.splitViewController presentViewController:targetController animated:YES completion:nil];

    }

else {


    [self.window addSubview:[splitViewController view]];

}

نصائح أخرى

If you want to show only one time you Login Screen then you need to help of Database.

I give simple example of how to work with database table.

Suppose your You have Table name is "Login"

add Login Table in 4 field

id - auto-inc.
username - TEXT;
password - TEXT;
status - TEXT, default 0;

Take two ViewController

1) LoginViewController
2) HomeViewController

And in application:didFinishLaunchingWithOptions

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

     /// here you need to get status from "Login" table 

     if (status == 0)
     {
        /// code of initialize LoginViewController
       self.window.rootViewController = self.LoginViewController;
     }
     else if (status == 1)
     {
          /// code of initialize HomeViewController
       self.window.rootViewController = self.HomeViewController;     
     }
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    return YES;
}

In above code, When you start you app. first time at that time check condition of status from Login table.

first time you got status = 0 because we put default value of status is 0 so here,

self.window.rootViewController = self.LoginViewController;

At that time you enter userName and Password to your Login Screen. after entered username and password you click on Login Button at That time you also need to fire Query of update status of you 'Login' table is 1

And when again you star your application at that time you ger status is 1 so,

self.window.rootViewController = self.HomeViewController;     

So, you can not display your login screen again.

if (![[NSUserDefaults standardUserDefaults] boolForKey:@"firstLaunch"]) {



[self.window addSubview:[splitViewController view]];
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"firstLaunch"];
LoginViewController *targetController = [[LoginViewController alloc] initWithNibName:@"LoginViewController" bundle:nil];
targetController.modalPresentationStyle = UIModalPresentationFullScreen;


[self.splitViewController presentViewController:targetController animated:YES completion:nil];

}

else {


[self.window addSubview:[splitViewController view]];

}


// my comment[window addSubview:splitViewController.view];
[window makeKeyAndVisible];


return YES;

You should implement the viewDidAppear method inside the Controller that needs to be skipped and then in the viewDidAppear method, do a segue to the view you want to send the user to, something like this:

- (void)viewDidAppear:(BOOL)animated {

  //if user is already logged in then skip to welcome view
  //implement your login method here

  if ([self isUserLoggedIn]) {
    [self performSegueWithIdentifier:@"WelcomeTo" sender:self];
  }

}

for ios7

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top