Sharing Data Between an iOS 8 Share Extension and Main App

Share Data between Project and Share Extension

Your helper function getDataSourceArray() tries to access UserDefaults.standard which is not shared between your host app and the extension app. You need to use the shared container.

  • UserDefaults.standard -> not shared between host and extension
  • UserDefaults(suiteName:) -> shared between host and extension

Try to change your function to this:

func getDataSourceArray() - > [Wishlist] ? {
if let data = UserDefaults(suiteName: UserDefaults.Keys.groupKey).value(forKey: Keys.dataSourceKey) as ? Data {
if let dataSourceArray =
try ? PropertyListDecoder().decode(Array < Wishlist > .self, from: data) as[Wishlist] {
return dataSourceArray
}
}
return nil
}

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