alacarter
alacarter

Reputation: 359

NSFileManager user/permission change on lock screen with password

When a password-protected iPhone is locked and apps are running in the background, do they run as a different user?

The following code works in the simulator all of the time, and on physical devices when the screen is not locked. When the screen is locked, it gives error 13: operation not permitted

 if (![[NSFileManager defaultManager] createDirectoryAtPath:dataPath withIntermediateDirectories:NO attributes:nil error:&error]) {
            NSLog(@"Could not create data directory: %@", [error localizedDescription]);
            NSLog(@"error: %@", [error description]);
    } else {
        NSLog(@"\n\n----Created OpenSenseData Directory----");
    }
}

The code is called as a result of CLLocationManager receiving a location update.

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations

The likely explanation is that apps run as a different user when in the background. If that's the case, is there a way to change the app permissions to allow file or directory creation when the screen is locked?

Upvotes: 0

Views: 846

Answers (1)

alacarter
alacarter

Reputation: 359

This is an issue with the data permissions and is solvable by removing NSFileDataProtection from the parent directory. The initial parent directory has to be created while the application is in the foreground, and this does remove encryption from the file in question

NSDictionary *protection = [NSDictionary dictionaryWithObject:NSFileProtectionNone forKey:NSFileProtectionKey];
[[NSFileManager defaultManager] setAttributes:protection ofItemAtPath:parentDirectoryPath error:nil];
[[NSFileManager defaultManager] createDirectoryAtPath:dataPath withIntermediateDirectories:NO attributes:nil error:&error];

Upvotes: 2

Related Questions