7

Currently, I have an iOS app written in Swift that will be replaced by a new version via Xamarin Forms. I am having trouble accessing the old (Swift) Keychain, where ItemNotFound is the only request code returned. Both apps are using the same Bundle ID.

Here's the code I'm using:

byte[] _LegacyGetKeychainItem<T>(string key)
{
    using (var record = ExistingRecordForKey(key))
    using (var match = SecKeyChain.QueryAsData(record, false, out var resultCode))
    {
        if (resultCode == SecStatusCode.Success)
        {
            return match.ToArray();
        }
    }

    throw new InvalidOperationException(string.Format("GetKeychainItem: data was null for key: {0}", key));
}

SecRecord ExistingRecordForKey(string key)
{
    return new SecRecord(SecKind.GenericPassword)
    {
        Service = SEC_SERVICE, // Same as Bundle ID
        Account = key
    };
}
CharithJ
  • 46,289
  • 20
  • 116
  • 131
igorgue
  • 17,884
  • 13
  • 37
  • 54
  • 4
    Why is this tagged Android? Have you deleted the old app? Or did you install the new app with the same bundle id on top of the old app? What code are you using to access the Keychain? What errors or exceptions are you getting? – Jason Aug 27 '19 at 22:08
  • 1
    Here is an article , maybe helpful .https://riptutorial.com/xamarin-ios/example/8337/using-keychain – Junior Jiang Aug 28 '19 at 03:27
  • @Jason I updated the question, removed the android tag and added some of my code – igorgue Aug 28 '19 at 20:28
  • @igorgue: Have you checked the Entitlements.plist for Keychain enablement? – CharithJ Sep 06 '19 at 04:21
  • @CharithJ Yeah they are the same... – igorgue Sep 06 '19 at 17:58
  • Have you selected the plist for the correct configuration as I have shown in the screenshot? – CharithJ Sep 06 '19 at 21:44

1 Answers1

0

Check if Entitlements.plist have the Enable Keychain set

<dict>
    <key>keychain-access-groups</key>
    <array>
        <string>BUNDLE_ID</string>
    </array>
</dict>

If it's already there. Check your project's iOS Bundle Signing (Project => iOS Bundle Signing).

You may have to set "Custom Entitlements" as "Entitlement.plist", because default setting for "Custom Entitlements" is empty for simulators. Have a look at this and this answers.

enter image description here enter image description here

CharithJ
  • 46,289
  • 20
  • 116
  • 131