How to Share Keychain with other Apps in Swift 4.2 – KeychainSwift (Step by Step Tutorial)

Hi Alex here from and the welcome back to the Christmas Calendar tutorial series and in this lesson we are going to take it where we left off with Keychain. Go ahead and take a look at the previous video if you are new to this video or playlist or channel and then we can start off learning how you can share Keychain in swift 4.2. So let’s get started! Okay! Now that you know the basics of using Keychain Swift and again let me just give a huge shout out for this awesome GitHub repository and its owner you can find a link just right here or in the resources down below. Go ahead and click the link and you will be taken to all of my resources that I create on YouTube. Okay and of course it is free no-brainer there. Great! Now in the previous video we have learned how to set and get data strings and booleans Go ahead and take a look at it if you haven’t already done so and in this video we are going to talk about sharing Sharing our Keychain! Now first of all let me just open up the previous project and the one type of sharing and that is what we are going to discuss first is through iCloud. Now enabling Keychain sharing through our cloud is very very simple. All you have to do is in the SetViewController when you create your keychain you set it like this: KeychainSwift(keyPrefix) and so on Now just scroll all the way down to the viewDidLoad. Where is it here we go And in the viewDidLoad before you set any keychain go ahead and type in keychain.synchronizable = true. Aand that is all you have to do. This is so amazing and that’s why I love this library because now your keychain is synchronizable meaning that any other device of your user that is logged in with their iCloud can access this keychain. Now if keychain is allowed under iCloud. So that is in settings you tap on your user, well your of your Apple ID and then go under iCloud and keychain and set it to on. If you want to test it out. We are not going to test it out because it’s just one single line of code and it works. Now that is it for our iCloud and one caveat that we have discovered it doesn’t really work on MacOS. But hey we are working on iOS devices in this tutorial. Okay! Now another way of sharing our keychain… let me just open up my SharedKeychainSwift project here and of course go ahead and take a look at the link down below for the GitHub repository you may clone it and then as in the previous video go ahead and install the Podfile. Okay! Now another way of sharing it is going to Capabilities and you can see here we have Keychain Sharing. Okay for the synchronizable you don’t even have to enable iCloud or keychain sharing it works right over the bat. But what do we have to do if we want to share our Keychain between targets and between applications here is my other so here we go it’s my SharedKeychainSwiftOtherApp. What if you want to share the keychain between and these two? Well first of all let me show you how you can share your keychain between targets. Okay! What you need to do is first of all go ahead and take a look at the resources and you will find… let me just scroll all the way there… under SharedKeychainSwiftResources you will find SharedKeychainSwift. Now that is a simple file that I have created so go ahead and drag and drop it onto your project. And this

time make sure that the extension and this is an iMessage extension it is selected. We are adding this to target basically it is accessible through our extension too. Okay! Let’s click on finish and with this awesome short file you will be able to use Keychain Swift inside your extension and SharedKeychainSwift Now just to be sure I go ahead and install the Podfiles and you need to add TinyConstrainsand and KeychainSwift to both of the target. Okay, let me just do that. Open up Terminal here Let’s make it a little bigger. Let’s go back to our finder here. Let’s type in cd and a space and let me just go into the code here we go. Let’s drop it in there and hit return. Now we want to type in a pod init and hit return. Ohh not pod install because we already have a pod file. Okay, it is downloading installing and I have purposefully try to install because we will have this kind of warning. Now if you read through it basically it says that always embed Swift a standard libraries built setting is defined. So what you need to is use this flag under your target Go ahead and copy that out and go to your project: Target, Build Settings and I believe… let me just go back to Terminal: always embed Swift libraries. Okay! Always embed Swift standard libraries there we go and you will have an option here: other. Okay, go ahead and paste the inherited flag there and under extensions do others same. Okay and now let us just go back to our Terminal and do another pod install and this should get rid of the warning. There we go! We have got ridden of it. Okay this only happens when you have an extension. Great! Now let us just go to back to our target and what you want to do is go into Capabilities and first of all we need to let’s say make Xcode create our app ID on the Apple Developer Portal. And for that I usually enable In-App Purchases and then I will disable it after it has been created. Okay so let’s do that for both the app and the extension. Okay! Let’s just wait for that and we will see that it is created in the Apple Developer Portal in just a moment Now let us just enable keychain sharing and start with the main target, not the extension and the target. Okay! Let’s just scroll all the way down and set it to on Now here you have a Keychain Group and let me just copy this because what we want to do is set the same Keychain Group in the extension. Let’s select on here and replace the Keychain Group that has been generated. Okay! Basically… Let me just copy that out again command C and paste it inside here. Okay, let’s check out now if we do have those App ID’s enabled Let’s go to our developer portal go ahead and log in if you haven’t done that already Select account select certificates identifiers and profiles and then app IDs. And then you want to search for your app ID. And I believe it was… let me just go to my target general SharedKeychain. Okay, let’s go back SharedKeychain there we go. It has been

