简体   繁体   English

无法在应用程序和今日扩展之间共享数据

[英]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. 这是用于在应用程序和Today Extension之间共享数据的代码。 For testing purpose i add the only one item in the NSUserDefaults. 为了进行测试,我在NSUserDefaults中添加了唯一一项。

App code for saving the data to NSUserDefaults. 用于将数据保存到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中获取数据

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 从此网站上: 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 它必须以group开头。在演示中,该组称为group.ShareAlike
  4. Let Xcode go through the process of creating this group for you. 让Xcode完成为您创建该组的过程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Swift中的应用和今日扩展之间共享核心数据数据库 - Share Core Data database between app and today extension in Swift iOS Today Extension - 在之间共享核心数据 - iOS Today Extension - Share core data between 如何在 iOS 应用程序和今日视图扩展程序之间共享数据 - How do I share data between iOS app and today view extension (iOS Swift)在应用程序和Today Extension之间共享EKEventStore和访问 - (iOS Swift) Share EKEventStore and access between app and Today Extension 如何使用NSUserDefaults与包含应用程序共享数据? - 今日Extension Widget - How to share data with containing app using NSUserDefaults ? - Today Extension Widget 无法使用“今日扩展的共享应用程序组”共享数据 - Unable to share data using Shared App Groups for Today Extension 我们可以在Today- Widget-Extension中共享App类吗 - Can we share App class in Today- Widget-Extension iOS - 与多个应用程序目标共享今日扩展? - iOS - Share a Today Extension with multiple app targets? 在主机应用和今日扩展之间共享用户生成的图像的最佳方法是什么 - What is the best way to share user generated images between host app and today extension 无法从今天的分机上打开主机应用程序 - Can't open host app from today extension
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM