If you want to add the widget to your existing iOS app and have access to the storage, you are in the right place. I will quickly show you how below.
First of all, you will need to add Keychain Group and App Group in both your app and widget target’s Build Settings under Signing & Capabilities. If you want to know their differences, you can read up on this documentation by Apple. Moreover, naming them can be based on your app bundle identifier. Here is an example:
- App Bundle Identifier: com.example.yungkien.com
- App Group: group.com.example.yungkien.com.shared
- Keychain Group: com.example.yungkien.com.shared
Then, you can start to integrate the app group and keychain group into your code. In this example, I am going to use a third-party library called KeychainAccess to interact with the keychain access. Here is my sample code:
You can add both your app and your widget to the file’s Target Membership so that they can both use it to save and load from the keychain storage.