created. Now let’s just do that again What we are searching for here is that this prefix because we need to have this prefix because the actual SharedKeychain is generated with the prefix. And the generated SharedKeychainGroup that we have just created. Let’s copy this out and then go into our code and let us just create a Keychain. We are going to create it in the SetViewController and as you can see here we have basically we have text field where we will enter a string. And we will set it into keychain delete the name and we have a clear button for clearing the Keychain. What we want to do now is basically first of all let us just start the Keychains. In the viewDidLoad or rather yet let’s go into the AppDelegate. And before we create our view controller let me just type in a SharedKeychainSwift. start and as you can see we have two options if we select the second one we have more options if not that that’s a synchronizable is set to true by default. We are going to use the first one. And as you can see we have KeychainSwift and an accessGroup. Okay! Now let us just create a KeychainSwift Let’s import Keychain first: import KeychainSwift There we go! And we will not do this time with the prefix so you may see how that works. Okay, let’s just copy this out paste it in here. We have our access group and if you just jump right into the start definition you can see that the accessGroup is a string Now what is that accessGroup? Let’s go back and let’s create our accessGroup: let and now it’s time to paste that in Ohh I have already copied out something else Let’s go back to our chrome copy our prefix again and now it’s time to paste that in there. Hit a dot and then go to your Capabilities and just scroll all the way down and copy out your Keychain Group. Let’s go back and add it in there. So your prefix . and your Keychain Group. Okay! Let’s copy this accessGroup and paste it in there. Now why do we need this prefix? Because we have created a Keychain Group this entitlements file has been created and as you can see under Keychain Access Groups we have this string and here we have an app identifier prefix. That is why we need to add that in there and if you go to your extension again the extension entitlements has been created. And it is the same that is important. Now that is it for setting it up in the AppDelegate just three lines of code pretty easy Let’s go back to our SetViewController and let’s set into Keychain our text Okay! What you want to do here is type in SharedKeychainSwift and you can do all of the functions that are in the basic library. We can set our value and this will be our… I believe it is called nameTextField. Yes, nameTextField.text and of course that is an optional If not otherwise well let’s add in an empty string and for the key and I will add it here name. Now this is very very loosely written go ahead and take a look at the safe way I have written it in the previous video. That is that and let me just add in here that delete the clear functions too. SharedKeychainSwift.delete

and for the key and that is name. And SharedKeychainSwift.clear and that is it. We don’t need to have any keys here because we are clearing everything out. Okay! Now let us just go into the MessagesViewController under your extension and I have already created a nameLabel and a getFromKeychainsButton. This is quite familiar if you have taken a look at the previous video. What we want to do is getFromKeychains So: SharedKeychainSwift.get and we are going to get our string value for the key “name” again. These keys should be in enum, that I have discussed in the previous video. Okay, and we are going to set and this as the name as the text of our and name label. Okay! I will just select this: “let name = ” and then set the “nameLabel.text = name” and the name that we have just gotten from our Keychain Okay! Now that we have our name there is one last thing that we need to add here is that is that we need to create this keychain in the extension app too Let’s go to our AppDelegate and simply copy all of the starting 3 lines here And add it in down into our MessagesViewController into our viewDidLoad Okay, pick for the setupViews. That is there Okay, and now let us just go into our SharedKeyChainSwift Build and run Oh Keychain Swift isn’t there so let’s import it: “import KeychainSwift” hit command + R to build and run again Now let’s add our name in. Okay! Here we go! Alex. set. Great! And now let’s stop this and go to our extension and build and run there. And when we hit that Get button we should get our name in the label. Okay, let’s just wait for it to initialize and there we go! We hit the Get button and the name is Alex. Now that is pretty cool! Okay, one last thing I want to show you and we will finish off this tutorial with that is now that we have set the name into this app I want to share it with another app. And here we go! Go ahead and download the SharedKeychainSwiftOtherApp from the GitHub repository. Let me resize this. Okay! What you need to do is basically do the same. Go into your AppDelegate and add the initialization. There we go! Make sure the accessGroup is the same. And then go to and of course you need to import KeychainSwift. Let’s hit command B and we see that everything builds. Oh we need to add the SharedKeychainSwift one tiny but more important detail. Let’s just go all the way to our projects and add it in and there. Okay, let’s finish that. Let it command B and build has succeeded. Great! Now we want to get our keychain string so we will do that the same as we did in the extension Let me just copy this out and paste it inside here. Great! Now let us just build and run and see if everything worked okay. Now let’s hit Get maybe it needs an initialization. Oh sorry

I just forgot and most probably you already noticed that I need to set our in-app purchases. There we go! And enable Keychain Sharing and set the keychain to the same as it was in the basic app. Great and let’s see if this already has been taken in effect let’s check if we have the new key there. Yes, we do that this other app. And then let’s go back to our code and get it. Okay! There we go! The name is Alex! And that is as simple as that Go ahead and uncheck all of the in-app purchases now and I will do that. Go into your target Capabilities uncheck in-app purchases. This was only for Xcode to realize that hey we need to create our app ID in the Apple Developer Portal That is it for Keychain Swift and how to handle Keychain in separate targets and separate apps. Okay! I hope you enjoyed this one! If you did go ahead and give it a thumbs up and subscribe to the channel and I dare you to hit that notification bell Now it is your turn go ahead and comment down below how do you use keychain in your apps? And that is it for me today and I will see you in the next one!