Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

File security when device is locked and background fetch in iOS7

I am using file security using NSFileProtectionKey attribute like this:

[[NSFileManager defaultManager] setAttributes:@{NSFileProtectionKey : NSFileProtectionComplete} ofItemAtPath:coreDataFilePath error:NULL];

So, no file in that path can be opened when device is passcode locked. The problem is that from iOS7 Apps can run in the background even while device is locked. When running like that Coredata [NSManagedObjectContext save:] will fail with error:

NSCocoaErrorDomain, 134030, The operation couldn’t be completed. (Cocoa error 134030.) { NSSQLiteErrorDomain = 23; NSUnderlyingException = "Updating max pk failed: authorization denied"; }

How to handle such case?

like image 362
8suhas Avatar asked Sep 23 '13 13:09

8suhas


Video Answer


1 Answers

First, see Session 204 "What's New with Multitasking" from the WWDC 2013 videos. It covers this case.

There are several approaches you can take, in order of security preference:

  • Write the data to another location (using NSFileProtectionCompleteUnlessOpen) until the device is unlocked, then merge. This is generally the preferred approach.
  • Set your main database to NSFileProtectionCompleteUnlessOpen and keep it open.
  • Set your database to NSFileProtectionCompleteUntilFirstUserAuthentication. This is generally the worst approach.
like image 195
Rob Napier Avatar answered Oct 20 '22 11:10

Rob Napier