Thursday, May 26, 2016

Storing more entries in Keychain

Recently i got a requirement that involves storing more than one entry in the keychain.Apparently keychain accepts one entry for the application.

We know keychain in iOS are unique to each application.And we store as "Key-value" combination in keychain.Both keys and values can be strings.Now the problem is what if there is a need to store more than one entry in keychain for the same application.

Solution is Create a mutable dictionary and add "key-value" entries in the same dictionary each for entry you wanted to save in keychain.

For instance:

I want to save username,password,last login time in the keychain.Then create a dictionary called "User details" and going to add "username":"xyz" ,"Password":"1234","Last login time":"12:34:45pm"

Our dictionary "User details" looks like
username   xyz
password 1234
last login time 12:34:45

Then add this entire "User details" dictionary in keychain like below:

Code excerpts are in Xamarin.But it gives you idea fairly about

//entry in dictionary
            keyItemsDict.SetValueForKey (NSObject.FromObject(value),(NSString)key);
            keyItemsDict.SetValueForKey (NSObject.FromObject(Convert.ToBase64String(value)),keyStr);

            NSData dicDt = NSJsonSerialization.Serialize (keyItemsDict,0,out error);
//keyItemsDict is a dictionary            var s = new SecRecord (SecKind.GenericPassword) {
                ValueData =dicDt,
                Generic = NSData.FromString ("KeyChain")
            };

            SecKeyChain.Add (s);  //add in keychain

Wednesday, May 11, 2016

What happens when installing ipa and launching the app from springboard

When we build the app for release what happens really is it creates a directory which contains all the things our app needs.
Once ipa is generated,right click the app file (not the Dipa file) and select show package contents.Besides normal project files and resources you will be able to see other two things:

  1. Provisioning file
  2. Code signature directory-Inside it there is a file called "Code resources", a plist file which has cryptographic hashes for all the files in the solution.
When installing the ipa,it checks whether the provisioning file is actually signed from apple and compares each one of the hashes in the code resources against all the real files to verify whether its modified since the build.If anything fails,app won't be installed.

And then when you launch the app,it checks the app has not been modified and you still have provision file for the app to run.If any problem arises in these steps,app will crash.

For further reading on this topic,where to go from here? Check out Demystifying iOS certificates and provisioning file