几天前我也在想同样的问题。
这是我的解决方案:
在didFinishLaunchingWithOptions
,我调用一个方法检查您的数据库中是否有帐户和密码(核心数据?我只使用用户默认值)。
如果有,请尝试登录,如果登录失败,则显示模态视图。如果成功,请将您的 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)];
});
}
});
}
}