91

iOS 8 revealed a new API yesterday concerning App Groups. It was kind of messy before to share data and communicate between apps and I believe that's precisely what App Groups is intended to correct.

In my app I have enabled App Groups and added a new group but I just can't find any documentation on how to use it. Documentation and API references only state how to add a group.

So what is App Groups really intended to do? Is there any documentation somewhere on how to use it?

TenaciousJay
  • 6,750
  • 3
  • 45
  • 48
streem
  • 9,044
  • 5
  • 30
  • 41

6 Answers6

86

Another benefit to App Groups is the ability to share a NSUserDefaults database. This also works for App Extensions (notification center widgets, custom keyboards, etc).

Initialize your NSUserDefaults object like this in all applications in the app group and they will share the database:

Objective-C:

[[NSUserDefaults alloc] initWithSuiteName:@"<group identifier>"];

Swift:

NSUserDefaults(suiteName: "<group identifier>")

Keep in mind everything from the [NSUserDefaults standardUserDefaults] database for each application will not carry over into this database.

The documentation gives a correct example as well (As of Beta 3).

And don't forget to synchronize the database:

[yourDefaults synchronize];
Marmoy
  • 8,009
  • 7
  • 46
  • 74
Andrew
  • 15,357
  • 6
  • 66
  • 101
  • 2
    Works only on simulator (XCode 6 beta 5, iOS 8 beta 5) – iOS Dev Aug 13 '14 at 13:56
  • 8
    NOTE: If you're using this with an iOS 8 Extension (e.g. keyboard), you'll need to ensure your extension has RequestsOpenAccess=YES in its Info.plist file. – Kiran Panesar Aug 27 '14 at 15:09
  • 1
    I did RequestsOpenAccess=YES and follow instruction still it wan't works for me on Device and works fine on Emulator, anything specific for device ? – MAC Nov 19 '14 at 12:33
  • Same issue with me, i have integrated share extension in my app, it work on simulator, but not working on real device ,,i have added the RequestOpenAccess =YES but its not work, my question is http://stackoverflow.com/questions/30489894/share-voice-memo-files-in-ios-application – Ravi Ojha May 29 '15 at 03:35
  • @Tejinder You have not provided any information as to how I might diagnose your problem. What version of iOS are you using? More than likely you are running into a provisioning problem and you should check your provisioning profiles to make sure they are correctly configured for app groups. – Andrew Jun 18 '15 at 05:04
  • Do you know if it possible to use the same app group between two different organizations? Would we be able to utilize an app group in a library that we distribute to other organizations? – Mike Richards Jan 26 '16 at 18:22
  • 3
    @MikeRichards not sure, but if I remember correctly the app groups are prefixed with a team id – Andrew Jan 26 '16 at 18:25
  • @SantaClaus Yes you must have same team development id as explain here: https://developer.apple.com/library/ios/documentation/Miscellaneous/Reference/EntitlementKeyReference/Chapters/EnablingAppSandbox.html#//apple_ref/doc/uid/TP40011195-CH4-SW19 – Bernard May 27 '16 at 01:59
  • @SantaClaus Can we allow app groups container for iTunes File Sharing `Application supports iTunes file sharing` ? – Sunil Targe Dec 06 '16 at 03:06
  • Note that for UserDefaults, Apple states that the synchronize method is unnecessary and shouldn’t be used. It will be deprecated at some stage. https://developer.apple.com/documentation/foundation/userdefaults/1414005-synchronize – KTPatel Oct 18 '22 at 16:05
79

Sharing NSUserDefaults data between multiple apps

In order to have shared defaults between an app and an extension or between 2 apps you have to add an App Group in your settings using the following steps:

  1. In the Project Navigator click on the *.xcodeproj file (should be at the top).
  2. To the right of the Project Navigator look for Project and Targets. Under targets click on your primary target (should be the first thing under Targets).
  3. Towards the top, click on the Capabilities tab.
  4. In the App Groups section click the switch to the right to turn App Groups ON.
  5. Click on the + button and add an App Group named group.com.company.myApp.
  6. Go to the same place in your other apps and this group should now be available to select. Turn this group on for each app that will be using this shared data.

Note: If you go to the Apple Developer Portal (the Apple website that shows all of your Certificates, Identifiers, Devices and Provisioning Profiles) and go to Identifiers > App Groups you should see this new App Group.

To store data:

var userDefaults = NSUserDefaults(suiteName: "group.com.company.myApp")!
userDefaults.setObject("user12345", forKey: "userId")
userDefaults.synchronize()

To retrieve data:

var userDefaults = NSUserDefaults(suiteName: "group.com.company.myApp")
if let testUserId = userDefaults?.objectForKey("userId") as? String {
  print("User Id: \(testUserId)")
}
pkamb
  • 33,281
  • 23
  • 160
  • 191
TenaciousJay
  • 6,750
  • 3
  • 45
  • 48
  • 2
    Thanks! It took a bit of searching through documentation to figure out how to do this and I thought others might appreciate the steps I took. – TenaciousJay Feb 05 '16 at 03:47
  • Thanks for the detailed description...! do we need to created any certificate from Apple Developer Portal (the Apple website that shows all of your Certificates, Identifiers, Devices and Provisioning Profiles) to enable this group? same like Push notification.? – Arbaz Shaikh Mar 21 '16 at 08:44
  • When you do step 5 it should add the App Group to the Apple Developer Portal, you should not have to go directly to the Developer Portal to add it. After you do step 5 you can go to the portal and see that it should have created it for you. – TenaciousJay Mar 21 '16 at 21:35
  • @TenaciousJay Superb answer. Just want to add If you want to launch the app from the func application(app: UIApplication, openURL url: NSURL, options: [String : AnyObject]) -> Bool – Taimur Ajmal Oct 09 '16 at 13:20
  • great answer. thanks @TenaciousJay. I am trying to retrieve a variable and its value from AppB into AppA, how would I do this? For instance if "riderCancelledRequest = true" in AppB, how would I retrieve this in AppA? – LizG Jan 19 '17 at 01:10
  • In Swift 3.0, since you're not storing an object you'd probably use "set" instead of "setObject". So in AppB it would include the line: userDefaults!.set(true, forKey: "riderCancelledRequest"). And in AppA include the line: userDefaults?.bool(forKey: "riderCancelledRequest"). – TenaciousJay Jan 19 '17 at 17:38
  • Is there any other process to follow for uploads the apps? I'm Parsing the data between Native iOS and Unity App. – Krunal Nagvadia Sep 06 '19 at 07:18
  • By far the best solution around. This should be the accepted answer – eharo2 May 15 '20 at 03:57
37

Application groups, according to my interpretation of the existing documentation, are primarily targeted for extensions, more specifically, for widgets. Widgets are their own application bundle that coexist with your app. Since they are a separate application and therefore have their own sandbox, you will need to use App Groups to share files.

After some header grep'ing, I think I found the API needed, but was actually put in as part of iOS 7.

NSFileManager has a method on it containerURLForSecurityApplicationGroupIdentifier: where you can pass in the identifier you created when turning on App Groups for your apps:

NSURL *containerURL = [[NSFileManager defaultManager] 
           containerURLForSecurityApplicationGroupIdentifier:@"group.com.company.app"];
