@@ -513,23 +513,29 @@ - (void)_setEntryCount:(NSInteger)entryCount {
513
513
// accessible and when you try to use that data. Sidestep this issue altogether
514
514
// by using NSFileProtectionNone
515
515
- (BOOL )canAccessEntryData {
516
- BOOL result = YES ;
517
- if ([_fileDataProtectionMode isEqualToString: NSFileProtectionComplete ]) {
518
- result = [[UIApplication sharedApplication ] isProtectedDataAvailable ];
519
- } else if ([_fileDataProtectionMode isEqualToString: NSFileProtectionCompleteUntilFirstUserAuthentication ]) {
520
- // For "complete until first auth", if we were previously able to access data, then we'll still be able to
521
- // access it. If we haven't yet been able to access data, we'll need to try until we are successful.
522
- if (_canAccessData == NO ) {
523
- if ([[UIApplication sharedApplication ] isProtectedDataAvailable ]) {
524
- // we are unlocked, so we're good to go.
525
- _canAccessData = YES ;
526
- } else {
527
- // we are locked, so try to access data.
528
- _canAccessData = [NSData dataWithContentsOfURL: [NSURL fileURLWithPath: _filePath] options: NSDataReadingMappedAlways error: NULL ] != nil ;
529
- }
530
- }
516
+ if ([_fileDataProtectionMode isEqualToString: NSFileProtectionNone ])
517
+ return YES ;
518
+
519
+ if ([_fileDataProtectionMode isEqualToString: NSFileProtectionCompleteUntilFirstUserAuthentication ] && _canAccessData)
520
+ return YES ;
521
+
522
+ // -[UIApplication isProtectedDataAvailable] checks whether the keybag is locked or not
523
+ UIApplication *application = [UIApplication performSelector: @selector (sharedApplication )];
524
+ if (application) {
525
+ _canAccessData = [application isProtectedDataAvailable ];
526
+ }
527
+
528
+ // We have to fallback to a direct check on the file if either:
529
+ // - The application doesn't exist (happens in some extensions)
530
+ // - The keybag is locked, but the file might still be accessible because the mode is "until first user authentication"
531
+ if (!application || (!_canAccessData && [_fileDataProtectionMode isEqualToString: NSFileProtectionCompleteUntilFirstUserAuthentication ])) {
532
+ int fd;
533
+ _canAccessData = ((fd = open ([_filePath fileSystemRepresentation ], O_RDONLY)) != -1 );
534
+ if (_canAccessData)
535
+ close (fd);
531
536
}
532
- return result;
537
+
538
+ return _canAccessData;
533
539
}
534
540
535
541
- (FICImageTableEntry *)_entryDataAtIndex : (NSInteger )index {
0 commit comments