简体   繁体   English

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

[英]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. 我正在尝试创建一个今天的扩展,通过使用共享应用程序组容器显示来自父应用程序的数据,然后将持久存储添加到上下文。

  • Add Today Extension Target 添加今天扩展目标
  • Turn on app groups for parent app and extension and select same group 为父应用和扩展程序启用应用程序组,然后选择相同的组
  • Add Today Extension as Target Membership for Data model and entities 将今日扩展添加为数据模型和实体的目标成员资格
  • Add Persistent store to context 将持久存储添加到上下文
  • Fetch Objects 获取对象

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 : 所以解决方案是:

  1. In your Contraning App,create the sql data model in your shared container ,insert data in this database.Adjust your code in todayExtension ,and fetch data. 在Contraning App中,在共享容器中创建sql数据模型,在此数据库中插入数据。在todayExtension中调整代码,然后获取数据。
  2. Or Using NSUserDefaults to share data instead. 或者使用NSUserDefaults来共享数据。

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.

相关问题 如何使用NSUserDefaults与包含应用程序共享数据? - 今日Extension Widget - How to share data with containing app using NSUserDefaults ? - Today Extension Widget 今日扩展(应用组) - Today Extension (App Groups) 在Swift中的应用和今日扩展之间共享核心数据数据库 - Share Core Data database between app and today extension in Swift 无法在应用程序和今日扩展之间共享数据 - Can't share data between app and today extension 如何使用应用程序组与多个应用程序(不仅仅是扩展程序)共享核心数据,并避免损坏的核心数据存储? - How to share Core Data with multiple Apps (not just an extension) using App Groups and avoid corrupted Core Data stores? iOS - 与多个应用程序目标共享今日扩展? - iOS - Share a Today Extension with multiple app targets? iOS Today Extension - 在之间共享核心数据 - iOS Today Extension - Share core data between iOS Today扩展NSUserDefaults使用回调与包含应用程序共享数据 - iOS Today Extension NSUserDefaults Sharing Data with Containing App using Callbacks 使用今日扩展访问托管应用中的代码和数据 - Access code and data in hosting app using today extension 如何在 iOS 应用程序和今日视图扩展程序之间共享数据 - How do I share data between iOS app and today view extension
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM