- 首页 > 睿知 > it技术 > >
代码片段-需要登录的控制器代码实现
// 数组中为需要登录的控制器
private static let viewControllerClassesThatRequireUserSession: [AnyObject.Type] = [FavoritesCollectionViewController.self, CartViewController.self, AccountViewController.self]func tabBarController(tabBarController: UITabBarController, shouldSelectViewController viewController: UIViewController) -> Bool {
// 如果用户已经登录返回true
guard !AccountManager.defaultAccountManager.isUserLoggedIn else {
return true
}let visibleController: UIViewController// 判断将要跳转的控制器,是否需要登录
if let navigationController = viewController as? UINavigationController {
visibleController = navigationController.topViewController ?? viewController
} else {
visibleController = viewController
}let shouldPresentSignInScreen = AppDelegate.viewControllerClassesThatRequireUserSession.contains { $0 == visibleController.dynamicType }if shouldPresentSignInScreen {
SignInViewController.presentSignInViewController() { success in
if success {
// 登录成功,跳转到之前选择的控制器
self.tabBarController.selectedViewController = viewController
}
}return false
}return true
}
推荐阅读