Storing access token and refresh token in KeyChain - ios

I would like to know how to effectively store the access token, refresh tokens and their expirations in the iOS keychain.
All the examples I have seen seem to store only one key-value combination. How do we store multiple key values for one keychain identifier?
If there is a better way to store the above, please let me know.

You will first want to build a NSDictionary with the key/values you want.
Next, you could use something like Lockbox to store that NSDictionary to the keychain, using the provided setDictionary:forKey: interface.
UPDATE: To change values stored in that dictionary, you only have to pass by a NSMutableDictionary (that's the common way of doing):
NSMutableDictionary *mutableDict = [[LockBox dictionaryForKey:#"YourRefreshTokenDictionaryKey"] mutableCopy];
mutableDict[#"access_token"] = #"NewAccessToken";
[LockBox setDictionary:mutableDict forKey:#"YourRefreshTokenDictionaryKey"];
FYI, a NSMutableDictionary is a subclass of NSDictionary, so it's safe to save that back directly to the keychain!

Related

Edit NSUserDefaults Managed Configuration Dictionary

MDM Providers write to a dictionary which is retrievable using
NSUserDefaults.StandardUserDefaults.DictionaryForKey("com.apple.configuration.managed")
How can I programatically write key/value pairs to this dictionary?
Firstly retrieve the configuration dictionary:
NSMutableDictionary configDic = NSUserDefaults.StandardUserDefaults.DictionaryForKey("com.apple.configuration.managed").MutableCopy() as NSMutableDictionary;
Then you can modify this NSMutableDictionary like:
configDic["key1"] = (NSString)"value1";
At last write back to NSUserDefaults:
NSUserDefaults.StandardUserDefaults.SetValueForKey(configDic, (NSString)"com.apple.configuration.managed");
NSUserDefaults.StandardUserDefaults.Synchronize();
Because NSDictionary can't be modified after initialization, we need to convert it to NSMutableDictionary. If the value of configDic is type of NSDictionary(or NSArray), we also need to copy it deeply before we want to modify it.

How to prevent order changing in NSDictionary in ios

Hi In my application I have a login screen where I have to post credentials to C# server.The order which I used is as such below
username
password
domainname
Code:
NSDictionary*disPost=[[NSDictionary alloc]initWithObjectsAndKeys:#"naresh",#"UserName",#"n123",#"Password",#"naresh-in",#"DomainName",nil];
I can able to post the data to server successfully but the credentials order is changing like below.
Password
UserName
DomainName
Due to this reason I am getting an exceptional error from server. Please help me to resolve this issue as soon as possible.
NSDictionary has key-value pairs, it is not ordered. You need an array to keep the order of the elements. You could use 2 arrays (1 for keys the other for values) or use an array for keys (to know the order) and a dictionary for the key-value pairs.
NSDictionary is not an ordered container. If you really want an ordered dictionary then use OrderedDictionay from,
http://www.cocoawithlove.com/2008/12/ordereddictionary-subclassing-cocoa.html
Your question makes me think that its not NSDictionary that you want to use, because with dictionaries you really shouldn't care about the order.
And I think the glitch in your code is probably with the part that sends to the server, try sending them separately, or use another datatype than NSDictionary.
good luck!

Valueforkey in NSUserDefaults

I am new to IOS programming and am trying to modify some code a developer wrote for me. I'm having problems in the following code
NSUserDefaults *pref=[NSUserDefaults standardUserDefaults];
NSString *strUrl=[pref valueForKey:#"HistoryUrl"];
if (strUrl.length>0)
{
newUrl=strUrl;
}
else
{
newUrl=#"http://www.google.com";
}
The HistoryUrl parameter seems to have the value 'http://www.yahoo.com' stored in it. I've looked everywhere and searched the net on how to replace this value to google's address. I have even gone through all the code in XCode and can't find where historyurl is declared:
Where is HistoryUrl declared?
How can it be modified?
Thanks in advance!
#"HistoryUrl" is an NSString* containing the string HistoryURL. Thats' how you write an NSString* with fixed data.
pref is an object representing the user's preferences.
The user's preference contain multiple key - value pairs. For example there might be a key named "HistoryUrl" which might have some value.
The valueForKey: method reads the value that is stored under the key "HistoryUrl" and stores it into strUrl. If there is no key named "HistoryUrl" then the result will be nil. (The use of valueForKey: is strange, because it is not a method of NSUserDefaults itself; typically one would use objectForKey:)
The following code checks whether the value read has any characters in it (length is roughly speaking the number of characters); if there are any characters then newUrl is set to that value; if there were no characters then newUrl is set to the NSString* "http:/www.google.com".
So someone at some time has stored a value under the name "HistoryUrl" into the application's preference file. You remove that value by calling
[pref removeObjectForKey:#"HistoryUrl"]
Or, since you don't seem to want anything other than "google", remove all the code and just write NSString* newUrl = #"http://www.google.com" if that's what you want.
HistoryURL is an arbitrary key and it is being used in your code to retrieve a value from NSUserDefaults. At some stage in your code, you will want to use setObject:forKey: to update the value stored in NSUesrDefaults. You will also need to call synchronize to save the new value after it has been set.
Where is HistoryUrl declared?
It's not. #"HistoryUrl" is just a string. Read up on NSUserDefaults to learn how it all works, but in a nutshell the defaults system is like an associative array (also known as a dictionary or map), where you can key/value pairs. In this case, #"HistoryUrl" is the key, and #"http://www.yahoo.com" is the value. You can make up whatever keys you want for storing your values.
How can it be modified?
Do you want to modify the key, or the value associated with the key? If the former, just make up a different key and use it. If the latter, use the methods of NSUserDefaults to set a different value:
NSUserDefaults *pref=[NSUserDefaults standardUserDefaults];
[pref setObject:#"http://google.com" forKey:#"HistoryUrl"];
[pref synchronize];
Note: the -synchronize call isn't strictly necessary, as the system will generally write your change eventually. But a lot of people like to call it whenever they make a change to the defaults system.

Best way to store user information for my iOS app

What kind of database do you suggest? I want to store user email, username, password, and a couple other random pieces of information. It doesn't have to be fancy. Just a simple database. Are there any free options?
The user information needs to be stored in the keychain to keep it secure.
Any other information could be stored in any one of:
User defaults NSUserDefaults
File on disk (maybe a plist)
Database Core Data (technically just a file on disk)
Which you choose depends on what the data is, how much there is and what kind of access you need to it.
If your data is small and chosen by the user as some kind of setting then user defaults makes sense and is the lowest cost for you to implement.
To use a database, check out Core Data intro.
Wain is right but I think as you want to store small amount of data for further use, the most efficient ways is to use NSUserDefault.
NSUserDefault stores data in NSDictionary type things.
I think this is the step you have to take:
1- check if data exists. I mean if user selected the number if the last run of your app. So in viewDidLoad method:
NSMutableDictionary *userDefaultDataDictionary = [[[NSUserDefaults standardUserDefaults] dictionaryForKey:ALL_DATA_KEY] mutableCopy];
if (userDefaultDataDictionary) {
// so the dictionary exists, which means user has entered the number in previous app run
// and you can read it from the NSDictionaty:
if(userDefaultDataDictionary[LABLE_KEY]){
//and store it
}
}
2 - you can implement some method like syncronize to store data in NSUserDefault every time something has been changed.
- (void) synchronize
{
NSMutableDictionary *dictionaryForUserDefault = [[[NSUserDefaults standardUserDefaults] dictionaryForKey:ALL_DATA_KEY] mutableCopy];
if(!dictionaryForUserDefault)
dictionaryForUserDefault = [[NSMutableDictionary alloc] init];
dictionaryForUserDefault[LABLE_KEY] = //data you want to store
[[NSUserDefaults standardUserDefaults] setObject:dictionaryForUserDefault forKey:ALL_DATA_KEY];
[[NSUserDefaults standardUserDefaults] synchronize];
}
P.S. and don't forget to #define your keys for your dictionary:
#define LABLE_KEY #"Lables"
#define ALL_DATA_KEY #"AllData"
Store it in a plist. If you're talking about data pertaining to one or a few users, that's probably the easy thing. here is a simple example.
Since you say database, store in Sqlite. There's some provided stuff for it already in xcode.
The entire database is contained in one file, which can be moved around if you need to.
Here is some more information on how to use one in your app.

iOS: Proper way of using keychain as a generic mutable dictionary

I need a way to use keychain as a generic mutable dictionary, I've checked a few libraries:
KeychainItemWrapper: looks like one instance of the wrapper is just one key/value pair, as you need to use kSecAttr* for the keys, for a generic dictionary structure, you need to maintain a list of the wrappers, which is not easy.
PDKeychainBindings: this one does not require kSecAttr* as keys, you can you any string, but it does not provide a way to purge all keychain data, you need to know what key's you've used and remove them individually.
Is there any library that uses keychain as a generic mutable dictionary? Most importantly, has the ability to purge all data like removeAllObjects?
Thanks
Just have a single keychain entry and put a JSON string (human readable) or binary plist data (efficient if using NSPropertyListBinaryFormat_v1_0) as its contents.
http://developer.apple.com/library/ios/#documentation/Foundation/Reference/NSJSONSerialization_Class/Reference/Reference.html
https://developer.apple.com/library/ios/#documentation/cocoa/Reference/Foundation/Classes/NSPropertyListSerialization_Class/Reference/Reference.html

Resources