Skip to content

Commit dc9b5e4

Browse files
yaroluchkojosefaidtharsh62
authored
feat(swift): Documentation for shared keychain (#7890)
* feat(swift): Documentation for shared keychain * Remove stray space in shared keychain documentation Co-authored-by: josef <[email protected]> * adding team id details * adding the inline filter back that was accidentally removed * Update src/pages/[platform]/build-a-backend/auth/advanced-workflows/index.mdx Co-authored-by: josef <[email protected]> * Update src/pages/[platform]/build-a-backend/auth/advanced-workflows/index.mdx Co-authored-by: josef <[email protected]> * Update src/pages/[platform]/build-a-backend/auth/advanced-workflows/index.mdx Co-authored-by: josef <[email protected]> * Update src/pages/[platform]/build-a-backend/auth/advanced-workflows/index.mdx Co-authored-by: josef <[email protected]> --------- Co-authored-by: josef <[email protected]> Co-authored-by: Harshdeep Singh <[email protected]>
1 parent 9df50a9 commit dc9b5e4

File tree

1 file changed

+71
-0
lines changed
  • src/pages/[platform]/build-a-backend/auth/advanced-workflows

1 file changed

+71
-0
lines changed

src/pages/[platform]/build-a-backend/auth/advanced-workflows/index.mdx

+71
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,77 @@ func federateToIdentityPoolsUsingCustomIdentityId() async throws {
375375
}
376376
```
377377

378+
## Keychain Sharing
379+
380+
### Migrating to a Shared Keychain
381+
382+
To use a shared keychain:
383+
384+
1. In Xcode, go to Project Settings → Your Target → Signing & Capabilities
385+
2. Select +Capability
386+
3. Add Keychain Sharing capability
387+
4. Add a keychain group
388+
5. Repeat for all apps for which you want to share auth state, adding the same keychain group for all of them
389+
390+
To move to the shared keychain using this new keychain access group, specify the `accessGroup` parameter when instantiating the `AWSCognitoAuthPlugin`. If a user is currently signed in, they will be signed out when first using the access group:
391+
392+
```swift
393+
let accessGroup = AccessGroup(name: "\(teamID)com.example.sharedItems")
394+
let secureStoragePreferences = AWSCognitoSecureStoragePreferences(
395+
accessGroup: accessGroup)
396+
try Amplify.add(
397+
plugin: AWSCognitoAuthPlugin(
398+
secureStoragePreferences: secureStoragePreferences))
399+
try Amplify.configure()
400+
```
401+
402+
If you would prefer the user session to be migrated (which will allow the user to continue to be signed in), then specify the `migrateKeychainItemsOfUserSession` boolean in the AccessGroup to be true like so:
403+
404+
```swift
405+
let accessGroup = AccessGroup(
406+
name: "\(teamID)com.example.sharedItems",
407+
migrateKeychainItemsOfUserSession: true)
408+
let secureStoragePreferences = AWSCognitoSecureStoragePreferences(
409+
accessGroup: accessGroup)
410+
try Amplify.add(
411+
plugin: AWSCognitoAuthPlugin(
412+
secureStoragePreferences: secureStoragePreferences))
413+
try Amplify.configure()
414+
```
415+
416+
Sign in a user with any sign-in method within one app that uses this access group. After reloading another app that uses this access group, the user will be signed in. Likewise, signing out of one app will sign out the other app after reloading it.
417+
418+
### Migrating to another Shared Keychain
419+
420+
To move to a different access group, update the name parameter of the AccessGroup to be the new access group. Set `migrateKeychainItemsOfUserSession` to `true` to migrate an existing user session under the previously used access group.
421+
422+
### Migrating from a Shared Keychain
423+
424+
If you'd like to stop sharing state between this app and other apps, you can set the access group to be `AccessGroup.none` or `AccessGroup.none(migrateKeychainItemsOfUserSession: true)` if you'd like the session to be migrated.
425+
426+
### Retrieving Team ID
427+
428+
First, ensure your Info.plist has the `AppIdentifierPrefix` key:
429+
430+
```xml title="Info.plist"
431+
<?xml version="1.0" encoding="UTF-8"?>
432+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
433+
<plist version="1.0">
434+
<dict>
435+
<key>AppIdentifierPrefix</key>
436+
<string>$(AppIdentifierPrefix)</string>
437+
</dict>
438+
</plist>
439+
```
440+
441+
Then, you can retrieve the team ID from your Info.plist:
442+
443+
```swift
444+
guard let teamID = Bundle.main.infoDictionary?["AppIdentifierPrefix"] as? String else {
445+
fatalError("AppIdentifierPrefix key not found in Info.plist")
446+
}
447+
```
448+
378449
</InlineFilter>
379450
<InlineFilter filters={['javascript','react-native','angular','nextjs','react','vue']}>
380451
## Subscribing to Events

0 commit comments

Comments
 (0)