10

I am using extensions in my application. For sharing data between app and extension using app Groups. How Can I read App Group name programmatically so no need create extra configuration file for storing App Group name.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Sadasiba Sahoo
  • 129
  • 1
  • 6

3 Answers3

3

You'll only be able to use this in development (Debug mode), the entitlements are written into the embedded.mobileprovision file in the app bundle.

The embedded.mobileprovision will not be included when you archive an .ipa file or deliver your app to the AppStore.

When in Debug you can try this:

/* Swift 3 */

if let path = Bundle.main.path(forResource: "YourProjectName", ofType: "entitlements") {
    let dict = NSDictionary(contentsOfFile: path)
    let appGroups: NSArray = dict?.object(forKey: "com.apple.security.application-groups") as! NSArray
}
Oleh Zayats
  • 2,423
  • 1
  • 16
  • 26
  • 2
    So the app group name will have to be hard-coded for production? – SAHM May 03 '17 at 14:45
  • If you unzip you ipa file, you will get a file named 'embedded.mobileprovision'; the command `security cms -Di embedded.mobileprovision` will tell you the app group name. – DawnSong Feb 25 '19 at 11:27
1

The app group name typically is built with the main app's bundle identifier. So, if you're willing to make the assumption that you will prefix it with the standard "group." and perhaps suffix it with ".widget" (for the widget's bundle id) then you can obtain the app group name programmatically by using

var appGroupName: String { "group." + bundleIdentifier + ".widget" }

This post shows how to obtain the bundleIdentifier.

Phantom59
  • 947
  • 1
  • 8
  • 19
0

So, the correct answer is: It doesn’t work. Use a static class or constants for your app group names.

thomasgalliker
  • 1,279
  • 13
  • 19