简体   繁体   中英

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM