NSUserDefaults data sharing between Today Extension and Parent App

tomkou4tomkou4 USMember

Hello! I am working on the Today Extension project to my iOS App. And I am trying to achieve data sharing between them. I have added the App Groups possibility to App ID's to both projects. I have tried using the NSUserDefaults with the App Group ID, and interestingly I can share the data within a project (either Parent App or Today Extension) this way, but not between them. Am I missing something? Since both projects can use this App Group ID to share data within themselves, how is it not possible to share between them? Should I allow the external access somewhere?

Answers

  • DannyCDannyC US ✭✭✭✭
  • ramonesteban78ramonesteban78 ESUniversity ✭✭

    I'm having similar issues.
    I have followed all the steps described here: https://github.com/rolfbjarne/todaysharing/blob/master/README.md

    I have added the group app in the Entitlements.plist on both projects.

    But I'm not able to share info in the NSUserDefaults.

    Did you find a solution in your case?

  • ramonesteban78ramonesteban78 ESUniversity ✭✭

    I solved. What an silly error I was making!!

    Make sure in the properties of both projects, in the section iOS Bundle Sining, the Custom Entitlements field is informed with your Entitlements.plist file.

  • MANISH1988MANISH1988 Member
    edited April 2018

    I am having same issue unable to get the data from App Extension to App Container via NSUserDefaults
    I have already checked the pList and App Group Id

    Here is my following code

                private void saveDataForContainerApp()
                {
                    NSUserDefaults myDefaults = new NSUserDefaults("group.com.compnyname.appname");
                    myDefaults.SetString(mainObject, "contentKey"); 
                    myDefaults.SetURL(mainURL, "contentURL"); 
                    myDefaults.Synchronize();
    
                }
    

    In Container App

                if(myDefaults2 == null)
                {
                    myDefaults2 = new NSUserDefaults("group.com.compnyname.appname", NSUserDefaultsType.SuiteName);
                }
                Console.WriteLine("Testing defaults content Container " + myDefaults2.StringForKey("contentKey"));
                Console.WriteLine("Testing defaults content Container " + myDefaults2.StringForKey("contentURL"));
    

    Please let me know what can be the cause, If I am trying to print the data from user defaults in extension app it is working properly but I am unable to get the data in Container's App.

  • Cdn_EuroCdn_Euro Member ✭✭✭

    @MANISH1988 Did you get it working? I am trying to solve the same problem. I am trying to access NSUserDefaults in an Extension app. What is the code to store in the Xamarin iOS Project and retrieve in the Extension. Is the above code correct?

Sign In or Register to comment.