使用 xcode 和 swift 为您的应用添加年龄限制的最佳方式是什么

what is the best way to add age restrictions to your app using xcode and swift

我需要对我目前正在构建的应用程序提出年龄要求,我正在寻找关于你们认为将其添加到代码中的最有效方法的意见。我应该在注册屏幕或协议上使用 guard let 吗?

你可能有两个选择,比如首先整合一些社交,比如 Facebook or Google Integration

如果您不想使用任何社交登录,那么最好的方法是允许用户使用 PickerView select his/her 生日。

一旦从 pickerView 设置了生日,只需使用该日期使用以下代码

计算用户的年龄
func findAgeFromBirthDate(_ birthDate:Date) ->Int {
    let now = Date();
    let calendar = Calendar.current;

    let ageComponents = calendar.dateComponents([.year], from: birthDate, to: now);
    let age = ageComponents.year!;
    print(age);
    return age;
}

并像限制用户一样使用它:

if age < 13{
  print("You are not allowed to use this")
}else{
  print("You are welcome")
}

第二个选项是:

在应用程序中提供一个复选框类型的按钮,这将帮助用户在 his/her 年龄是否大于您的限制年龄时勾选或取消勾选。

像这样:

希望对您有所帮助。