Keychain Problem :( Could not save keychain : -34018

MesutPolat.2316MesutPolat.2316 USMember ✭✭
edited December 2016 in Xamarin.iOS

hi all.
i trying save id on keychain. but this not working. i working iPhoneSimulator.
i get this exeption:
Could not save keychain : -34018

My Code:

`public string UniqueID
String id;

            //use the bundle identifier of the app to identifie the value in the keychain
            String appId = NSBundle.MainBundle.InfoDictionary["CFBundleIdentifier"].ToString();
            String ServiceId = NSBundle.MainBundle.BundleIdentifier;
            //Try to read the id from the keychain
            var rec = new SecRecord(SecKind.GenericPassword)
                Service = ServiceId,
                Account = appId,

            SecStatusCode res;
            var match = SecKeyChain.QueryAsRecord(rec, out res);

            //Store a new ID to the keychain
            if (match?.Generic == null)
                //Get the vendor ID (does change after a reinstall of the app)
                var vendorId = UIKit.UIDevice.CurrentDevice.IdentifierForVendor.AsString().Replace("-", "");

                SecRecord record = new SecRecord(SecKind.GenericPassword)
                    Service = ServiceId,
                    Account = appId,
                    Generic = NSData.FromString(vendorId),
                    Accessible = SecAccessible.Always

                var statusCode = SecKeyChain.Add(record);

                if (statusCode != SecStatusCode.Success) //Could not save key to KeyChain: = -34018
                    Console.WriteLine("Could not save key to KeyChain: " + statusCode);

                id = vendorId;
            {   //Use ID from keychain
                id = match.Generic.ToString();

            return id;

Why can not I use KeyChain?
Can anyone help me with this?
This is not working just a simulator? Does the same problem occur on a physical device?
Thanks all


  • MesutPolat.2316MesutPolat.2316 USMember ✭✭


  • AntonPlotnikovAntonPlotnikov RUMember ✭✭

    Hello. I use following code
    using(var secRecord = new SecRecord(kind)) { secRecord.Service = service; secRecord.Account = account; secRecord.ValueData = NSData.FromString(value, NSStringEncoding.UTF8); secRecord.Accessible = SecAccessible.AlwaysThisDeviceOnly; // var matchRecord = SecKeyChain.QueryAsRecord(secRecord, out var keyChainResult); if (keyChainResult == SecStatusCode.Success) keyChainResult = SecKeyChain.Remove(secRecord); // keyChainResult = SecKeyChain.Add(secRecord); }

    Try it. And you should remove Keychain Access Group from your Entitlements.plist.

  • MesutPolat.2316MesutPolat.2316 USMember ✭✭

    Dear @AntonPlotnikov , thanks for answer.
    First i removed Keychain Access Group from Entitlements.plist.
    I tried your codes but I still get the same error.

    I do not understand where I made a mistake.
    Does the keychain process work only on a physical device?
    Do not we have a chance to try it on Simulator?

    Is there someone to help?

  • AntonPlotnikovAntonPlotnikov RUMember ✭✭
    edited December 2016

    @MesutPolat.2316 please see

    Keychain works in simulator also, not only in a physical device. Unfortunately KeyChain API is a very poor documented.

Sign In or Register to comment.