简体   繁体   English

如何使用NSUserDefaults与包含应用程序共享数据? - 今日Extension Widget

[英]How to share data with containing app using NSUserDefaults ? - Today Extension Widget

I am trying to create a Today Extension Widget which displays stored data in the widget. 我正在尝试创建一个Today Extension Widget ,它在窗口小部件中显示存储的数据。

Here's what I have done; 这就是我所做的;

  • Added New Today Widget Target 添加了New Today Widget Target
  • Set up UIViewController for widget 为小部件设置UIViewController
  • Enable App Groups for both the app and extension 为应用和扩展程序启用应用组

Now I have hit a road block, I am unsure the best way to retrieve and display a simple array of fetched data. 现在我遇到了障碍,我不确定检索和显示简单的获取数据数组的最佳方法。

There is very little Swift tutorials and they often do not use core data. Swift教程非常少,他们通常不使用核心数据。

In the main app project I fetch the array. 在主应用程序项目中,我获取数组。

 let moc = (UIApplication.sharedApplication().delegate as! AppDelegate).managedObjectContext
 let request = NSFetchRequest(entityName: "Objects")
    do {
      try
      self.objectsArray = moc.executeFetchRequest(request) as! [Objects]
    } catch {

    }

How can I use NSUserDefaults to store the objects array and then use in the today widget extension ? 如何使用NSUserDefaults存储对象数组,然后在今天的小部件扩展中使用? Or Even an array of string values. 或者甚至是一串字符串值。

1) Get url to your database 1)获取数据库的URL

var containerPath: String = NSFileManager.defaultManager().containerURLForSecurityApplicationGroupIdentifier‌​(YOUR_SECURITY_APP_GROUP).path 

var sqlitePath: String = "(containerPath)/("database.sqlite")"

2) Create Persistent Store Coordinator as you do in parent app. 2)像在父应用程序中一样创建持久性存储协调器。

3) Set it for your Managed Object context 3)为您的托管对象上下文设置它

context = NSManagedObjectContext()
context.persistentStoreCoordinator = coordinator

4) Retrieve objects as you do in your parent app 4)像在父应用程序中一样检索对象

let moc = context
let request = NSFetchRequest(entityName: "Objects")
do {
  try self.objectsArray = moc.executeFetchRequest(request) as! [Objects]
} catch {}

If you set up the App Groups correct and added the same group for both Application and Extension you can use NSUserDefaults. 如果您正确设置应用程序组并为应用程序和扩展程序添加了相同的组,则可以使用NSUserDefaults。 Just for instance, to write something from the app, you need: 例如,要从应用程序中编写内容,您需要:

static func setSharedScoreNumber(score: Int) {
    let defaults = UserDefaults(suiteName: "group.bubblewrapSharedDefaults") // this is the name of the group we added in "App Groups"
    defaults?.set(String(score), forKey: "score")
    defaults?.synchronize()
}

And to read from Todays Extension: 并阅读今日推广:

private func getScore() -> String {
    let defaults = UserDefaults(suiteName: "group.bubblewrapSharedDefaults")
    defaults?.synchronize()
    return String(describing: defaults!.object(forKey: "score") ?? "0")
}

Here's the complete guide how to do so. 这是完整的指南如何做到这一点。

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

相关问题 iOS Today扩展NSUserDefaults使用回调与包含应用程序共享数据 - iOS Today Extension NSUserDefaults Sharing Data with Containing App using Callbacks 无法使用“今日扩展的共享应用程序组”共享数据 - Unable to share data using Shared App Groups for Today Extension iOs - 如何使用url将数据从Widget(今日扩展)传递到App? - iOs - How to pass data from Widget (Today Extension) to App with url? 今日应用扩展小部件点击以打开包含应用 - Today App Extension Widget Tap To Open Containing App 我们可以在Today- Widget-Extension中共享App类吗 - Can we share App class in Today- Widget-Extension 如何在今天的小部件扩展中使用核心数据? - How to use Core data in widget today extension? 使用Core Data + iCloud sync时在应用程序和Today小部件之间共享数据的正确方法 - Correct way to share data between app and Today widget when using Core Data + iCloud sync 使用NSUserDefaults与Today Extension(小部件)共享一组自定义对象 - Sharing an array of custom objects with Today Extension (widget) with NSUserDefaults 在Swift中的应用和今日扩展之间共享核心数据数据库 - Share Core Data database between app and today extension in Swift 无法在应用程序和今日扩展之间共享数据 - Can't share data between app and today extension
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM