从登录重定向

时间:2014-02-10 16:26:47

标签: ios

如果有人在这里可以帮助我,我会非常感激,我遇到了一个问题,我需要登录我的应用程序,然后自动重定向到下一页,这就是它的全部。

提前谢谢

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    credentialsDictionary = [[NSDictionary alloc] initWithObjects:[NSArray     arrayWithObjects:@"password", @"1234", nil] forKeys:[NSArray arrayWithObjects:@"username",@"amit", nil]];
    // Do any additional setup after loading the view, typically from a nib.
    [_webview1 loadRequest:[NSURLRequest requestWithURL: [NSURL URLWithString:@"http://nutriments.info/test1/test3.php"]]];
}
- (IBAction)dismiss:(id)sender {
    [sender resignFirstResponder];
}
- (IBAction)dismiss1:(id)sender {
    [sender resignFirstResponder];
}
- (IBAction)dismiss2:(id)sender {
    [sender resignFirstResponder];
}
- (IBAction)dismiss3:(id)sender {
    [sender resignFirstResponder];
}
- (IBAction)dismiss4:(id)sender {
    [sender resignFirstResponder];
}

- (IBAction)enterCredentials {
    if ([[credentialsDictionary objectForKey:usernameField.text]isEqualToString:passwordField.text]) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Correct Password" message:@"This   password is correct." delegate:self cancelButtonTitle:@"Dismiss" otherButtonTitles:nil]
        [alert show];
    }
    else {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Incorrect Password" message:@"This     password is incorrect." delegate:self cancelButtonTitle:@"Dismiss" otherButtonTitles:nil];
        [alert show];
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (IBAction)sendtoadmin:(id)sender {
    MFMailComposeViewController *mailContoller = [[MFMailComposeViewController alloc]init];
    [mailContoller setMailComposeDelegate:self];
    NSString *email = @"******@hotmail.com";
    NSArray *emailArray = [[NSArray alloc]initWithObjects:email, nil];
    NSString *message = [@[_textview.text, _textview1.text,     _textview2.text]componentsJoinedByString: @"\n"];
    [mailContoller setMessageBody:message isHTML:NO];
    [mailContoller setToRecipients:emailArray];
    [mailContoller setSubject:@"Query"];
    [self presentViewController:mailContoller animated:YES completion:nil];
}

-(void)touchesBegan4:(NSSet *)touches withEvent:(UIEvent *)event {
    [[self textview] resignFirstResponder];
}

@end

2 个答案:

答案 0 :(得分:1)

您可以使用pushViewController:animated:completion:上的self.navigationController来推送视图。 如果您在enterCredentials函数的“成功”部分中使用此方法,则可以自动重定向视图。 E.G:

- (IBAction)enterCredentials {
    if ([[credentialsDictionary objectForKey:usernameField.text]isEqualToString:passwordField.text])
    {
        InsertNextViewControllerClassHere *nextViewController = [[InsertNextViewControllerClassHere alloc] init];
        [self.navigationController pushViewController:nextViewController animated:YES completion: nil];
    }
    else {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Incorrect Password" message:@"This     password is incorrect." delegate:self cancelButtonTitle:@"Dismiss" otherButtonTitles:nil];
        [alert show];
    }
}

您可能还希望在NSUserDefaults中保存变量is_logged_in。 这可以通过以下代码完成:

NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setBool:YES forKey:@"is_logged_in"];
[userDefaults synchronize];

然后,创建一个新方法- viewWillAppear,它将检查你刚刚设置的布尔值:

NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
if ([userDefaults boolForKey:@"is_logged_in"])
{
    InsertNextViewControllerClassHere *nextViewController = [[InsertNextViewControllerClassHere alloc] init];
    [self.navigationController pushViewController:nextViewController animated:YES completion: nil];
}

请注意,将所有密码保存在设备上是不安全的。 (未加密,似乎) 您可能希望将此部分移动到服务器,在发送之前加密密码。

答案 1 :(得分:0)

使用

显示登录视图
UIViewController myLoginViewController = [[MyLoginViewController alloc] initwithNibNamed:"MyLoginViewController"]; 
[myTabViewController presentModalViewController:myLoginViewController animated:YES];

隐藏它使用

//This should be done from the original View Controller i.e. myTabViewController preferably in a delegate called by the modal view controller.
[self dismissModalViewControllerAnimated:YES];