ASWebAuthenticationSession not working in Preview

class ViewModel : NSObject, ObservableObject, ASWebAuthenticationPresentationContextProviding {

private var authSession: ASWebAuthenticationSession?

  func signInWithOpenID(provider: OAuthProvider) {
        let url = getOIDCAuthenticationURL(provider: provider)
        
        authSession?.cancel()
        authSession = nil
        
        authSession = ASWebAuthenticationSession(url: url, callbackURLScheme: "com.ninjanutri") { callbackURL, error in
            if let error = error {
                print("Error: \(error.localizedDescription)")
                return
            }
            
            guard let callbackURL = callbackURL else { return }
            
            guard let idToken = callbackURL.valueOf("id_token") else { return }
            
            self.signInWithIdToken(provider: provider, idToken: idToken)
        }
        
        authSession?.prefersEphemeralWebBrowserSession = false
        authSession?.presentationContextProvider = self
        authSession?.start()
    }

  public func presentationAnchor(for session: ASWebAuthenticationSession) -> ASPresentationAnchor {        
        return ASPresentationAnchor()
    }
}
struct ContentView: View {
  @StateObject private var viewModel = ViewModel()

  var body: some View {
     Button {
      viewModel.signInWithOpenID(provider: .github)
     } label: {
        Text("Test")
     }
  }
}

when the prefersEphemeralWebBrowserSession is false, the alert and webview is totally working fine in Simulator and Real device, but not XCode Preview. Is this behaviour expected or it's a bug?

Hi,

Sorry to hear you are having problems getting previews working with ASWebAuthenticationSession. This sounds like an infrastructure issue in how previews interacts with the rest of the operating system. The best next step will be to file a feedback with diagnostics so we can take a look.

Install the logging profile using instructions available here: https://developer.apple.com/bug-reporting/profiles-and-logs/?name=swift On your mac running Xcode, and on your physical preview device (if you are using one).

Install the logging profile using the following instructions on your mac running Xcode; and if you are using one, your physical preview device (iOS or visionOS): https://developer.apple.com/bug-reporting/profiles-and-logs/?name=swift

Then when you reproduce the problem in Xcode:

  1. Either (a) an error banner will appear, click the "Diagnostics" button in that banner; or (b) if you're not seeing an error but you still want to provide diagnostics you can get the same diagnostics window by going under the Editor menu in the menu bar, then selecting the Canvas submenu, then selecting "Diagnostics".
  2. In the sheet that appears, click "Generate Report" in the bottom left of the sheet
  3. Attach (or make from the folder) the resulting zip file to the bug (will be named something like previews-diagnostics-0123456789.zip)
  4. Generate a sysdiagnose on your mac and any on-device preview devices, and attach those too

As a workaround for now you might want to try using on-device previews. By having it render and use your phone those missing sheets and alerts should show up just fine on the actual hardware.

ASWebAuthenticationSession not working in Preview
 
 
Q