[英]Unable to share data using Shared App Groups for Today Extension
I am trying to create a today extension that displays data from the parent app by using a shared app group container, and then adding the persistent store to a context. 我正在尝试创建一个今天的扩展,通过使用共享应用程序组容器显示来自父应用程序的数据,然后将持久存储添加到上下文。
I get no errors but the extension does not seem to be fetching any results. 我没有错误,但扩展似乎没有取得任何结果。 Does anybody have any suggestions where I may be going wrong ?
有没有人对我可能出错的地方有任何建议?
Heres what I am doing in the extension TodayViewController
以下是我在扩展
TodayViewController
所做的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)
}
}
In Apple's App Extension Programming Guide -- Sharing Data with Your Containing App 在Apple的App Extension编程指南中 - 与您的应用程序共享数据
Even though an app extension bundle is nested within its containing app's bundle, the running app extension and containing app have no direct access to each other's containers .
即使应用程序扩展包嵌套在其包含的应用程序包中,运行的应用程序扩展和包含应用程序也无法直接访问彼此的容器 。
So they wouldn't be able to share data directly,even if you 因此,即使您,他们也无法直接共享数据
Add Today Extension as Target Membership for Data model and entities
将今日扩展添加为数据模型和实体的目标成员资格
Instead,they communicated with each other by shared container(App groups) indirectly,which you had already setup in step 2. 相反,它们间接通过共享容器(应用程序组)相互通信,您已在步骤2中设置了它。
So the solution is : 所以解决方案是:
like this: 像这样:
// 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"];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.