In ASWebAuthenticationSession, what does prefersEphemeralWebBrowserSession change, when must you set it, and what does it cost your users?
answer
- the browser's existing data
- every cookie except session cookies
- false by default, set before start()
- no reuse of an earlier login
- let the member choose
basics
~20 sSetting prefersEphemeralWebBrowserSession to true before start() asks the browser neither to use the member's existing browsing data nor to keep what this sign-in creates, so each sign-in starts clean and the member must type credentials again.
solid answer
~50 s`prefersEphemeralWebBrowserSession` is `false` by default. Then the session's browser can use the member's normal browsing data for the site, which Apple describes as all cookies except session cookies, so a member already logged in to the studio portal can finish with one tap, and what the attempt stores stays for next time. Setting it to `true` asks the browser to use none of that data and to keep nothing from this attempt or share it with any other session. Set it before `start()`; afterwards it has no effect. Safari always honours it; on macOS a different default browser may not. The cost is reuse: an earlier successful login cannot help, so the member re-enters credentials every time. Apple's guidance is to let the user choose, for example a "Use a shared device" switch on the sign-in screen.
code
swift · 19 linesimport AuthenticationServices
import UIKit
@available(iOS 17.4, *)
@MainActor
func startPortalSignIn(
authorizeURL: URL,
signInOnSharedDevice: Bool,
anchor: any ASWebAuthenticationPresentationContextProviding,
completion: @escaping ASWebAuthenticationSession.CompletionHandler
) -> ASWebAuthenticationSession {
let session = ASWebAuthenticationSession(
url: authorizeURL, callback: .customScheme("yogastudio"), completionHandler: completion)
// Must be set before start(); setting it afterwards has no effect.
session.prefersEphemeralWebBrowserSession = signInOnSharedDevice
session.presentationContextProvider = anchor
session.start()
return session // keep it to call cancel() if the screen goes away
}go deeper
Recall the default value, that the property must be set before start(), and that true means the browser neither uses nor keeps browsing data.
Explain which cookies a default session can use, why a local logout does not end the portal login, and what the member loses with an ephemeral session.
Choose per context, such as personal phone versus shared device, and design account switching and logout around a value that is fixed at start().
Treat the flag as a product setting with a security and friction cost, and decide whether members or the team pick it, per device or per sign-in.
## What the property controls `ASWebAuthenticationSession` runs a web sign-in in a browser view the system presents. That browser has a **normal browsing session**: cookies and other data the member built up while using the site in Safari. The Boolean **`prefersEphemeralWebBrowserSession`** (iOS 13 and later) decides whether this one sign-in is allowed to touch it. The name is a request, not a command: the session *asks* the browser for an ephemeral session. On iOS the browser is a secure, embedded web view, and Apple states that Safari always respects the request. On macOS the user can pick another default browser, which might or might not. ## The default: shared browser data With the property left at **`false`**: - The browser can use the member's existing data for the portal. Apple's note is precise: **all cookies except session cookies** are available. A login kept only in a session cookie is therefore not reused. - A member who is already signed in to the portal can finish after the system alert without typing anything. - Whatever the portal stores during the sign-in stays in the normal browsing session, ready for the next sign-in and for Safari. That last point is the double edge. If the app signs the member out locally, the portal's own login can still be there, so the next attempt may complete without a password prompt. ## Ephemeral: a clean, disposable session With the property set to **`true`**, the browser is asked to: - avoid using any existing browsing data, such as cookies, during the attempt; - avoid keeping anything collected during the attempt beyond its lifetime; - avoid sharing that data with any other session. The flag governs only this session. Nothing in the API removes cookies that an earlier, non-ephemeral session left in the normal browser data; they are simply not used. | | `false` (default) | `true` | |---|---|---| | Existing portal login | can be reused | ignored | | Data from this sign-in | kept for later sessions and Safari | discarded after the attempt | | Typing credentials | often skipped | every time | | Fits | a member's own phone | a shared or front-desk device | ## Timing rules 1. Create the session. 2. Set `prefersEphemeralWebBrowserSession`, and `presentationContextProvider`, **before** calling `start()`. Apple's header says that setting the property after `start()` has no effect. 3. Call `start()`. Because the value is fixed when the session starts, an app that offers a choice reads the member's preference and creates a fresh session with the matching value each time. ## What it costs and how to choose Apple's own guidance is that an ephemeral session "may improve security but prevents reusing the result of a previously successful authentication, potentially forcing the user to reenter credentials", and that it is typically best to **let the user choose**. In practice: - On a member's personal phone, the default gives the smoother sign-in, especially when the studio portal also serves the website they use in Safari. - On an iPad at the studio's front desk, several people sign in on one device. Ephemeral sessions stop one member's portal login from carrying over to the next. - Switching accounts is easier with an ephemeral attempt, because no existing portal login steers the page. - If you keep the default but need a full logout, the app has to end the portal's own login too, typically by sending the member through the provider's logout page; the local logout alone does not touch browser data. ## Common mistakes - **Setting the flag in a completion handler or after `start()`.** It is ignored; the value has to be on the session before it starts. - **Expecting it to log the member out of the portal.** It only stops this attempt from using or keeping data; it never clears what a previous shared session stored. - **Turning it on everywhere to "be secure".** Members on their own phones then type a password every time, and an app that signs in often feels broken. - **Relying on a session cookie for one-tap reuse.** Session cookies are excluded from a shared session, so the portal needs a persistent login cookie if reuse is the goal. ## Related controls The SwiftUI wrapper, the `webAuthenticationSession` environment value, expresses the same choice through its `preferredBrowserSession` parameter with a `.ephemeral` value. Cross-platform libraries expose their own flag for it, and those are covered with the React Native sign-in material rather than here.
- Does an ephemeral session delete the portal cookies that an earlier normal session left behind?No. The property asks the browser not to use existing data during this attempt and not to keep its own data afterwards. It does not clear the normal browsing data, so a later non-ephemeral session can still find the old login.
- The portal keeps its login only in a session cookie. Will a non-ephemeral ASWebAuthenticationSession reuse it?No. Apple notes that when the session is not ephemeral, all cookies except session cookies are available to the browser. A login that lives only in a session cookie does not carry into the authentication session.
- Can you switch a running session to ephemeral when the member taps "Use another account"?No. The property has no effect once `start()` has been called. Cancel the running session with `cancel()` and start a new one with `prefersEphemeralWebBrowserSession` set to `true`.
An ephemeral session is like opening a private browsing window just to log in: nothing from your usual window comes in, nothing from the private one stays, and your usual window is left exactly as it was.
- Setting prefersEphemeralWebBrowserSession deletes the member's Safari cookies for the portal.
- You can flip prefersEphemeralWebBrowserSession after start() to drop cookies mid-flow.
- Sessions are ephemeral by default, so they never reuse Safari's login.
- A non-ephemeral session shares every cookie, session cookies included.
- Ephemeral sessions cost nothing, so every app should turn them on.