Search code examples
swiftxcodeprotocolscode-signingguard

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


I need to put an age requirement on the app i am currently building and im looking for opinions on what you guys think would be the most efficient way to add this into the code. Should i use a guard let on the sign up screen or protocol?


Solution

  • You probably have two options like first integrate some Socials like Facebook or Google Integration

    If you prefer not to use any social Logins then best way would come to allow user to select his/her birthday using PickerView.

    Once the birthdate is set from pickerView, simply use that date to calculate the Age of the user using following Code

    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;
    }
    

    And Use it like to restrict the User:

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

    Second Option Would be:

    Give a Checkbox kind of button in App, This would help the user to tick or Un-tick if his/her age is greater than your restricted age or not.

    Something like this:

    enter image description here

    Hope this helps.