0

I'm trying to add TodayExtension to my existing app and I want that app and extension will share data through NSUserDefaults. According to this tutorial http://www.glimsoft.com/06/28/ios-8-today-extension-tutorial/ I did like it say:

  • Create new target with Today extension
  • Add new App-Group in Extension and App
  • Add code to app
  • Add code to extension

App Code

func updateExtensionData() {
    let key = "identityCode"
    let valueForStore:Float = 2.0
    let extensiontDefaults = NSUserDefaults(suiteName: "group.company.mineSharedDefaults")
    extensiontDefaults?.setFloat(valueForStore, forKey: key)
    let results = extensiontDefaults?.synchronize()
    let checkCode = extensiontDefaults?.objectForKey(key)
    print("code for save \(valueForStore) synchronize result \(results) readed code \(checkCode!)")
}

Extension code

private func updateData() {
    let key = "identityCode"
    let extensionDefaults = NSUserDefaults(suiteName: "group.company.mineSharedDefaults")
    let checkCode = extensionDefaults?.floatForKey(key)
    print("synchronize result \(resut) readed code \(checkCode!)")
}

Issue

My issue is that extension always read 0 instead of 2. Clearly I'm missing something. There is some major difference between iOS8 and 9 in case of AppGroup and NSUserDefaults? I know that there is but between app an watchOS2.

For being more clear this is keys from App and Extension NSUserDefaults

App Keys

AppleKeyboards

AppleKeyboardsExpanded

AddingEmojiKeybordHandled

AppleLanguages

ApplePasscodeKeyboards

PreferredLanguages

AppleLocale

NSInterfaceStyle

MSVLoggingMasterSwitchEnabledKey

NSLanguages

AppleITunesStoreItemKinds

identityCode

AppleLanguagesDidMigrate

PKEnableStockholmSettings

Extension Keys

AppleKeyboards

AppleKeyboardsExpanded

AddingEmojiKeybordHandled

AppleLanguages

ApplePasscodeKeyboards

PreferredLanguages

AppleLocale

NSInterfaceStyle

MSVLoggingMasterSwitchEnabledKey

NSLanguages

AppleITunesStoreItemKinds

AppleLanguagesDidMigrate

PKEnableStockholmSettings

It's clear that key identityCode is not appearing in extension at all.

Community
  • 1
  • 1
Błażej
  • 3,617
  • 7
  • 35
  • 62
  • have you tried to store/retrieve a NSNumber instead of a float? I see that in the first code you store using setFloat but you retrieve it using objectForKey. If it is stored as fload you should use floatForKey – Duck Sep 23 '15 at 13:16
  • I try with String stored as object and extension read it as nil. I've also checked keys and there is no key with name `identityCode`. – Błażej Sep 23 '15 at 13:18
  • can you explain why checkCode is using objectForKey in the first code and floatForKey on the second? – Duck Sep 23 '15 at 13:20
  • Well I guess it's mine mistake, rest when I experiment with String, but this change nothing. – Błażej Sep 23 '15 at 13:24
  • I have updated my answer. – Duck Sep 23 '15 at 13:55
  • What's the point creating another key if any of those are accessible between app and extension. Nither I try with `thiIsMySuperUniqueKey`. Same result. – Błażej Sep 23 '15 at 13:58
  • these keys are internally used by iOS. You cannot use them to store your stuff. – Duck Sep 23 '15 at 14:03
  • Where I say that I'm using those listed keys? I've just print them to show that when I do it then in app side there is one more key (this is key which I add) and on extension key there is not. – Błażej Sep 23 '15 at 14:08
  • you are using it here: `let key = "identityCode"`. You define that and then store the values using it. You cannot do that. This key is reserved by iOS. You must create your own key. – Duck Sep 23 '15 at 14:11

2 Answers2

0

Ok I'm not sure what helps but I've done two thing:

  • rename current AppGroup identifier and create new one with name group.my.entire.bundle.id.of.my.app.sharedData instead of group.appname.sharedData
  • remove and create new AppGroup identifier

I can use "identityCode" as key.

Błażej
  • 3,617
  • 7
  • 35
  • 62
0

CThere could be several things! first; did you allow keychain sharing under "Capabilities" for both your container app and the extension, and did you set the Keychain groups to the same group?

I recommend using this: https://github.com/kefbytes/KeychainWrapper download it, it's very simple, just add the swiftfile to your project

allow keychain sharing under cabailities, set your serviceName and accesgroup like this

KeychainWrapper.serviceName = "give it a name in here"

(servicename is neccessary for it to work)

KeychainWrapper.accessGroup = "write your accessgroup name here"

(accesgroup is optional, since you are setting it under Capabilities)

save a value to a key like this

KeychainWrapper.setString("2", forKey: "identityCode"

and retrieve with

KeychainWrapper.stringForKey("identityCode")

MrKale
  • 43
  • 1
  • 11