无法使用今日分机的共享应用程序组共享数据
问题描述:
我正试图通过使用共享应用程序组容器来显示来自父应用程序的数据,然后将持久存储添加到上下文中的今天的扩展。无法使用今日分机的共享应用程序组共享数据
- 应用上的群体加入今天扩张对象
- 转母的应用和推广,并选择同一组
- 添加如今扩展为目标会员的数据模型和实体
- 添加持久性存储上下文
- 取回对象
我没有得到任何错误,但扩展似乎没有获取任何结果。有人有任何建议,我可能会出错吗?
继承人我在做什么在扩展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)
}
}
答
在苹果的应用扩展编程指南 - 共享数据与您包含的应用程序
即使一个应用程序扩展束嵌套在其包含的应用程序包中,正在运行的应用程序扩展程序和包含应用程序的人不能直接访问彼此的容器容器。
所以他们将无法直接共享数据,即使你
加入今天扩展为目标的成员资格数据模型和实体
相反,它们连通,其他通过共享容器(App组)间接进行,您已经在步骤2中设置了它。
因此,解决办法是:
- 在你Contraning应用程序,在您的共享容器创建SQL数据模型,在此database.Adjust在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"];
这是很容易对我来说,我主要的应用程序和扩展的今天与应用组和NSUserDefaults的之间共享各种数据。这可能需要15分钟设置!请参阅步骤5及以后的内容:http://www.glimsoft.com/06/28/ios-8-today-extension-tutorial/ – owlswipe