Wayne Hartman
  • 18,369
  • 7
  • 84
  • 116
  • Thanks, I believe you're right about extensibility. I'm a little bit more dubious about this iOS 7 method, it seems pretty static to me, iOS 8 introduced real interactions and communication between apps. – streem Jun 04 '14 at 08:15
  • @Justafinger This iOS 7 method is only for creating a URL to write to and read from data between the shared applications. This particular method is only really useful (from what I see so far) for widgets, but not other extensions. – Wayne Hartman Jun 04 '14 at 12:33
  • Well, i believe Apple wanted to make this easier in iOS 8 without having to create a container programmatically and share custom files. [Documentation](https://developer.apple.com/library/prerelease/ios/documentation/General/Conceptual/ExtensibilityPG/ExtensionScenarios.html) states that you should be able to share data by using NSUserDefault. I guess i'm missing something, because that's not working for me. – streem Jun 04 '14 at 14:47
  • @Justafinger I have not been able to get `NSUserDefaults` to work, either. I chalk to up to Beta 1 bug. – Wayne Hartman Jun 04 '14 at 15:59
  • @WayneHartman There is a workaround for the `NSUserDefaults` database. See my answer. – Andrew Jun 05 '14 at 14:40
6

One important trap I tapped into today is the following:

In many projects I saw a single app target and with different bundle identifiers set for each configuration of that target. Here things get messy. What the developers intended was to create a debug app for the debug config and a production app for the release target.

If you do so both apps will share the same NSUserDefaults when they are set up like so

var userDefaults = NSUserDefaults(suiteName: "group.com.company.myApp")
userDefaults!.setObject("user12345", forKey: "userId")
userDefaults!.synchronize()

This causes problems in many places:

  1. Imagine you set YES for a key when a special app-intro-screen has been shown to the user. The other app will now also read YES and don't show the intro.
  2. Yes some apps also store oAuth tokens in their user defaults. Anyways... Depending on the implementation, the app will recognize that there's a token and start retrieving data using the wrong token. The chance is high that this will fail with strange errors.

The solution to this problem in general is to prefix the defaults keys with the current configuration built. You can detect the configuration easily at runtime by setting different bundle identifiers for your configurations. Then just read the bundle identifier from NSBundle.mainBundle(). If you have the same bundle identifiers you need to set different preprocessor macros like

#ifdef DEBUG
  NSString* configuration = @"debug";
#elif RELEASE
  NSString* configuration = @"release";
#endif

In Swift it will look almost the same:

#if DEBUG
  let configuration = "debug"
#elseif RELEASE
  let configuration = "release"
#endif
Nerrolken
  • 1,975
  • 3
  • 24
  • 53
blackjacx
  • 9,011
  • 7
  • 45
  • 56
  • 9
    Your problems exist because you mix all your data in a shared UserDefaults. You should use `NSUserDefaults.standardUserDefaults()` for data that should not be shared. – Daniel Feb 22 '16 at 15:18
4

iOS App Group

App Group allows you to share data(UserDefaults, Files, CoreData(manage model graph), POSIX locks) between different processes(applications, extensions...) from the same development team(account). It creates a shared container whit id which shoyld start from group. for saving/caching data which you are allowed to access thought url and IPC

To use App Group with UserDefaults

  1. Add App Group capability with the same id for ALL targets(app, extension...) which you will get an access from enter image description here

After creation you are able to check it on Apple Developer. Certificates, IDs & Profiles -> Identifiers -> ALL Groups

enter image description here

  1. Write from Application1
let defaults = UserDefaults(suiteName: "group.goforit")
defaults?.setValue("Hello World!", forKey: "key1")
  1. Read from Application2
let defaults = UserDefaults(suiteName: "group.goforit")
let result = defaults?.value(forKey: "key1") //Hello World!

shared container root location single URL.

let rootURL = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: "group.goforit")

//file:///Users/alex/Library/Developer/CoreSimulator/Devices/1AF41817-FE2E-485A-A592-12C39C0B0141/data/Containers/Shared/AppGroup/DC14D43F-2C2C-4771-83BE-64A9F54BD2E1/

[iOS App Extension]

yoAlex5
  • 29,217
  • 8
  • 193
  • 205
2

To store

let shared: NSUserDefaults = NSUserDefaults(suiteName: "group.abcapp")!
shared.setObject("abc.png", forKey: "favEmoji")
shared.synchronize()
Hardik Thakkar
  • 15,269
  • 2
  • 94
  • 81