简体   繁体   中英

IOS & objective C development - how to make the login page appear to the user?

I am in my first days of IOS app development, I am trying to build an authentication system for an already existent iOS App using Objective C.

The app's rootviewcontroller is a tabsview followed by navigationControllers.

What i've done so far:

1- creating the loginviewController class & designing it's UI in the storyboard

2- the same thing for the "registration" & "recover my password" classes

3- linking the root viewcontroller with the login page with a segue of type modal.

4- linking the login page with the registration & recover my password pages with segues of type push.

Now i don't know the steps that i should follow to make the login page appear to the user when he first enters the app & eventually store his state so he can access the app later without having to enter his credentials every time (unless he logs out).

Any help is greatly appreciated, thank you

I am available for any clarifications or eventually some screenshots/source code if needed.

Edit 1 : this is the content of my didFinishLaunchingWithOptions method in my appdelegate.m :

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    TabsViewController *controller = (TabsViewController *)self.window.rootViewController;


    controller.managedObjectContext = self.managedObjectContext;
    _observer = [[MyStoreObserver alloc] init];
    [[SKPaymentQueue defaultQueue] addTransactionObserver:_observer];

    //Create sub directories in doesn'n exist
    NSString *documentsDirectory =[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
    NSString *pathToFile = [documentsDirectory stringByAppendingString:@"export"];
    BOOL isDir = YES;
    BOOL isFile = [[NSFileManager defaultManager] fileExistsAtPath:pathToFile isDirectory:&isDir];

    if(isFile)
    {
        //it is a file, process it here how ever you like, check isDir to see if its a directory
    }
    else
    {
        [self createSubDirectories];
        //not a file, this is an error, handle it!
    }


    return YES;
}

What should I add/change ??

Set your rootViewController programmatically by checking login status.

You can save your login status as a bool in NSUserDefault after Login & Logout.

After Login

[[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"LoginStatus"];

After Logout

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

Modify your appDelegate - didFinishLaunchingWithOptions: delegate method as follows

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

    //Check Login status
    if (![[NSUserDefaults standardUserDefaults] boolForKey:@"LoginStatus"])
    {
      // user not logged in
      LOGIN_VIEWCONTROLLER *rootviewcontroller=[storyboard instantiateViewControllerWithIdentifier:LOGIN_VIEWCONTROLLER_IDENTIFIER];
   }
   else
   {
      // user already logged in
      TABBARCONTROLLER *rootviewcontroller=[storyboard instantiateViewControllerWithIdentifier:TABBARCONTROLLER_IDENTIFIER];
   }
   self.window.rootViewController = rootviewcontroller;
   [self.window makeKeyAndVisible];

   return YES;
  }

Two Options:

1) You will need database (core data) to keep track of users log. It also may help you for offline login. You can create and set property 'isLoggedIn' to find last logged in user.

2) Create and save logged-in user details in file. Refer this file for next start up. If user is logged-out , then delete that file.

Update 1: Create login View, push tabBar on succesful login , followed by navigation controller for further flow. Refer image : 故事板

Fixed it,

I actually changed the rootviewcontroller by adding this line of code in appdelegate

LoginViewController *controller = (LoginViewController *)self.window.rootViewController;

I also changed the entry of the application in my storyboard to be loginviewcontroller.

And I also added a method called prepareforSegue in my loginviewcontroller to pass as a parameter the managed objectcontext because it caused some errors in the beginning:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{


    if ([[segue identifier] isEqualToString:@"toHome"]) {
        TabsViewController_iPhone *tabsViewController;
        tabsViewController=[segue destinationViewController];
        tabsViewController.managedObjectContext=self.managedObjectContext;

    }

}

Hope to have helped someone out there. If you need more explanations on how I fixed the problem I am available. Thank's for everyone.

Edit:

After taking some time to understand how objective C works, I came to the conclusion that my answer above was not complete, so this is actually what happend:

After the user logs in the first time, I store his user & password in NSUserDefaults . When he logs out I set the NSUserDefaults user/pass variables to Null.

So now I added a test to the AppdDelegate' didFinishLaunchingWithOptions method that says:

if the user/pass variables on NSUserDefaults are set -> go to the main menu screen of the app

else -> go to the login screen of the app.

this is the code:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

    NSString *username = [defaults stringForKey:@"username"];
    NSString *password = [defaults stringForKey:@"password"];

    if (username != nil && password != nil)
        {

            self.window = [[UIWindow alloc] initWithFrame:UIScreen.mainScreen.bounds];
            UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MyStoryboard" bundle:nil];
            // determine the initial view controller here and instantiate it with
            UIViewController *viewController =  [storyboard instantiateViewControllerWithIdentifier:@"HEQMainMenuView"];

            self.window.rootViewController = viewController;
            [self.window makeKeyAndVisible];
        }
    else
        {

            self.window = [[UIWindow alloc] initWithFrame:UIScreen.mainScreen.bounds];
            UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MyStoryboard" bundle:nil];
            // determine the initial view controller here and instantiate it with
            UIViewController *viewController =  [storyboard instantiateViewControllerWithIdentifier:@"HEQLoginView"];

            self.window.rootViewController = viewController;
            //viewController.man
            [self.window makeKeyAndVisible];
        }
}

Hope this helps :D

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM