Created
July 2, 2024 21:22
-
-
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"
This file contains 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
// 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() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you, this really helped me!