Communicating and persisting data between apps with App Groups










72















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?










share|improve this question




























    72















    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?










    share|improve this question


























      72












      72








      72


      38






      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?










      share|improve this question
















      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?







      ios objective-c ios8 ios-app-extension






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Mar 21 '16 at 21:58









      TenaciousJay

      4,72633143




      4,72633143










      asked Jun 3 '14 at 12:43









      streemstreem

      7,40552538




      7,40552538






















          5 Answers
          5






          active

          oldest

          votes


















          72














          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];





          share|improve this answer




















          • 2





            Works only on simulator (XCode 6 beta 5, iOS 8 beta 5)

            – Otto Boy
            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 stackoverflow.com/questions/30489894/…

            – Ravi Ojha
            May 29 '15 at 3:35






          • 3





            @MikeRichards not sure, but if I remember correctly the app groups are prefixed with a team id

            – Santa Claus
            Jan 26 '16 at 18:25



















          59














          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)")






          share|improve this answer

























          • this needs to be voted up. great answer.

            – AppA11y
            Jan 8 '16 at 16:22











          • 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 5 '16 at 3:47











          • Best answer! Thank you so much, exactly what I needed +1000000

            – Unome
            Feb 9 '16 at 19:57











          • 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 8: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



















          36














          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"];





          share|improve this answer























          • 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 4 '14 at 8: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 4 '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 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 4 '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 4 '14 at 15:59











          • @WayneHartman There is a workaround for the NSUserDefaults database. See my answer.

            – Santa Claus
            Jun 5 '14 at 14:40


















          4














          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





          share|improve this answer




















          • 7





            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



















          1














          To store



          let shared: NSUserDefaults = NSUserDefaults(suiteName: "group.abcapp")!
          shared.setObject("abc.png", forKey: "favEmoji")
          shared.synchronize()





          share|improve this answer






















            Your Answer






            StackExchange.ifUsing("editor", function ()
            StackExchange.using("externalEditor", function ()
            StackExchange.using("snippets", function ()
            StackExchange.snippets.init();
            );
            );
            , "code-snippets");

            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "1"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader:
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            ,
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













            draft saved

            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f24015506%2fcommunicating-and-persisting-data-between-apps-with-app-groups%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            5 Answers
            5






            active

            oldest

            votes








            5 Answers
            5






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            72














            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];





            share|improve this answer




















            • 2





              Works only on simulator (XCode 6 beta 5, iOS 8 beta 5)

              – Otto Boy
              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 stackoverflow.com/questions/30489894/…

              – Ravi Ojha
              May 29 '15 at 3:35






            • 3





              @MikeRichards not sure, but if I remember correctly the app groups are prefixed with a team id

              – Santa Claus
              Jan 26 '16 at 18:25
















            72














            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];





            share|improve this answer




















            • 2





              Works only on simulator (XCode 6 beta 5, iOS 8 beta 5)

              – Otto Boy
              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 stackoverflow.com/questions/30489894/…

              – Ravi Ojha
              May 29 '15 at 3:35






            • 3





              @MikeRichards not sure, but if I remember correctly the app groups are prefixed with a team id

              – Santa Claus
              Jan 26 '16 at 18:25














            72












            72








            72







            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];





            share|improve this answer















            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];






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Mar 28 '17 at 12:08









            Marmoy

            6,29043767




            6,29043767










            answered Jun 5 '14 at 14:39









            Santa ClausSanta Claus

            12.2k55291




            12.2k55291







            • 2





              Works only on simulator (XCode 6 beta 5, iOS 8 beta 5)

              – Otto Boy
              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 stackoverflow.com/questions/30489894/…

              – Ravi Ojha
              May 29 '15 at 3:35






            • 3





              @MikeRichards not sure, but if I remember correctly the app groups are prefixed with a team id

              – Santa Claus
              Jan 26 '16 at 18:25













            • 2





              Works only on simulator (XCode 6 beta 5, iOS 8 beta 5)

              – Otto Boy
              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 stackoverflow.com/questions/30489894/…

              – Ravi Ojha
              May 29 '15 at 3:35






            • 3





              @MikeRichards not sure, but if I remember correctly the app groups are prefixed with a team id

              – Santa Claus
              Jan 26 '16 at 18:25








            2




            2





            Works only on simulator (XCode 6 beta 5, iOS 8 beta 5)

            – Otto Boy
            Aug 13 '14 at 13:56






            Works only on simulator (XCode 6 beta 5, iOS 8 beta 5)

            – Otto Boy
            Aug 13 '14 at 13:56





            8




            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





            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




            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





            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 stackoverflow.com/questions/30489894/…

            – Ravi Ojha
            May 29 '15 at 3:35





            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 stackoverflow.com/questions/30489894/…

            – Ravi Ojha
            May 29 '15 at 3:35




            3




            3





            @MikeRichards not sure, but if I remember correctly the app groups are prefixed with a team id

            – Santa Claus
            Jan 26 '16 at 18:25






            @MikeRichards not sure, but if I remember correctly the app groups are prefixed with a team id

            – Santa Claus
            Jan 26 '16 at 18:25














            59














            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)")






            share|improve this answer

























            • this needs to be voted up. great answer.

              – AppA11y
              Jan 8 '16 at 16:22











            • 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 5 '16 at 3:47











            • Best answer! Thank you so much, exactly what I needed +1000000

              – Unome
              Feb 9 '16 at 19:57











            • 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 8: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
















            59














            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)")






            share|improve this answer

























            • this needs to be voted up. great answer.

              – AppA11y
              Jan 8 '16 at 16:22











            • 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 5 '16 at 3:47











            • Best answer! Thank you so much, exactly what I needed +1000000

              – Unome
              Feb 9 '16 at 19:57











            • 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 8: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














            59












            59








            59







            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)")






            share|improve this answer















            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)")







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 13 '18 at 4:08









            pkamb

            15k1283122




            15k1283122










            answered Oct 27 '15 at 17:59









            TenaciousJayTenaciousJay

            4,72633143




            4,72633143












            • this needs to be voted up. great answer.

              – AppA11y
              Jan 8 '16 at 16:22











            • 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 5 '16 at 3:47











            • Best answer! Thank you so much, exactly what I needed +1000000

              – Unome
              Feb 9 '16 at 19:57











            • 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 8: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


















            • this needs to be voted up. great answer.

              – AppA11y
              Jan 8 '16 at 16:22











            • 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 5 '16 at 3:47











            • Best answer! Thank you so much, exactly what I needed +1000000

              – Unome
              Feb 9 '16 at 19:57











            • 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 8: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

















            this needs to be voted up. great answer.

            – AppA11y
            Jan 8 '16 at 16:22





            this needs to be voted up. great answer.

            – AppA11y
            Jan 8 '16 at 16:22













            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 5 '16 at 3:47





            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 5 '16 at 3:47













            Best answer! Thank you so much, exactly what I needed +1000000

            – Unome
            Feb 9 '16 at 19:57





            Best answer! Thank you so much, exactly what I needed +1000000

            – Unome
            Feb 9 '16 at 19:57













            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 8:44





            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 8: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






            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












            36














            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"];





            share|improve this answer























            • 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 4 '14 at 8: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 4 '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 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 4 '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 4 '14 at 15:59











            • @WayneHartman There is a workaround for the NSUserDefaults database. See my answer.

              – Santa Claus
              Jun 5 '14 at 14:40















            36














            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"];





            share|improve this answer























            • 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 4 '14 at 8: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 4 '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 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 4 '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 4 '14 at 15:59











            • @WayneHartman There is a workaround for the NSUserDefaults database. See my answer.

              – Santa Claus
              Jun 5 '14 at 14:40













            36












            36








            36







            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"];





            share|improve this answer













            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"];






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Jun 4 '14 at 2:58









            Wayne HartmanWayne Hartman

            14.2k470107




            14.2k470107












            • 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 4 '14 at 8: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 4 '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 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 4 '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 4 '14 at 15:59











            • @WayneHartman There is a workaround for the NSUserDefaults database. See my answer.

              – Santa Claus
              Jun 5 '14 at 14:40

















            • 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 4 '14 at 8: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 4 '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 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 4 '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 4 '14 at 15:59











            • @WayneHartman There is a workaround for the NSUserDefaults database. See my answer.

              – Santa Claus
              Jun 5 '14 at 14:40
















            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 4 '14 at 8:15





            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 4 '14 at 8: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 4 '14 at 12:33





            @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 4 '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 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 4 '14 at 14:47





            Well, i believe Apple wanted to make this easier in iOS 8 without having to create a container programmatically and share custom files. Documentation 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 4 '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 4 '14 at 15:59





            @Justafinger I have not been able to get NSUserDefaults to work, either. I chalk to up to Beta 1 bug.

            – Wayne Hartman
            Jun 4 '14 at 15:59













            @WayneHartman There is a workaround for the NSUserDefaults database. See my answer.

            – Santa Claus
            Jun 5 '14 at 14:40





            @WayneHartman There is a workaround for the NSUserDefaults database. See my answer.

            – Santa Claus
            Jun 5 '14 at 14:40











            4














            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





            share|improve this answer




















            • 7





              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














            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





            share|improve this answer




















            • 7





              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












            4








            4







            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





            share|improve this answer















            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






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Mar 7 '16 at 21:26









            Nerrolken

            1,15321847




            1,15321847










            answered Dec 10 '15 at 23:31









            blackjacxblackjacx

            1,81111926




            1,81111926







            • 7





              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













            • 7





              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








            7




            7





            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






            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












            1














            To store



            let shared: NSUserDefaults = NSUserDefaults(suiteName: "group.abcapp")!
            shared.setObject("abc.png", forKey: "favEmoji")
            shared.synchronize()





            share|improve this answer



























              1














              To store



              let shared: NSUserDefaults = NSUserDefaults(suiteName: "group.abcapp")!
              shared.setObject("abc.png", forKey: "favEmoji")
              shared.synchronize()





              share|improve this answer

























                1












                1








                1







                To store



                let shared: NSUserDefaults = NSUserDefaults(suiteName: "group.abcapp")!
                shared.setObject("abc.png", forKey: "favEmoji")
                shared.synchronize()





                share|improve this answer













                To store



                let shared: NSUserDefaults = NSUserDefaults(suiteName: "group.abcapp")!
                shared.setObject("abc.png", forKey: "favEmoji")
                shared.synchronize()






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Sep 6 '16 at 10:01









                Hardik ThakkarHardik Thakkar

                7,25114751




                7,25114751



























                    draft saved

                    draft discarded
















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid


                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.

                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f24015506%2fcommunicating-and-persisting-data-between-apps-with-app-groups%23new-answer', 'question_page');

                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    這個網誌中的熱門文章

                    How to read a connectionString WITH PROVIDER in .NET Core?

                    Node.js Script on GitHub Pages or Amazon S3

                    Museum of Modern and Contemporary Art of Trento and Rovereto