Finding out if a device is locked from the notification widget

I would like to know if the device is locked when I load the Notification / Today widget, so I can show the widget correctly. (this is financial, and we don’t want to show balances on a locked phone)

On devices with TouchID, I can just try to access Keychain, and if I get

errSecInteractionNotAllowed

back, it is locked. Things are good. This does not work on devices without a touchID (but with a PIN). I found several things that recommend using

[[UIApplication sharedApplication] protectedDataAvailable]

However, I do not have [UIApplication sharedApplication]widgets.

Any ideas where and how to do this? I just need yes / no: the device is locked.

thank

[UPDATE: here is the code that I have]

Getting the file name:

+ (NSString *)lockedDeviceFilename {
    NSURL *directoryUrl = [[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:USER_DEFAULTS_GROUP_NAME];
   return [directoryUrl.path stringByAppendingPathComponent:@"security.dummy"];
}

/ ( , :

NSError *error = nil;

NSString *documentPath = [FOOStorageGatekeeper lockedDeviceFilename];

[[NSFileManager defaultManager] removeItemAtPath:documentPath error:&error];

BOOL created = [[NSFileManager defaultManager] createFileAtPath:documentPath
                                                       contents:[@"super secret file contents. we only care about the permissions" dataUsingEncoding:NSUTF8StringEncoding]
                                                     attributes:@{NSFileProtectionKey : NSFileProtectionComplete}];

:

 BOOL isReadable = [[NSFileManager defaultManager] fileExistsAtPath:[FOOStorageGatekeeper lockedDeviceFilename]];

  NSLog(@"isReadable? %@", isReadable ? @"YES" : @"NO");

, TouchID . , , NSFileProtectionKey NSFileProtectionComplete... : (

: .

+4
3

NSFileProtectionComplete , . , .

[[NSFileManager defaultManager] createFileAtPath:someFilePath
                                        contents:[@"Lock screen test." dataUsingEncoding:NSUTF8StringEncoding]
                                      attributes:@{NSFileProtectionKey: NSFileProtectionComplete}];

EDIT. . ( , .)

NSData *data = [NSData dataWithContentsOfURL:[FOOStorageGatekeeper lockedDeviceUrl] options: NSDataReadingMappedIfSafe error:&error];

if (error != nil && error.code == 257) {
    NSLog(@"**** the keychain appears to be locked, using the file method");
    return YES;
}

, errSecInteractionNotAllowed, , TouchID.

() ( , rego iOS dev)

+7

, 3-4 . , . : createFileAtPath,

NSData *data = [NSData dataWithContentsOfURL:[FOOStorageGatekeeper lockedDeviceUrl] options: NSDataReadingMappedIfSafe error:&error];

if (error != nil && error.code == 257) {
    NSLog(@"**** the keychain appears to be locked, using the file method");
    return YES;
}

, errSecInteractionNotAllowed, , TouchID.

() ( , rego iOS dev)

+2

, ( ).

: https://www.apple.com/business/docs/iOS_Security_Guide.pdf

It seems that the files are locked 10 seconds after locking the device.

Knowing that you can create files from extensions, and it seems to work.

+1
source

All Articles