iOS, using biometric authentication only for a subset of an apps functionality












0














I'm working on an app where the user needs to be authenticated for only some of the features, but they are supposed to remain 'anonymous' for the rest of the features. Is there a way to implement biometric authentication within the app when the user tries to access specific features?



For instance, let us say that to participate in an open discussion forum within the app they do not need to authenticate, but once they try to send a direct message they will need to authenticate using Touch ID or Face ID. Can this be done, or can biometric authentication only be used when the user opens the app?










share|improve this question



























    0














    I'm working on an app where the user needs to be authenticated for only some of the features, but they are supposed to remain 'anonymous' for the rest of the features. Is there a way to implement biometric authentication within the app when the user tries to access specific features?



    For instance, let us say that to participate in an open discussion forum within the app they do not need to authenticate, but once they try to send a direct message they will need to authenticate using Touch ID or Face ID. Can this be done, or can biometric authentication only be used when the user opens the app?










    share|improve this question

























      0












      0








      0







      I'm working on an app where the user needs to be authenticated for only some of the features, but they are supposed to remain 'anonymous' for the rest of the features. Is there a way to implement biometric authentication within the app when the user tries to access specific features?



      For instance, let us say that to participate in an open discussion forum within the app they do not need to authenticate, but once they try to send a direct message they will need to authenticate using Touch ID or Face ID. Can this be done, or can biometric authentication only be used when the user opens the app?










      share|improve this question













      I'm working on an app where the user needs to be authenticated for only some of the features, but they are supposed to remain 'anonymous' for the rest of the features. Is there a way to implement biometric authentication within the app when the user tries to access specific features?



      For instance, let us say that to participate in an open discussion forum within the app they do not need to authenticate, but once they try to send a direct message they will need to authenticate using Touch ID or Face ID. Can this be done, or can biometric authentication only be used when the user opens the app?







      ios swift






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 20 '18 at 9:57









      Sjur Sundin

      334




      334
























          2 Answers
          2






          active

          oldest

          votes


















          3














          Yes, you can use it anywhere you want to. Just ask for authentication and then, after authentication is successfully completed, show another ViewController.






          share|improve this answer





























            0














            you can do biometric auth anywhere you want, Here is a sample that you can try:



            func authenticationWithTouchID(completion: (Bool) -> ()) {
            let localAuthenticationContext = LAContext()
            localAuthenticationContext.localizedFallbackTitle = "Use Passcode"

            var authError: NSError?
            let reasonString = "To access the secure data"

            if localAuthenticationContext.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: &authError) {

            localAuthenticationContext.evaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, localizedReason: reasonString) { success, evaluateError in
            if success {
            completion(true)
            } else {
            //TODO: User did not authenticate successfully, look at the error and take appropriate action
            guard let error = evaluateError else { return }
            print(self.evaluateAuthenticationPolicyMessageForLA(errorCode: error._code))
            //TODO: If you have choosen the 'Fallback authentication mechanism selected' (LAError.userFallback). Handle gracefully
            completion(false)
            }
            }
            } else {
            guard let error = authError else { return }
            //TODO: Show appropriate alert if biometry/TouchID/FaceID is lockout or not enrolled
            print(self.evaluateAuthenticationPolicyMessageForLA(errorCode: error.code))
            }
            }

            func evaluatePolicyFailErrorMessageForLA(errorCode: Int) -> String {
            var message = ""
            if #available(iOS 11.0, macOS 10.13, *) {
            switch errorCode {
            case LAError.biometryNotAvailable.rawValue:
            message = "Authentication could not start because the device does not support biometric authentication."

            case LAError.biometryLockout.rawValue:
            message = "Authentication could not continue because the user has been locked out of biometric authentication, due to failing authentication too many times."

            case LAError.biometryNotEnrolled.rawValue:
            message = "Authentication could not start because the user has not enrolled in biometric authentication."

            default:
            message = "Did not find error code on LAError object"
            }
            } else {
            switch errorCode {
            case LAError.touchIDLockout.rawValue:
            message = "Too many failed attempts."

            case LAError.touchIDNotAvailable.rawValue:
            message = "TouchID is not available on the device"

            case LAError.touchIDNotEnrolled.rawValue:
            message = "TouchID is not enrolled on the device"

            default:
            message = "Did not find error code on LAError object"
            }
            }
            return message;
            }

            func evaluateAuthenticationPolicyMessageForLA(errorCode: Int) -> String {
            var message = ""
            switch errorCode {

            case LAError.authenticationFailed.rawValue:
            message = "The user failed to provide valid credentials"

            case LAError.appCancel.rawValue:
            message = "Authentication was cancelled by application"

            case LAError.invalidContext.rawValue:
            message = "The context is invalid"

            case LAError.notInteractive.rawValue:
            message = "Not interactive"

            case LAError.passcodeNotSet.rawValue:
            message = "Passcode is not set on the device"

            case LAError.systemCancel.rawValue:
            message = "Authentication was cancelled by the system"

            case LAError.userCancel.rawValue:
            message = "The user did cancel"

            case LAError.userFallback.rawValue:
            message = "The user chose to use the fallback"

            default:
            message = evaluatePolicyFailErrorMessageForLA(errorCode: errorCode)
            }
            return message
            }


            Now You can use this function anywhere you want



            authenticationWithTouchID(completion: {(success) in
            if success {
            //TODO: User authenticated successfully, take appropriate action
            } else {
            //TODO: User authenticateion failed, take appropriate action
            }
            })





            share|improve this answer





















              Your Answer






              StackExchange.ifUsing("editor", function () {
              StackExchange.using("externalEditor", function () {
              StackExchange.using("snippets", function () {
              StackExchange.snippets.init();
              });
              });
              }, "code-snippets");

              StackExchange.ready(function() {
              var channelOptions = {
              tags: "".split(" "),
              id: "1"
              };
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function() {
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled) {
              StackExchange.using("snippets", function() {
              createEditor();
              });
              }
              else {
              createEditor();
              }
              });

              function createEditor() {
              StackExchange.prepareEditor({
              heartbeatType: 'answer',
              autoActivateHeartbeat: false,
              convertImagesToLinks: true,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: 10,
              bindNavPrevention: true,
              postfix: "",
              imageUploader: {
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              },
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              });


              }
              });














              draft saved

              draft discarded


















              StackExchange.ready(
              function () {
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53390411%2fios-using-biometric-authentication-only-for-a-subset-of-an-apps-functionality%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              2 Answers
              2






              active

              oldest

              votes








              2 Answers
              2






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              3














              Yes, you can use it anywhere you want to. Just ask for authentication and then, after authentication is successfully completed, show another ViewController.






              share|improve this answer


























                3














                Yes, you can use it anywhere you want to. Just ask for authentication and then, after authentication is successfully completed, show another ViewController.






                share|improve this answer
























                  3












                  3








                  3






                  Yes, you can use it anywhere you want to. Just ask for authentication and then, after authentication is successfully completed, show another ViewController.






                  share|improve this answer












                  Yes, you can use it anywhere you want to. Just ask for authentication and then, after authentication is successfully completed, show another ViewController.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 20 '18 at 10:04









                  Robert Dresler

                  4,0601526




                  4,0601526

























                      0














                      you can do biometric auth anywhere you want, Here is a sample that you can try:



                      func authenticationWithTouchID(completion: (Bool) -> ()) {
                      let localAuthenticationContext = LAContext()
                      localAuthenticationContext.localizedFallbackTitle = "Use Passcode"

                      var authError: NSError?
                      let reasonString = "To access the secure data"

                      if localAuthenticationContext.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: &authError) {

                      localAuthenticationContext.evaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, localizedReason: reasonString) { success, evaluateError in
                      if success {
                      completion(true)
                      } else {
                      //TODO: User did not authenticate successfully, look at the error and take appropriate action
                      guard let error = evaluateError else { return }
                      print(self.evaluateAuthenticationPolicyMessageForLA(errorCode: error._code))
                      //TODO: If you have choosen the 'Fallback authentication mechanism selected' (LAError.userFallback). Handle gracefully
                      completion(false)
                      }
                      }
                      } else {
                      guard let error = authError else { return }
                      //TODO: Show appropriate alert if biometry/TouchID/FaceID is lockout or not enrolled
                      print(self.evaluateAuthenticationPolicyMessageForLA(errorCode: error.code))
                      }
                      }

                      func evaluatePolicyFailErrorMessageForLA(errorCode: Int) -> String {
                      var message = ""
                      if #available(iOS 11.0, macOS 10.13, *) {
                      switch errorCode {
                      case LAError.biometryNotAvailable.rawValue:
                      message = "Authentication could not start because the device does not support biometric authentication."

                      case LAError.biometryLockout.rawValue:
                      message = "Authentication could not continue because the user has been locked out of biometric authentication, due to failing authentication too many times."

                      case LAError.biometryNotEnrolled.rawValue:
                      message = "Authentication could not start because the user has not enrolled in biometric authentication."

                      default:
                      message = "Did not find error code on LAError object"
                      }
                      } else {
                      switch errorCode {
                      case LAError.touchIDLockout.rawValue:
                      message = "Too many failed attempts."

                      case LAError.touchIDNotAvailable.rawValue:
                      message = "TouchID is not available on the device"

                      case LAError.touchIDNotEnrolled.rawValue:
                      message = "TouchID is not enrolled on the device"

                      default:
                      message = "Did not find error code on LAError object"
                      }
                      }
                      return message;
                      }

                      func evaluateAuthenticationPolicyMessageForLA(errorCode: Int) -> String {
                      var message = ""
                      switch errorCode {

                      case LAError.authenticationFailed.rawValue:
                      message = "The user failed to provide valid credentials"

                      case LAError.appCancel.rawValue:
                      message = "Authentication was cancelled by application"

                      case LAError.invalidContext.rawValue:
                      message = "The context is invalid"

                      case LAError.notInteractive.rawValue:
                      message = "Not interactive"

                      case LAError.passcodeNotSet.rawValue:
                      message = "Passcode is not set on the device"

                      case LAError.systemCancel.rawValue:
                      message = "Authentication was cancelled by the system"

                      case LAError.userCancel.rawValue:
                      message = "The user did cancel"

                      case LAError.userFallback.rawValue:
                      message = "The user chose to use the fallback"

                      default:
                      message = evaluatePolicyFailErrorMessageForLA(errorCode: errorCode)
                      }
                      return message
                      }


                      Now You can use this function anywhere you want



                      authenticationWithTouchID(completion: {(success) in
                      if success {
                      //TODO: User authenticated successfully, take appropriate action
                      } else {
                      //TODO: User authenticateion failed, take appropriate action
                      }
                      })





                      share|improve this answer


























                        0














                        you can do biometric auth anywhere you want, Here is a sample that you can try:



                        func authenticationWithTouchID(completion: (Bool) -> ()) {
                        let localAuthenticationContext = LAContext()
                        localAuthenticationContext.localizedFallbackTitle = "Use Passcode"

                        var authError: NSError?
                        let reasonString = "To access the secure data"

                        if localAuthenticationContext.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: &authError) {

                        localAuthenticationContext.evaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, localizedReason: reasonString) { success, evaluateError in
                        if success {
                        completion(true)
                        } else {
                        //TODO: User did not authenticate successfully, look at the error and take appropriate action
                        guard let error = evaluateError else { return }
                        print(self.evaluateAuthenticationPolicyMessageForLA(errorCode: error._code))
                        //TODO: If you have choosen the 'Fallback authentication mechanism selected' (LAError.userFallback). Handle gracefully
                        completion(false)
                        }
                        }
                        } else {
                        guard let error = authError else { return }
                        //TODO: Show appropriate alert if biometry/TouchID/FaceID is lockout or not enrolled
                        print(self.evaluateAuthenticationPolicyMessageForLA(errorCode: error.code))
                        }
                        }

                        func evaluatePolicyFailErrorMessageForLA(errorCode: Int) -> String {
                        var message = ""
                        if #available(iOS 11.0, macOS 10.13, *) {
                        switch errorCode {
                        case LAError.biometryNotAvailable.rawValue:
                        message = "Authentication could not start because the device does not support biometric authentication."

                        case LAError.biometryLockout.rawValue:
                        message = "Authentication could not continue because the user has been locked out of biometric authentication, due to failing authentication too many times."

                        case LAError.biometryNotEnrolled.rawValue:
                        message = "Authentication could not start because the user has not enrolled in biometric authentication."

                        default:
                        message = "Did not find error code on LAError object"
                        }
                        } else {
                        switch errorCode {
                        case LAError.touchIDLockout.rawValue:
                        message = "Too many failed attempts."

                        case LAError.touchIDNotAvailable.rawValue:
                        message = "TouchID is not available on the device"

                        case LAError.touchIDNotEnrolled.rawValue:
                        message = "TouchID is not enrolled on the device"

                        default:
                        message = "Did not find error code on LAError object"
                        }
                        }
                        return message;
                        }

                        func evaluateAuthenticationPolicyMessageForLA(errorCode: Int) -> String {
                        var message = ""
                        switch errorCode {

                        case LAError.authenticationFailed.rawValue:
                        message = "The user failed to provide valid credentials"

                        case LAError.appCancel.rawValue:
                        message = "Authentication was cancelled by application"

                        case LAError.invalidContext.rawValue:
                        message = "The context is invalid"

                        case LAError.notInteractive.rawValue:
                        message = "Not interactive"

                        case LAError.passcodeNotSet.rawValue:
                        message = "Passcode is not set on the device"

                        case LAError.systemCancel.rawValue:
                        message = "Authentication was cancelled by the system"

                        case LAError.userCancel.rawValue:
                        message = "The user did cancel"

                        case LAError.userFallback.rawValue:
                        message = "The user chose to use the fallback"

                        default:
                        message = evaluatePolicyFailErrorMessageForLA(errorCode: errorCode)
                        }
                        return message
                        }


                        Now You can use this function anywhere you want



                        authenticationWithTouchID(completion: {(success) in
                        if success {
                        //TODO: User authenticated successfully, take appropriate action
                        } else {
                        //TODO: User authenticateion failed, take appropriate action
                        }
                        })





                        share|improve this answer
























                          0












                          0








                          0






                          you can do biometric auth anywhere you want, Here is a sample that you can try:



                          func authenticationWithTouchID(completion: (Bool) -> ()) {
                          let localAuthenticationContext = LAContext()
                          localAuthenticationContext.localizedFallbackTitle = "Use Passcode"

                          var authError: NSError?
                          let reasonString = "To access the secure data"

                          if localAuthenticationContext.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: &authError) {

                          localAuthenticationContext.evaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, localizedReason: reasonString) { success, evaluateError in
                          if success {
                          completion(true)
                          } else {
                          //TODO: User did not authenticate successfully, look at the error and take appropriate action
                          guard let error = evaluateError else { return }
                          print(self.evaluateAuthenticationPolicyMessageForLA(errorCode: error._code))
                          //TODO: If you have choosen the 'Fallback authentication mechanism selected' (LAError.userFallback). Handle gracefully
                          completion(false)
                          }
                          }
                          } else {
                          guard let error = authError else { return }
                          //TODO: Show appropriate alert if biometry/TouchID/FaceID is lockout or not enrolled
                          print(self.evaluateAuthenticationPolicyMessageForLA(errorCode: error.code))
                          }
                          }

                          func evaluatePolicyFailErrorMessageForLA(errorCode: Int) -> String {
                          var message = ""
                          if #available(iOS 11.0, macOS 10.13, *) {
                          switch errorCode {
                          case LAError.biometryNotAvailable.rawValue:
                          message = "Authentication could not start because the device does not support biometric authentication."

                          case LAError.biometryLockout.rawValue:
                          message = "Authentication could not continue because the user has been locked out of biometric authentication, due to failing authentication too many times."

                          case LAError.biometryNotEnrolled.rawValue:
                          message = "Authentication could not start because the user has not enrolled in biometric authentication."

                          default:
                          message = "Did not find error code on LAError object"
                          }
                          } else {
                          switch errorCode {
                          case LAError.touchIDLockout.rawValue:
                          message = "Too many failed attempts."

                          case LAError.touchIDNotAvailable.rawValue:
                          message = "TouchID is not available on the device"

                          case LAError.touchIDNotEnrolled.rawValue:
                          message = "TouchID is not enrolled on the device"

                          default:
                          message = "Did not find error code on LAError object"
                          }
                          }
                          return message;
                          }

                          func evaluateAuthenticationPolicyMessageForLA(errorCode: Int) -> String {
                          var message = ""
                          switch errorCode {

                          case LAError.authenticationFailed.rawValue:
                          message = "The user failed to provide valid credentials"

                          case LAError.appCancel.rawValue:
                          message = "Authentication was cancelled by application"

                          case LAError.invalidContext.rawValue:
                          message = "The context is invalid"

                          case LAError.notInteractive.rawValue:
                          message = "Not interactive"

                          case LAError.passcodeNotSet.rawValue:
                          message = "Passcode is not set on the device"

                          case LAError.systemCancel.rawValue:
                          message = "Authentication was cancelled by the system"

                          case LAError.userCancel.rawValue:
                          message = "The user did cancel"

                          case LAError.userFallback.rawValue:
                          message = "The user chose to use the fallback"

                          default:
                          message = evaluatePolicyFailErrorMessageForLA(errorCode: errorCode)
                          }
                          return message
                          }


                          Now You can use this function anywhere you want



                          authenticationWithTouchID(completion: {(success) in
                          if success {
                          //TODO: User authenticated successfully, take appropriate action
                          } else {
                          //TODO: User authenticateion failed, take appropriate action
                          }
                          })





                          share|improve this answer












                          you can do biometric auth anywhere you want, Here is a sample that you can try:



                          func authenticationWithTouchID(completion: (Bool) -> ()) {
                          let localAuthenticationContext = LAContext()
                          localAuthenticationContext.localizedFallbackTitle = "Use Passcode"

                          var authError: NSError?
                          let reasonString = "To access the secure data"

                          if localAuthenticationContext.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: &authError) {

                          localAuthenticationContext.evaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, localizedReason: reasonString) { success, evaluateError in
                          if success {
                          completion(true)
                          } else {
                          //TODO: User did not authenticate successfully, look at the error and take appropriate action
                          guard let error = evaluateError else { return }
                          print(self.evaluateAuthenticationPolicyMessageForLA(errorCode: error._code))
                          //TODO: If you have choosen the 'Fallback authentication mechanism selected' (LAError.userFallback). Handle gracefully
                          completion(false)
                          }
                          }
                          } else {
                          guard let error = authError else { return }
                          //TODO: Show appropriate alert if biometry/TouchID/FaceID is lockout or not enrolled
                          print(self.evaluateAuthenticationPolicyMessageForLA(errorCode: error.code))
                          }
                          }

                          func evaluatePolicyFailErrorMessageForLA(errorCode: Int) -> String {
                          var message = ""
                          if #available(iOS 11.0, macOS 10.13, *) {
                          switch errorCode {
                          case LAError.biometryNotAvailable.rawValue:
                          message = "Authentication could not start because the device does not support biometric authentication."

                          case LAError.biometryLockout.rawValue:
                          message = "Authentication could not continue because the user has been locked out of biometric authentication, due to failing authentication too many times."

                          case LAError.biometryNotEnrolled.rawValue:
                          message = "Authentication could not start because the user has not enrolled in biometric authentication."

                          default:
                          message = "Did not find error code on LAError object"
                          }
                          } else {
                          switch errorCode {
                          case LAError.touchIDLockout.rawValue:
                          message = "Too many failed attempts."

                          case LAError.touchIDNotAvailable.rawValue:
                          message = "TouchID is not available on the device"

                          case LAError.touchIDNotEnrolled.rawValue:
                          message = "TouchID is not enrolled on the device"

                          default:
                          message = "Did not find error code on LAError object"
                          }
                          }
                          return message;
                          }

                          func evaluateAuthenticationPolicyMessageForLA(errorCode: Int) -> String {
                          var message = ""
                          switch errorCode {

                          case LAError.authenticationFailed.rawValue:
                          message = "The user failed to provide valid credentials"

                          case LAError.appCancel.rawValue:
                          message = "Authentication was cancelled by application"

                          case LAError.invalidContext.rawValue:
                          message = "The context is invalid"

                          case LAError.notInteractive.rawValue:
                          message = "Not interactive"

                          case LAError.passcodeNotSet.rawValue:
                          message = "Passcode is not set on the device"

                          case LAError.systemCancel.rawValue:
                          message = "Authentication was cancelled by the system"

                          case LAError.userCancel.rawValue:
                          message = "The user did cancel"

                          case LAError.userFallback.rawValue:
                          message = "The user chose to use the fallback"

                          default:
                          message = evaluatePolicyFailErrorMessageForLA(errorCode: errorCode)
                          }
                          return message
                          }


                          Now You can use this function anywhere you want



                          authenticationWithTouchID(completion: {(success) in
                          if success {
                          //TODO: User authenticated successfully, take appropriate action
                          } else {
                          //TODO: User authenticateion failed, take appropriate action
                          }
                          })






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Nov 20 '18 at 11:33









                          Shibili k.p

                          23819




                          23819






























                              draft saved

                              draft discarded




















































                              Thanks for contributing an answer to Stack Overflow!


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid



                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.


                              To learn more, see our tips on writing great answers.





                              Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                              Please pay close attention to the following guidance:


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid



                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.


                              To learn more, see our tips on writing great answers.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function () {
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53390411%2fios-using-biometric-authentication-only-for-a-subset-of-an-apps-functionality%23new-answer', 'question_page');
                              }
                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown







                              Popular posts from this blog

                              "Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

                              If I really need a card on my start hand, how many mulligans make sense? [duplicate]

                              Alcedinidae