title: SwiftUI Biometric authentication as an optional feature in application to block the UI behind
body: <p>I am new to SwiftUI and I would like to use the Biometric authentication(Face ID, Touch ID) to lock the UI behind. I could do that as a mandatory check(using a flag isUnlocked). But I need to set it as a preference for user.User should be able to On/OFF the feature from a different tab say Settings.</p>
  <p>I used below code for authenticating user</p>
  <pre><code>    @State private var isUnlocked = false
  var body: some View {
      TabView{
          if isUnlocked{
              HomeView()
                  .tabItem {
                      Label(&quot;Home&quot;, systemImage: &quot;house&quot;)
                  }
              CategoryView()
                  .tabItem {
                      Label(&quot;Category&quot;, systemImage: &quot;list.bullet.rectangle.portrait&quot;)
                  }
              SettingsView()
                  .tabItem {
                      Label(&quot;Settings&quot;, systemImage: &quot;gearshape.2&quot;)
                  }
          }else{
              Button(&quot;Unlock&quot;){
                  authenticate()
              }
          }
      }
  }
  
  func authenticate(){
      let context = LAContext()
      var error : NSError?
      let reason = &quot;Please unlock the device to see information&quot;
      if context.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: &amp;error){
          context.evaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, localizedReason: reason) { success, error in
              if success{
                  isUnlocked = true
              }
          }
      }
  }
  </code></pre>
  <p>How can I achieve this</p>