Ios 无法使用今日扩展的共享应用程序组共享数据

Ios 无法使用今日扩展的共享应用程序组共享数据,ios,swift,core-data,nsfilemanager,ios8-today-widget,Ios,Swift,Core Data,Nsfilemanager,Ios8 Today Widget,我正在尝试创建一个today扩展,通过使用共享应用程序组容器显示来自父应用程序的数据,然后将持久存储添加到上下文中 添加今日扩展目标 为父应用程序和扩展打开应用程序组,然后选择相同的组 添加今日扩展作为数据模型和实体的目标成员资格 将持久存储添加到上下文 获取对象 我没有收到任何错误,但扩展似乎没有获取任何结果。有人对我可能出的问题有什么建议吗 以下是我在扩展名TodayViewController中所做的操作 class TodayViewController: UIViewControl

我正在尝试创建一个today扩展,通过使用共享应用程序组容器显示来自父应用程序的数据,然后将持久存储添加到上下文中

  • 添加今日扩展目标
  • 为父应用程序和扩展打开应用程序组,然后选择相同的组
  • 添加今日扩展作为数据模型和实体的目标成员资格
  • 将持久存储添加到上下文
  • 获取对象
我没有收到任何错误,但扩展似乎没有获取任何结果。有人对我可能出的问题有什么建议吗

以下是我在扩展名
TodayViewController中所做的操作

class TodayViewController: UIViewController, NCWidgetProviding {

var context: NSManagedObjectContext!

@IBOutlet weak var table: UITableView!
var objectsArray = [Objects]()

override func viewDidLoad() {
    super.viewDidLoad()

    let fileManager = NSFileManager.defaultManager()
    var containerPath = fileManager.containerURLForSecurityApplicationGroupIdentifier("group.com.Company.AppName")

    containerPath = containerPath?.URLByAppendingPathComponent("SingleViewCoreData.sqlite")
    let modelURL = NSBundle.mainBundle().URLForResource("AppName", withExtension: "momd")
    let model = NSManagedObjectModel(contentsOfURL: modelURL!)
    let coordinator = NSPersistentStoreCoordinator(managedObjectModel: model!)
    do {
        try coordinator.addPersistentStoreWithType(NSSQLiteStoreType, configuration: nil, URL: containerPath, options: nil)
    } catch {
     print("yellow")
    }

    context = NSManagedObjectContext(concurrencyType: .MainQueueConcurrencyType)
    context.persistentStoreCoordinator = coordinator


    let moc = context
    let request = NSFetchRequest(entityName: "Objects")
    request.sortDescriptors = [NSSortDescriptor(key: "date", ascending: true)]


    do {
        try
            self.objectsArray = moc.executeFetchRequest(request) as! [Objects]
            print ("objects count \(objectsArray.count)")
    } catch {
        // failure
        print("Fetch failed")
    }

    self.table.reloadData()
}

// MARK: - Table view data source

func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    // #warning Potentially incomplete method implementation.
    // Return the number of sections.
    //return sectionsArray.count

    return 1
}


func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    return self.objectsArray.count
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
   let cell = table.dequeueReusableCellWithIdentifier("Cell") as UITableViewCell!

   cell.textLabel!.textColor = UIColor.whiteColor()
   cell.textLabel!.text = self.objectsArray[indexPath.row].title

   return cell

}

func widgetPerformUpdateWithCompletionHandler(completionHandler: ((NCUpdateResult) -> Void)) {
    // Perform any setup necessary in order to update the view.

    // If an error is encountered, use NCUpdateResult.Failed
    // If there's no update required, use NCUpdateResult.NoData
    // If there's an update, use NCUpdateResult.NewData

    completionHandler(NCUpdateResult.NewData)
}

}
在苹果的应用程序扩展编程指南中--与包含应用程序的应用程序共享数据

即使应用程序扩展包嵌套在其包含的应用程序包中,运行的应用程序扩展和包含的应用程序也无法直接访问彼此的容器

所以他们不能直接共享数据,即使你

添加今日扩展作为数据模型和实体的目标成员资格

相反,它们通过共享容器(应用程序组)间接地相互通信,您已经在步骤2中设置了共享容器

因此,解决方案是:

  • 在Controning应用程序中,在共享容器中创建sql数据模型,在此数据库中插入数据。在todayExtension中调整代码,然后获取数据
  • 或者使用NSUserDefaults来共享数据 像这样:

    // Create and share access to an NSUserDefaults object
    NSUserDefaults *mySharedDefaults = [[NSUserDefaults alloc] initWithSuiteName: @"com.example.domain.MyShareExtension"];
    
    // Use the shared user defaults object to update the user's account
    [mySharedDefaults setObject:theAccountName forKey:@"lastAccountName"];
    

    我很容易在我的主应用程序和一个带有应用程序组和默认值的today扩展程序之间共享各种数据。这可能需要15分钟来设置!请参见第5步及其后的内容: