简体   繁体   English

登录后的iOS App切换视图

[英]IOS App switch views after login

I'm learning to develop for IOS in Xcode 5. The app uses a master-detail template which it populates from a simple API i've written in PHP. 我正在学习使用Xcode 5为IOS开发。该应用程序使用一个主从模板,该模板由我用PHP编写的简单API填充。

I want users to log into the app so that the app can make requests to the API on behalf of the user. 我希望用户登录到应用程序,以便该应用程序可以代表用户向API发出请求。 I'll explain what i've set up (which could be entirely wrong): 我将解释我的设置(这可能是完全错误的):

I've created a login view and made it the initial view. 我创建了一个登录视图并将其设置为初始视图。 My login view is controlled by a class named LoginViewController which contains a 'logUserIn' method. 我的登录视图由名为LoginViewController的类控制,该类包含一个'logUserIn'方法。 When the 'Login' button in the view is pressed this method sends a post request to the API to check the user's credentials, my app then reads the response to see if they are valid. 当按下视图中的“登录”按钮时,此方法向API发送发布请求以检查用户的凭据,然后我的应用读取响应以查看其是否有效。 This is where i'm stuck. 这就是我卡住的地方。

After i've determined I have a valid set of credentials I want to do 2 things: 确定我有一组有效的凭据后,我想做两件事:

  1. Save the credentials for use on subsequent requests 保存凭据以供后续请求使用
  2. Switch from the login view back to the master-detail view 从登录视图切换回主从视图

If when the app loads there are already valid credentials saved the loginViewController should switch straight to the master-detail view. 如果在加载应用程序时已经保存了有效的凭据,则loginViewController应该直接切换到主从视图。

Here's my LoginViewController.m: 这是我的LoginViewController.m:

#import "NTFYLoginViewController.h"

@interface NTFYLoginViewController ()

@end

@implementation NTFYLoginViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Attempt to read saved credentials
    // Check they still work
    // Switch to master-detail view if credentials exist and are valid
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)logUserIn:(id)sender
{
    NSString *username = self.usernameField.text;
    NSString *password = self.passwordField.text;

    // The code here talks to the api and checks the credentials, 
    // i've swapped it for pseudo code below as the actual code 
    // is irrelevant

    if(user is logged in)
    { 
        // Save credentials
        // Switch to master-detail view
    }

    // Display login error    
}

@end

So, here is what I actually want to know: 所以,这是我真正想知道的:

  1. Is this the best way to go about doing this? 这是执行此操作的最佳方法吗?
  2. How can I switch to the MasterViewController from my LoginViewController? 如何从LoginViewController切换到MasterViewController?

Use the KeyChain services to save the login credentials, as well as other sensitive information that you do not want to be easily accessible by an attacker. 使用KeyChain服务来保存登录凭据以及您不希望攻击者轻松访问的其他敏感信息。

To go back to your master view controller, there are several approaches you can take. 回到主视图控制器,可以采用几种方法。 The easiest would be to display the login view controller modally on top of the master when the app launches (or when it determines the user needs to enter credentials). 最简单的方法是在应用启动时(或在确定用户需要输入凭据时)将登录视图控制器以模态方式显示在主视图上。 Once the user enters the credentials and you determine the user is authenticated, dismiss the login view controller back to the master view controller. 用户输入凭据后,如果您确定用户已通过身份验证,则将登录视图控制器退回主视图控制器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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