> ## Documentation Index
> Fetch the complete documentation index at: https://docs.portalhq.io/llms.txt
> Use this file to discover all available pages before exploring further.

# loginWithApple

> Returns the Sign in with Apple authorize URL for you to open in a browser of your choice. Does not open the browser itself.

**Function Signature**

```swift theme={null}
public func loginWithApple() async throws -> AuthorizeUrlResult
```

A `PortalAuth` method. Fetches a fresh authorize URL from Portal and returns it; your app opens it (for example with `UIApplication.shared.open`) and later forwards the redirect to [`handleRedirect`](./portalauthhandleredirect). For a flow that also owns the browser, use [`signInWithApple`](./portalauthsigninwithapple).

**Returns**

* **`AuthorizeUrlResult`**: Contains the following:
  * **`authorizeUrl`**: `String`, the Apple authorize URL, trimmed of surrounding whitespace.

**Throws**

* `PortalAuthError.authMethodUnavailable(.apple)`: Apple is not enabled for this auth environment. Not retryable; check `getMethods()`.
* `PortalAuthError.accountAbstractionUnavailable(message:)`: `isAccountAbstracted: true` was requested but gas sponsorship is not enabled or configured.
* `PortalRequestsError.unauthorized`: The `redirectUrl` is not on the environment's allow list, the Auth Environment ID does not match an environment with authentication switched on, or an enabled provider is missing its credentials in the dashboard. That last case fails the call for every provider, not only the misconfigured one.
* Other transport errors unchanged.

**Notes**

* Call it on every tap and **never cache the result**. The URL embeds a single-use `state` shared by both provider URLs in one backend response, so a stale URL fails at the provider with no useful message.
* The redirect arrives through the OS, like a magic link, and works with an `https://` Universal Link redirect; see [Register your redirect](../guide/client-auth#register-your-redirect).
* Apple posts the result to Portal's callback URL (`https://api.portalhq.io/api/v3/auth/oauth/callback/<AUTH_ENVIRONMENT_ID>`), which you register as a Return URL on your Services ID. Portal then redirects to your `redirectUrl`, so your app only ever sees the final redirect.
* Portal identifies an end user by email. A user who picks **Hide My Email** becomes a distinct end user, with a distinct wallet, from the same person signing in with Google or a magic link. Apple also supplies the email only on the user's first consent for a given Services ID, so changing the Services ID is a breaking change for existing users. See [Apple OAuth](../../../resources/authentication/apple-oauth#how-apple-handles-email-addresses).
* The button that starts the flow is your own control. Follow Apple's Human Interface Guidelines for the Sign in with Apple button.
* If the user cancels at Apple, Portal redirects to your `redirectUrl` with `?error=oauth_failed`, which `handleRedirect` throws as `PortalAuthError.authenticationFailed(error:)`.
* Available starting from SDK version 8.0.0.

**Example Usage**

```swift theme={null}
import UIKit
import PortalSwift

func openAppleInBrowser() async {
    do {
        let authorize = try await auth.loginWithApple()

        guard let url = URL(string: authorize.authorizeUrl) else {
            print("The authorize URL could not be parsed.")
            return
        }
        await UIApplication.shared.open(url)
        // Forward the redirect that comes back to auth.handleRedirect(url).
    } catch PortalAuthError.authMethodUnavailable(let method) {
        print("\(method.rawValue) is not enabled for this auth environment.")
    } catch {
        print("Error starting Apple sign-in: \(error)")
    }
}
```

**Related Documentation**

* [signInWithApple reference](./portalauthsigninwithapple)
* [handleRedirect reference](./portalauthhandleredirect)
* [Google or Apple in your own browser](../guide/client-auth#google-or-apple-in-your-own-browser)
* [Apple OAuth](../../../resources/authentication/apple-oauth)
