我正在编写一个iOS应用程序,我需要让用户登录。登录将需要向Web服务发出JSON请求,然后将用户详细信息存储在Core Data中或任何最佳位置。
此外,我需要在主应用程序启动之前出现登录模式,我知道为此,我在- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
中调用它。
那么,我需要一个简单的登录,用户名和密码字段,发出请求,然后将生成的JSON数据存储到某个地方,也许是核心数据?
我搜索了一个登录示例的高低,但它们都非常简陋或者没有做我想要的。我正在考虑创建一个xib文件然后调用它,但我不确定这一切。
答案 0 :(得分:1)
只需将didFinishLaunchingWithOptions
模式推送到您的LoginViewController
到导航堆栈即可。如果您正在使用故事板,则可以将LoginViewController
设为根视图控制器,或者设置一个segue,为其指定一个标识符,然后只需调用[rootViewController performSegueWithIdentifier:@"YourSegueId" sender:self]
。
答案 1 :(得分:1)
我几天前想知道同样的问题
这是我的解决方案:
在didFinishLaunchingWithOptions
中,我调用一个方法检查数据库中是否有帐户和密码(核心数据?我只是使用userdefault)。
如果有,请尝试登录,如果登录失败,则显示模态视图。如果成功,请将您的appdelegate.window.rootviewcontroller设置为主视图控制器
如果没有,请显示模态视图。
或登录失败,blablabla ......
抱歉我的英语不好 这是我的代码:- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[[AFNetworkActivityIndicatorManager sharedManager] setEnabled:YES];
UINavigationController *nav = [[UINavigationController alloc] init];
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
self.window.rootViewController = nav;
[self.window makeKeyAndVisible];
[ZTCAPIClient registerUserInfo];
return YES;
}
+ (void) registerUserInfo {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *account = [defaults stringForKey:@"account"];
if(!account) {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
// load default value
[self performSelector:@selector(registerDefaultsFromSettingsBundle)];
dispatch_async(dispatch_get_main_queue(), ^{
ZTCUserSettingsViewController *userSettingsView = [[ZTCUserSettingsViewController alloc] init];
UINavigationController *usersSettingsNav = [[UINavigationController alloc] initWithRootViewController:userSettingsView];
[[[[[UIApplication sharedApplication] delegate] window] rootViewController] presentModalViewController:usersSettingsNav animated:NO];
[ZTCNotice showSuccessNoticeInView:userSettingsView.view title:[NSString stringWithFormat:@"%@,%@",NSLocalizedString(@"login first time use title", nil),NSLocalizedString(@"login first time use message", nil)]];//TODO
});
});
} else {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
if ([ZTCAPIClient loginWithAccount:[defaults stringForKey:@"account"] Password:[defaults stringForKey:@"password"] BaseURL:[defaults stringForKey:@"url"]]) {
//DLog(@"Log in SUCCESS");
dispatch_async(dispatch_get_main_queue(), ^{
UITableViewController *viewController = [[ZTCTaskListViewController alloc] initWithStyle:UITableViewStylePlain];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:viewController];
[[[[UIApplication sharedApplication] delegate] window] setRootViewController:nav];
});
} else {
//DLog(@"Log in FAIL");
dispatch_async(dispatch_get_main_queue(), ^{
ZTCUserSettingsViewController *userSettingsView = [[ZTCUserSettingsViewController alloc] init];
UINavigationController *usersSettingsNav = [[UINavigationController alloc] initWithRootViewController:userSettingsView];
[[[[[UIApplication sharedApplication] delegate] window] rootViewController] presentModalViewController:usersSettingsNav animated:NO];
[ZTCNotice showErrorNoticeInView:userSettingsView.view title:NSLocalizedString(@"login fail title", nil) message:NSLocalizedString(@"login fail message", nil)];
});
}
});
}
}