Skip to content

Instantly share code, notes, and snippets.

@samsonjs
Created July 2, 2024 21:22
Show Gist options
  • Save samsonjs/cea3546f94aa99c2f90e282b33c54100 to your computer and use it in GitHub Desktop.
Save samsonjs/cea3546f94aa99c2f90e282b33c54100 to your computer and use it in GitHub Desktop.
How to fix a Swift 6 warning: "Sending main actor-isolated value of type 'WebAuthenticationSession' with later accesses to nonisolated context risks causing data races; this is an error in the Swift 6 language mode"
// https://iosdev.space/@andy/112714337582288785
import AuthenticationServices
import SwiftUI
struct ContentView: View {
@State var url = URL(string: "https://example.net")!
@Environment(\.webAuthenticationSession) var webAuthenticationSession
var body: some View {
VStack {
Button {
let session = webAuthenticationSession // copy before entering an isolated context
Task {
let token = try await session.authenticate(
using: url,
callback: .https(host: "example.net", path: "/whatever"),
preferredBrowserSession: nil,
additionalHeaderFields: [:]
)
print("token: \(token)")
}
} label: {
Text("Authenticate")
}
}
.padding()
}
}
@russell-archer
Copy link

Thank you, this really helped me!

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