Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Coredata in ios keyboard extension

I read below github page and used the code for sharing coredata between my ios Keyboard Extension and the app.

https://github.com/maximbilan/iOS-Shared-CoreData-Storage-for-App-Groups

The code is working right on the simulator but it is not working but addPersistentStore not working on real device:

lazy var persistentStoreCoordinator: NSPersistentStoreCoordinator? = {
    // The persistent store coordinator for the application. This implementation creates and return a coordinator, having added the store for the application to it. This property is optional since there are legitimate error conditions that could cause the creation of the store to fail.
    // Create the coordinator and store
    var coordinator: NSPersistentStoreCoordinator? = NSPersistentStoreCoordinator(managedObjectModel: self.managedObjectModel)

    let directory = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier:"group.testKeyboard.asanpardakht.ir")!

    let url = directory.appendingPathComponent("AsanPardakhtSharedData.sqlite")

    do {
        try coordinator!.addPersistentStore(ofType: NSSQLiteStoreType, configurationName: nil, at: url, options: nil)
    } catch var error as NSError {
        coordinator = nil
        NSLog("Unresolved error \(error), \(error.userInfo)")
        abort()
    } catch {
        fatalError()
    }
    print("\(String(describing: coordinator?.persistentStores))")
    return coordinator
}()

I get below errors:

NSError domain: "NSCocoaErrorDomain" - code: 512

"reason" : "Failed to create file; code = 1"

I tried setting RequestOpenAccess to YES in my info.plist file but it is not still working. Does someone has any idead about the problem?

like image 639
Husein Behboudi Rad Avatar asked Oct 24 '25 05:10

Husein Behboudi Rad


1 Answers

I figured out that below post works right for keyboard extension too:

https://github.com/maximbilan/iOS-Shared-CoreData-Storage-for-App-Groups

The only problem is that for keyboard extension we should also set RequestOpenAccess at info.plist to YES.

When set it to YES in the setting of the keyboard an option appears for the Full Access we should turn it On and then the code also works on real device.

Developers should make sure that before calling the method in the quest ask user to turn the option to On.

like image 129
Husein Behboudi Rad Avatar answered Oct 26 '25 18:10

Husein Behboudi Rad



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!