Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PoC] cloud synced anonymous ids #3806

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft

Conversation

aboedo
Copy link
Member

@aboedo aboedo commented Apr 11, 2024

This allows an app that doesn't have an account system to automatically sync anonymous IDs.

This is helpful in a few ways:

  • it allows automatic restores on different devices under the same iCloud account
  • it prevents sharing app store accounts to attach them to anonymous user IDs and share purchases

This is achieved by using NSUbiquituousKeyValueStorage, which syncs data through iCloud. Each app only gets 1mb worth of data, but we're only storing a single string.

Missing:

  • tests (tested on device and it works as expected though)
  • watchOS compatibility (watchOS only supports key value storage starting on watchOS 9.0)
  • a delegate to inform developer when an app store account change is detected and the user is logged out as a result

@aboedo aboedo self-assigned this Apr 11, 2024
@aboedo aboedo added the feat A new feature label Apr 11, 2024
private let store = NSUbiquitousKeyValueStore.default
private enum Constants {
static let keyValueStoreKey: String = "appUserID"
static let appUserIDPrefix: String = "$RCCloudAnonymousID:"
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

different prefix, so that we can still easily detect it, but the backend doesn't treat it as a regular anonymous user id (otherwise we'd be back to square one)

Comment on lines +123 to +124
if let reason = userInfo[NSUbiquitousKeyValueStoreChangeReasonKey] as? NSNumber {
if reason.intValue == NSUbiquitousKeyValueStoreAccountChange {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the trickiest part, since this notification could be triggered by either another device logging in / out, or you switching store accounts.

Not entirely sure how to solve it, I suppose one way would be to somewhat mix this in with a value that's unique per device... that is, if we do want to solve it.

It's kinda cool in that it allows you to have the user logged in on multiple devices at once, which if you're not using an accounts system, would be nice

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

btw, I tested this across two devices, and it's pretty neat that when you log out on one it logs out the other and they both change to the same appUserID

NotificationCenter.default.addObserver(
self,
selector: #selector(self.ubiquitousKeyValueStoreDidChange),
name: NSUbiquitousKeyValueStore.didChangeExternallyNotification,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we only subscribe to this if in cloudSyncedAnonymousIDMode? Maybel also the applicationWillEnterForegroundNotification subscribe since right now it's only for this use case as well

Comment on lines +97 to +98
logOut() called with cloud synced appUserIDs enabled. The current user will be logged out, and
a new appUserID will be generated.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is actually the same that happens in a normal logout right? Maybe we should mention that the iCloud user id has been reset?

Copy link
Member Author

@aboedo aboedo Apr 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oops, probably a copy/pasted string that I meant to update

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feat A new feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants