You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Swift/Xcode中如何根据条款接受状态切换启动ViewController?

实现条款接受状态判断的启动逻辑

嘿,这个需求在iOS应用里挺常见的,咱们一步步来搞定它:

1. 存储用户的条款接受状态

首先得找个地方存用户是否接受了条款,最方便的就是用UserDefaults——它专门用来存这种轻量的应用配置信息,代码也简单:
当用户在条款页面点击「接受」按钮时,执行下面的代码把状态存起来:

// 保存接受状态
UserDefaults.standard.set(true, forKey: "hasAcceptedTerms")
// 可选:确保立即同步到磁盘(一般不用,但极端场景下可以加)
UserDefaults.standard.synchronize()

2. 启动时判断并切换根视图控制器

接下来要在应用启动的入口点,根据存储的状态决定显示哪个页面。这里分两种常见情况:

情况一:使用AppDelegate(iOS 12及以下,或未启用Scene的项目)

在AppDelegate.swift的application(_:didFinishLaunchingWithOptions:)方法里添加判断逻辑:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    // 获取窗口实例
    let window = UIWindow(frame: UIScreen.main.bounds)
    self.window = window
    
    // 判断用户是否已接受条款
    let hasAccepted = UserDefaults.standard.bool(forKey: "hasAcceptedTerms")
    
    let rootViewController: UIViewController
    if hasAccepted {
        // 已接受,初始化欢迎页面
        rootViewController = WelcomeViewController()
    } else {
        // 未接受,初始化条款页面
        rootViewController = TermsAndConditionsViewController()
    }
    
    // 设置根VC并显示窗口
    window.rootViewController = rootViewController
    window.makeKeyAndVisible()
    
    return true
}

情况二:使用SceneDelegate(iOS 13+的标准配置)

在SceneDelegate.swift的scene(_:willConnectTo:options:)方法里处理:

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
    guard let windowScene = (scene as? UIWindowScene) else { return }
    
    let window = UIWindow(windowScene: windowScene)
    self.window = window
    
    let hasAccepted = UserDefaults.standard.bool(forKey: "hasAcceptedTerms")
    
    let rootViewController: UIViewController
    if hasAccepted {
        rootViewController = WelcomeViewController()
    } else {
        rootViewController = TermsAndConditionsViewController()
    }
    
    window.rootViewController = rootViewController
    window.makeKeyAndVisible()
}

3. 条款页面的「接受」按钮逻辑

在TermsAndConditionsViewController里,给「接受」按钮绑定点击事件,除了保存状态,还要切换到欢迎页面:

@IBAction func acceptButtonTapped(_ sender: UIButton) {
    // 保存接受状态
    UserDefaults.standard.set(true, forKey: "hasAcceptedTerms")
    
    // 切换到欢迎页面(替换根VC,避免用户返回条款页)
    let welcomeVC = WelcomeViewController()
    welcomeVC.modalPresentationStyle = .fullScreen
    self.present(welcomeVC, animated: true, completion: nil)
    
    // 或者如果你想用导航栈替换根VC(如果有导航控制器的话)
    // let navigationController = UINavigationController(rootViewController: welcomeVC)
    // UIApplication.shared.windows.first?.rootViewController = navigationController
}

注意点

  • 第一次启动应用时,UserDefaults里没有hasAcceptedTerms这个key,bool(forKey:)会默认返回false,正好显示条款页面,不用额外处理默认值。
  • 如果需要让用户可以重新查看条款(比如设置里加入口),只要在设置页面跳转到条款VC就行,不用修改启动逻辑。

内容的提问来源于stack exchange,提问作者Leon

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 10:37:13