Created
August 26, 2020 15:43
-
-
Save brunow/13d485999b73d00664ee474e4508bb61 to your computer and use it in GitHub Desktop.
Donate a NSUserActivity and restore it with SwiftUI
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hey! Thank you for this!