B4i Library iOS - TouchID

Hi All,

This code will allow to use the TouchID (Fingerprint) in your app.
The example is on a button named Button with click event.

I was thinking to build a class for this but I think this is more comfortable to copy the source and modify the subs.


B4X:
#PlistExtra: <key>NSFaceIDUsageDescription</key<string>Uses face id to allow authentication</string>
Sub Button_Click
ShowTouchID(Me,"TouchOK", "TouchFail")
End Sub


Sub ShowTouchID(SubHandler As Object, ApprovedSub As String, FailSub As String)
Dim noMe As NativeObject=Me
noMe.RunMethod("TouchID:::",Array(SubHandler,ApprovedSub,FailSub))
End Sub

Sub TouchOK
Log("Touch Approved")
End Sub

Sub TouchFail(ErrorDescription As String)
Log(ErrorDescription)
End Sub

#If OBJC

#import <LocalAuthentication/LocalAuthentication.h>

-(void)TouchID :(NSObject*)handler :(NSString*) subnameok :(NSString*) subnamefail
{
LAContext *myContext = [[LAContext alloc] init];
NSError *authError = nil;
NSString *myLocalizedReasonString = @"Used for quick and secure access to the test app";
if ([myContext canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&authError]) {
    [myContext evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics
                  localizedReason:myLocalizedReasonString
                            reply:^(BOOL success, NSError *error) {
            if (success) {
                [self.__c CallSubDelayed:self.bi :handler :(subnameok)];
            } else {
                [self.__c CallSubDelayed2:self.bi :handler :(subnamefail) :(error.localizedDescription)];
            }
        }];
} else {
[self.__c CallSubDelayed2:self.bi :handler :(subnamefail) :(authError.localizedDescription)];

}
}


#End If


Ask if you have any questions

Narek
 
Last edited by a moderator:

leBasic

Member
Licensed User
Longtime User
Thanks Narek!
Is there a way to test if the device has a touch id sensor before calling the ShowTouchID sub? I think the sub will return an error, but it would be nice to test it without making a touch id request (for example to show or not an option in a settings panel)
 

narek adonts

Well-Known Member
Licensed User
Longtime User
maybe this

B4X:
Sub CheckTouchID As Boolean
return nome.runMethod("CheckTouchID",Null).AsBoolean
End Sub

#If OBJC

-(BOOL)CheckTouchID
{
LAContext *myContext = [[LAContext alloc] init];
NSError *authError = nil;

if ([myContext canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&authError]) {
    return YES;
}
else
{
return NO;

}
}

#End If

But This will return false also when passcode is not set.
 

schimanski

Well-Known Member
Licensed User
Longtime User
After 'Touch Approved", how do I prevent the TouchID-Window from showing again and again?
 

schimanski

Well-Known Member
Licensed User
Longtime User
Then i think it was my mistake. I call show in application_active. So it seems, that this sub is always called after closing the touchID-window. I will change the code tomorrow...
Thanks for your reply...
 

Krammig

Member
Licensed User
Longtime User
Hello Narek, Thank you for this contribution.

I am new to B4i so I was wondering if you have a small example complete project that uses this and shows how it all works ?

thanks
 

Mike1970

Well-Known Member
Licensed User
Longtime User
You should add this line (change the description as needed):

B4X:
#PlistExtra:<key>NSFaceIDUsageDescription</key><string>Uses face id to allow authentication</string>

Otherwise the app may crash on new devices.

When I put this Plis, the compiler shows this error:


B4X:
Build system information
error: Bundle identifier is missing. B4iProject doesn't have a bundle identifier for the Release build configuration. Add a value for PRODUCT_BUNDLE_IDENTIFIER in the build settings editor. (in target 'B4iProject')


Error: ** BUILD FAILED **
 
Last edited:
Top