Store token locally in Xamarin.IOS

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Store token locally in Xamarin.IOS



I am working on a Xamarin cross-platform native project and I have to store the token (get from the server after login) both Android and IOS side. On the Android side I used SharedPrefferences which works great, but I can't find a solution for IOS. Now I am using KeyChain, but the result is always wrong.



Here is my code:



The KeyChain class:


using System;
using Foundation;
using Security;

namespace BloodNotes.iOS

public class KeyChain

public string ValueForKey(string key)

var record = ExistingRecordForKey(key);
SecStatusCode resultCode;
var match = SecKeyChain.QueryAsRecord(record, out resultCode);

if (resultCode == SecStatusCode.Success)
return NSString.FromData(match.ValueData, NSStringEncoding.UTF8);
else
return String.Empty;


public void SetValueForKey(string value, string key)

var record = ExistingRecordForKey(key);
if (value == "")

if (ValueForKey(key) != "")
RemoveRecord(record);

return;


// if the key already exists, remove it
if (ValueForKey(key) != "")
RemoveRecord(record);

var result = SecKeyChain.Add(CreateRecordForNewKeyValue(key, value));
if (result != SecStatusCode.Success)

throw new Exception(String.Format("Error adding record: " + result)); // I ALWAYS GET THIS EXCEPTION



private SecRecord CreateRecordForNewKeyValue(string key, string value)

return new SecRecord(SecKind.GenericPassword)

Account = key,
ValueData = NSData.FromString(value, NSStringEncoding.UTF8),
;


private SecRecord ExistingRecordForKey(string key)

return new SecRecord(SecKind.GenericPassword)

Account = key,
Label = key,
;


private bool RemoveRecord(SecRecord record)

var result = SecKeyChain.Remove(record);
if (result != SecStatusCode.Success)

throw new Exception(String.Format("Error removing record: 0", result));


return true;





And the TokenService:


using BloodNotes.ViewModel;
using Foundation;
using System.Diagnostics;

namespace BloodNotes.iOS.TokenService

class TokenService : Service.ITokenService

public const string KEY = "token";

public void SaveToken(string token)

KeyChain storage = new KeyChain();
storage.SetValueForKey(token, KEY);

Debug.WriteLine("RESULT: " + storage.ValueForKey(KEY));





Please, give me advise.



Thanks in advance for the answers!




1 Answer
1



Finally, I found a solution. Instead of KeyChain use NSUserDefaults.StandardUserDefaults, so my TokenService looks like:


using Foundation;
using System.Diagnostics;

namespace BloodNotes.iOS.TokenService

class TokenService : Service.ITokenService

public const string KEY = "token";

public void SaveToken(string token)

var storage = NSUserDefaults.StandardUserDefaults;
NSString value = new NSString(token);
NSString key = new NSString(KEY);
storage.SetValueForKey(value, key);

Debug.WriteLine("RESULT: " + storage.ValueForKey(key).ToString());








By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard