Context
I have an app that uses the container for two things: store a realm database, and store a log.txt file with some debugging logs for myself.
The realm database path is set via Realm.Configuration.defaultConfiguration
The log.txt file path is set with FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0].appendingPathComponent("log.txt")
I already have released and distributed the version 1 to a few users. Given the complexity around signing an app in Xcode, the app was released and distributed unsigned. And now I'm trying to release the version 2 signed, but here's when I'm getting this weird issue.
All unsigned apps (version 1, and the unsigned version 2) are using as container the path ~/Library/Containers/com.dgrcode.{appName}/Data. The signed version 2, however, is using ~/ as the container. So for example
log.txt is located at:
unsinged apps: ~/Library/Containers/com.dgrcode.{appName}/Data/Documents/log.txt.
signed app: ~/Documents/log.txt
Realm's default.realm file is located at:
unsigned apps: ~/Library/Containers/com.dgrcode.{appName}/Data/Library/Application Support/default.realm
signed app: I haven't found it yet 😓
The obvious problem is that any user from version 1 that installs version 2 signed, will start using a new database instead of continuing using the existing database. And obviously having my app's data spread through their ~/ directory is far from ideal.
Here's what I get when I run codesign -v -d on the first version (everything between {} has been redacted for clarity:
Executable={/path/to/app}
Identifier=com.dgrcode.{appName}
Format=app bundle with Mach-O universal (x86_64 arm64)
CodeDirectory v=20500 size=56564 flags=0x10002(adhoc,runtime) hashes=1757+7 location=embedded
Signature=adhoc
Info.plist entries=29
TeamIdentifier=not set
Runtime Version=13.3.0
Sealed Resources version=2 rules=13 files=2
Internal requirements count=0 size=12
Where I think the most relevant part is the Signature=adhoc and TeamIdentifier=not set.
Now I archive a version 2 of the app. I make a copy and I sign only one of them using codesign -v --sign. I package them inside a .dmg (not sure if this affects anything, but just in case), and check the .app inside each of the .dmg after mounting them.
Here's the result of codesign -v -d for each one:
unsigned:
Executable={path/to/app}
Identifier=com.dgrcode.{appName}
Format=app bundle with Mach-O universal (x86_64 arm64)
CodeDirectory v=20400 size=57452 flags=0x2(adhoc) hashes=1785+7 location=embedded
Signature=adhoc
Info.plist entries=31
TeamIdentifier=not set
Sealed Resources version=2 rules=13 files=4
Internal requirements count=0 size=12
signed
Executable={path/to/app}
Identifier=com.dgrcode.{appName}
Format=app bundle with Mach-O universal (x86_64 arm64)
CodeDirectory v=20400 size=57335 flags=0x0(none) hashes=1785+3 location=embedded
Signature size=4798
Signed Time=13 Nov 2023 at 12:17:24
Info.plist entries=31
TeamIdentifier=2W564BCY7Z
Sealed Resources version=2 rules=13 files=4
Internal requirements count=1 size=188
The unsigned app works as expected. When I open the unsigned app, it continues using the realm database in the previous location ~/Library/Containers/com.dgrcode.{appName}/Data, and I can see the log.txt update its content.
The signed app, however, doesn't use the same database, and is no longer writing to the log.txt file at ~/Library/Containers/com.dgrcode.{appName}/Data, but it's writing at ~/Documents/log.txt instead. It does use a database, but I have no clue where it is.
Questions
How can I make the signed app use the path ~/Library/Containers/com.dgrcode.{appName}/Data/ as its container lcoation?
How can something like this happen just by signing the .app?