Skip to content

Instantly share code, notes, and snippets.

@brunow
Created August 26, 2020 15:43
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brunow/13d485999b73d00664ee474e4508bb61 to your computer and use it in GitHub Desktop.
Save brunow/13d485999b73d00664ee474e4508bb61 to your computer and use it in GitHub Desktop.
Donate a NSUserActivity and restore it with SwiftUI
import SwiftUI
struct UserActivityView: View {
@State private(set) var activityType: String? = nil
@State private(set) var currentActivity: NSUserActivity? = nil
var body: some View {
Text(activityType ?? "No activity restored yet")
.onContinueUserActivity("userActivity", perform: { userActivity in
self.activityType = userActivity.activityType
})
.onAppear(perform: {
donateActivity()
})
}
func donateActivity() {
self.currentActivity = NSUserActivity(activityType: "userActivity")
self.currentActivity?.title = "title"
self.currentActivity?.isEligibleForHandoff = true
self.currentActivity?.isEligibleForPrediction = true
self.currentActivity?.persistentIdentifier = UUID().uuidString
self.currentActivity?.becomeCurrent()
}
}
struct UserActivityView_Previews: PreviewProvider {
static var previews: some View {
UserActivityView()
}
}
@rafaelclaycon
Copy link

Hey! Thank you for this!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment