Is it safe to access the contents of an app's bundle from within an app extension?

I've got some materials in an app's bundle (some info.plist values, and some images in .xcassets files etc.)

If I access them from within an app extension (a notification service extension, notification content extension for example), then it appears to work. However while running the extensions in the debugger, there were some messages in the console saying the app bundle wasn't loaded. So despite it working, this message made me wonder if its not a safe practice and it working was luck and/or timing etc. and if the materials should instead be duplicated within the extension bundle and obtained from there instead of accessing them from the app bundle?

Answered by DTS Engineer in 820888022

I would consider this to be safe.

However, my advice is that you put these assets into a framework. An appex accessing a framework from its container app is pretty fundamental to how appexes work, so I can’t imagine how it could ever break.

ps Note that the above is trickier for system extensions, because on macOS they get copied to a secure location. So if your app and sysex have to share a framework, you must embed that framework in the sysex, not the app. However, that’s not a concern for appexes and, critically, on iOS and its child platforms you can only place frameworks at the app level within your bundle. See Placing Content in a Bundle.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Here's a bit of specific code example, suppose an app extension wants to obtain the value of something in the info.plist which is in the app's bundle (rather than the extension's bundle).

Can it be guaranteed that the app's bundle is always accessible/loaded etc. when being accessed from the app extension?

    private class func getObjectFromInfoPlist(name: String)  -> Any? {
        var bundle = Bundle.main
        if bundle.bundleURL.pathExtension == "appex" { 
            let url = bundle.bundleURL.deletingLastPathComponent().deletingLastPathComponent()
            if let otherBundle = Bundle(url: url) {
                bundle = otherBundle
            }
        }
       return bundle.object(forInfoDictionaryKey: name)
    }
Accepted Answer

I would consider this to be safe.

However, my advice is that you put these assets into a framework. An appex accessing a framework from its container app is pretty fundamental to how appexes work, so I can’t imagine how it could ever break.

ps Note that the above is trickier for system extensions, because on macOS they get copied to a secure location. So if your app and sysex have to share a framework, you must embed that framework in the sysex, not the app. However, that’s not a concern for appexes and, critically, on iOS and its child platforms you can only place frameworks at the app level within your bundle. See Placing Content in a Bundle.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Is it safe to access the contents of an app's bundle from within an app extension?
 
 
Q