Anbu Raj
Anbu Raj

Reputation: 841

Can't share data between app and today extension

I am developing to-do list app. In this app, i add the today extension. It is used to show the to-do list for today.

This is the code for share data between app and today extension. For testing purpose i add the only one item in the NSUserDefaults.

App code for saving the data to NSUserDefaults.

NSUserDefaults *shared = [[NSUserDefaults alloc]initWithSuiteName:@"group.compname.appname"];

[shared setValue:@"Test" forKey:@"test"];

[shared synchronize];

Today extension code for fetch the data from NSUserDefaults

NSUserDefaults *shared = [[NSUserDefaults alloc]initWithSuiteName:@"group.compname.appname"];

NSString *str =  [ shared valueForKey:@"test"] ;

NSLog(@" Text = %@", str);

I am always getting the 'null' value.

Upvotes: 0

Views: 496

Answers (1)

Markus
Markus

Reputation: 436

Sounds like you haven't added the group to the entitlements/capabilities.

From this site: http://www.shinobicontrols.com/blog/posts/2014/07/21/ios8-day-by-day-day-2-sharing-extension

  1. Go to the capabilities tab of the app's target
  2. Enable App Groups
  3. Create a new app group, entitled something appropriate. It must start with group.. In the demo the group is called group.ShareAlike
  4. Let Xcode go through the process of creating this group for you.

Upvotes: 1

Related Questions