M-Swift All articles
Swift Architecture

Your App Group Is a Trust Boundary, Not a Folder

M-Swift

App groups are the plumbing behind a lot of "it just works" behavior on iOS. A widget reads the same cache as the app. A share extension drops a file into a folder the main app already knows. A SwiftData store lives in one place and every target points at it. Convenient. Also a trust boundary that rarely gets a second look.

The problem isn't that app groups are unsafe. It's that they're broad. Every target that declares the group can read and write the whole container. There's no per-folder ACL inside it. If a widget only needs a summary, it can still open the database. If an extension only needs a draft, it can still read the cache. Most teams find this out during an incident, not a code review.

Here's the audit I'd run before the next release.

Map the container before you touch the code

Start with a spreadsheet, not a search. List every target: main app, widget extension, share extension, notification service, watch app, whatever ships. For each one, capture the app group identifiers in its entitlements, the provisioning profile that backs it, and the files it expects to find.

Then compare. Mismatches show up in boring places: Debug has the group, Release doesn't. The widget's entitlement was copied from a sample and still says group.com.example.widget. A new extension was added to the target list but never to the App Group capability. These are the failures that look like "the widget is broken" but are really "the widget never had access."

A small diagnostic type helps keep the path honest:

enum SharedContainer {
    static let identifier = "group.com.example.app"

    static var url: URL {
        guard let url = FileManager.default.containerURL(
            forSecurityApplicationGroupIdentifier: identifier
        ) else {
            preconditionFailure("Missing app group entitlement: \(identifier)")
        }
        return url
    }
}

In production, don't crash on a missing container. Log it, fall back to a local cache, and surface the problem in diagnostics. The point is to make the dependency visible.

Entitlements and file protection disagree quietly

Two targets can both list the same group and still behave differently. One reason is file protection. The app might write with .completeUntilFirstUserAuthentication, while an extension assumes it can read before first unlock. Widgets and notification extensions often run at times the user isn't looking. If the file is protected with .complete, reads can fail after a reboot until the device is unlocked.

Decide per file, not per container. A shared cache can be .completeUntilFirstUserAuthentication. A draft the user expects to persist should be .complete. Write the choice down and test it on a real device: reboot, don't unlock, trigger the extension, and see what happens. The simulator will happily lie to you here.

Also check the Keychain. App groups share files; Keychain access groups share credentials. They are different entitlements. If a widget doesn't need a refresh token, don't give its target the keychain access group that contains one. Keep secrets in a narrower access group and let the shared container hold only non-sensitive state.

When one target writes a new schema

SwiftData in a shared container is where "convenient" turns into "who migrated first?" The app ships a new model version. The widget launches before the app after an update. It opens the old store with the new schema, or the app migrates while the widget is reading. Both are bad days.

Version the store and make migration a deliberate step. Every target that can open the store should know how to migrate it, or should refuse to open it until the app has. A simple version marker in the container can coordinate this:

struct SharedStore {
    static let schema = Schema([Item.self])

    static var configuration: ModelConfiguration {
        ModelConfiguration(
            "Shared",
            schema: schema,
            url: SharedContainer.url.appending(path: "shared.store")
        )
    }
}

Then test the ugly sequence: install the old build, create real data, upgrade, launch the widget first, then the app. Copy a production store into a test fixture and do it again. If the migration only works when the app launches first, you don't have a migration plan. You have a race.

Prove the side door is closed

You can't make an app group container read-only for one target. The OS doesn't work that way. What you can do is design so the widget and extensions never need the sensitive stuff. Put shared summaries in one file, app-only data in another, and keep credentials out of the container entirely.

Add a debug-only screen that lists the container path, the current schema version, and which target is asking. During QA, open the widget and confirm it reads only the summary. Try to make it read the app-only file and watch it fail. That's your negative test. If it succeeds, you've found a side door.

A short checklist

App groups are supposed to make sharing easy. That's exactly why they deserve an audit. The goal isn't to stop using them. It's to make sure every target that can open the door is a target you meant to give a key.

All Articles

Related Articles

Friendly Fire From Your Network Layer: Auditing Swift Retry Logic Before the Bill Arrives

Friendly Fire From Your Network Layer: Auditing Swift Retry Logic Before the Bill Arrives

Picking a Local Store You Won't Regret: SwiftData and GRDB for Offline-First Apps

Picking a Local Store You Won't Regret: SwiftData and GRDB for Offline-First Apps

The Red Dot Doesn't Lie — Except When It Does: Reconciling Notification Badges in Swift

The Red Dot Doesn't Lie — Except When It Does: Reconciling Notification Badges in Swift