Skip to content

Instantly share code, notes, and snippets.

@jazzycamel
Last active August 14, 2020 14:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jazzycamel/ddfa2252fdeb9f4ee4e1f1ba3c1a9a4a to your computer and use it in GitHub Desktop.
Save jazzycamel/ddfa2252fdeb9f4ee4e1f1ba3c1a9a4a to your computer and use it in GitHub Desktop.
Using iOS Face ID (and Touch ID) with Qt5
#include "biometrics.h"
#ifdef Q_OS_IOS
#include "localAuth-c-interface.h"
#endif
#include <QDebug>
Biometrics::Biometrics(QObject *parent) : QObject(parent)
{
}
void Biometrics::invokeBiometrics(){
#ifdef Q_OS_IOS
qDebug() << "Local Auth:" << localAuth();
#endif
}
#ifndef BIOMETRICS_H
#define BIOMETRICS_H
#include <QObject>
class Biometrics : public QObject
{
Q_OBJECT
public:
explicit Biometrics(QObject *parent = nullptr);
public slots:
void invokeBiometrics();
signals:
};
#endif // BIOMETRICS_H
#ifndef LOCALAUTHCINTERFACE_H
#define LOCALAUTHCINTERFACE_H
bool localAuth();
#endif // LOCALAUTHCINTERFACE_H
BOOL localAuth();
#import <LocalAuthentication/LocalAuthentication.h>
BOOL localAuth() {
LAContext *laContext=[[LAContext alloc] init];
NSError *authError=nil;
NSString *localisedReasonString=@"App would like to make logging in easier.";
__block BOOL result=false;
dispatch_semaphore_t sema=dispatch_semaphore_create(0);
if ([laContext canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&authError]){
[laContext evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics localizedReason:localisedReasonString reply:^(BOOL success, NSError *error){
result=success;
dispatch_semaphore_signal(sema);
}];
} else return false;
if(![NSThread isMainThread]){
dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
} else {
while(dispatch_semaphore_wait(sema, DISPATCH_TIME_NOW)){
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:0]];
}
}
return result;
}
...
ios {
OBJECTIVE_HEADERS += localAuth.h
OBJECTIVE_SOURCES += localAuth.mm
HEADERS += localAuth-c-interface.h
LIBS += -framework LocalAuthentication
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment