Ios8 Extension and Container App How to Share Data

ios8 extension and container app how to share data

My solution is edit the Info.plist of my custom keyboard:

NSExtension -> NSExtensionAttributes -> RequestsOpenAccess

Make it YES.

Then delete the keyboard in your device and re-add it. In your keyboard add page you will find a setting named "Allow Full Access", enable it and the keyboard will access the shared data.

iOS8 extension : share images between container and extension

From Apple's Documentation on App Extension Programming

Sharing Data with Your Containing App

The security domains for an app extension and its containing app are distinct, even though the extension bundle is nested within the containing app’s bundle. By default, your extension and its containing app have no direct access to each other’s containers.

You can, however, enable data sharing. For example, you might want to allow your app extension and its containing app to share a single large set of data, such as prerendered assets.

.....

When you set up a shared container, the containing app—and each contained app extension that you allow to participate in data sharing—have read and write access to the shared container. To avoid data corruption, you must synchronize data accesses. Use Core Data, SQLite, or Posix locks to help coordinate data access in a shared container.

Accessing Core Data from both container app and extension

 lazy var persistentContainer: NSPersistentContainer = {
/*
The persistent container for the application. This implementation
creates and returns a container, having loaded 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.
*/
let container = NSPersistentContainer(name: "xx")

let appName: String = "xx"
var persistentStoreDescriptions: NSPersistentStoreDescription

let storeUrl = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: "group.com.xx.xx.container")!.appendingPathComponent("xx.sqlite")

let description = NSPersistentStoreDescription()
description.shouldInferMappingModelAutomatically = true
description.shouldMigrateStoreAutomatically = true
description.url = storeUrl

container.persistentStoreDescriptions = [NSPersistentStoreDescription(url: FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: "group.com.xxx.xx.container")!.appendingPathComponent("xx.sqlite"))]

container.loadPersistentStores(completionHandler: { (storeDescription, error) in
if let error = error as NSError? {
fatalError("Unresolved error \(error), \(error.userInfo)")
}
})
return container
}()

Today extension: syncing data with container app

Well, I have learned two things here:

First of all, Always double check your entitlements, mine somehow got messed up, and that's why the shared container behaved so awkwardly.

Second:
Although viewWillAppear(_:) is not called, when you dismiss the notification center, it's still possible to trigger an update from your app delegate:

func applicationDidBecomeActive(application: UIApplication) {
NSNotificationCenter.defaultCenter().postNotificationName(updateDataNotification, object: nil)
}

Then in your view controller:

override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)

NSNotificationCenter.defaultCenter().addObserverForName(updateDataNotification, object: nil, queue: NSOperationQueue.mainQueue()) { (_) -> Void in
self.tableView.reloadData()
}
}

override func viewDidDisappear(animated: Bool) {
super.viewDidDisappear(animated)
NSNotificationCenter.defaultCenter().removeObserver(self)
}

Updating your Today widget is simple: each time the notification center is pulled down, viewWillAppear(:_) is called, so you can query for new data there.

I'll update the example project on GitHub shortly.

How to share Parse login session with iOS extension?

There are two possible solutions:

A) The elegant approach

Enable local data sharing which shares persistent data between the main iOS app and its extensions.

// Enable data sharing in main app.
Parse.enableDataSharingWithApplicationGroupIdentifier("...")

// Enable data sharing in app extensions.
Parse.enableDataSharingWithApplicationGroupIdentifier("...", containingApplicaiton: "...")

Local data sharing in Parse SDKs allows you do share persistent local
data between your main application and extensions that it contains,
including Keyboard, Share/Today/Photo/Action extensions and Document
Providers.

As described in the docs.

B) The manual approach

After login in the app, store the Parse session token PFUser.current().sessionToken in an ecrypted, shared data container.

The extension can then access the session token and continue the session with PFUser.become(sessionToken:).



Related Topics



Leave a reply



Submit