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

> Auth0.swift のインストール方法と使い始め方

# Auth0.swift

<Card title="概要">
  iOS、macOS、tvOS、watchOS、visionOS 向けの Swift SDK です。Auth0 をアプリにシームレスに統合し、ログインとログアウトの追加、資格情報の安全な保存、ユーザー情報へのアクセスを実現できます。

  **詳細は** [**GitHub repository**](https://github.com/auth0/Auth0.swift)

  **v2 から移行する場合は、** [**Migration Guide**](https://github.com/auth0/Auth0.swift/blob/master/V3_MIGRATION_GUIDE.md) **をご確認ください**
</Card>

<div id="documentation">
  ## ドキュメント
</div>

* [Quickstart](/docs/ja-jp/quickstart/native/ios-swift/interactive): Auth0.swift を iOS / macOS アプリにゼロから組み込む方法を説明します。
* [サンプルアプリ](https://github.com/auth0-samples/auth0-ios-swift-sample/tree/master/Sample-01): 実際に試せる、完全に動作する iOS / macOS アプリです。
* [使用例](https://github.com/auth0/Auth0.swift/blob/master/EXAMPLES.md): 主要な機能の使い方を説明しています。
* [API ドキュメント](https://auth0.github.io/Auth0.swift/documentation/auth0): コードコメントから自動生成されたドキュメントで、利用可能なすべての機能を説明しています。

  * [Web Auth](https://auth0.github.io/Auth0.swift/documentation/auth0/webauth)
  * [資格情報マネージャー](https://auth0.github.io/Auth0.swift/documentation/auth0/credentialsmanager)
  * [Authentication API クライアント](https://auth0.github.io/Auth0.swift/documentation/auth0/authentication)
  * [MFA API クライアント](https://auth0.github.io/Auth0.swift/documentation/auth0/mfaclient)
* [FAQ](https://github.com/auth0/Auth0.swift/blob/master/FAQ.md): Auth0.swift に関するよくある質問にお答えします。
* [Auth0 ドキュメント:](https://auth0.com/docs) ドキュメントサイトで、Auth0 の詳細をご確認ください。

<div id="getting-started">
  ## はじめに
</div>

<div id="requirements">
  ### 要件
</div>

* iOS 14.0+ / macOS 11.0+ / tvOS 14.0+ / watchOS 7.0+ / visionOS 1.0+
* Xcode 16.x
* Swift 6.0+

<Warning>
  Xcode、Swift、各プラットフォームのバージョンのサポート終了が**破壊的変更**と見なされない時期については、[サポートポリシー](https://github.com/auth0/Auth0.swift/tree/master#support-policy)を確認してください。
</Warning>

<div id="installation">
  ### インストール
</div>

<div id="swift-package-manager">
  #### Swift Package Manager
</div>

Xcodeで次のメニュー項目を開きます。

**File > Add Packages...**

**Search or Enter Package URL** 検索ボックスに、このURLを入力します。

```http lines theme={null}
https://github.com/auth0/Auth0.swift
```

次に、依存関係ルールを選択して、**パッケージを追加** をクリックします。

<div id="cocoapods">
  #### Cocoapods
</div>

次の行を`Podfile`に追加してください:

```bash lines theme={null}
pod 'Auth0', '~> 3.0'
```

次に、`pod install`を実行します。

<div id="carthage">
  #### Carthage
</div>

`Cartfile`に次の行を追加してください。

```bash lines theme={null}
github "auth0/Auth0.swift" ~> 3.0
```

次に、`carthage bootstrap --use-xcframeworks`を実行します。

<div id="configure-the-sdk">
  ### SDK を設定する
</div>

[Auth0 Dashboard](https://manage.auth0.com/#/applications/) に移動し、新しい **Native** アプリケーションを作成します。

Auth0.swift が Auth0 と通信するには、Auth0 アプリケーションの **<Tooltip tip="Client ID: Auth0 から登録済みリソースに付与される識別子です。" cta="用語集を見る" href="/docs/ja-jp/glossary?term=Client+ID">Client ID</Tooltip>** と **Domain** が必要です。これらの情報は、Auth0 アプリケーションの設定ページで確認できます。[カスタムドメイン](/docs/ja-jp/customize/custom-domains) を使用している場合は、設定ページの値ではなく、<Tooltip tip="Custom Domain: 特別な、または独自の名前を持つサードパーティのドメイン。" cta="用語集を見る" href="/docs/ja-jp/glossary?term=custom+domain">カスタムドメイン</Tooltip> の値を使用してください。

<div id="configure-client-id-and-domain-with-a-plist">
  #### plist を使用して Client ID と Domain を設定する
</div>

アプリバンドル内に `Auth0.plist` という名前の `plist` ファイルを作成し、次の内容を追加します。

```xml lines theme={null}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>ClientId</key>
    <string>{yourAuth0ClientId}</string>
    <key>Domain</key>
    <string>{yourAuth0Domain}</string>
</dict>
</plist>
```

<div id="configure-client-id-and-domain-programmatically">
  #### Client ID と Domain をコードで設定する
</div>

<div id="for-web-auth">
  ##### Web Auth の場合
</div>

```swift lines theme={null}
Auth0
    .webAuth(clientId: "{yourAuth0ClientID}", domain: "{yourAuth0Domain}")
    // ...
```

<div id="for-the-authentication-api-client">
  ##### Authentication API クライアントの場合
</div>

```swift lines theme={null}
Auth0
    .authentication(clientId: "{yourAuth0ClientID}", domain: "{yourAuth0Domain}")
    // ...
```

<div id="configure-web-auth-ios-macos">
  ### Web Authを設定する (iOS / macOS)
</div>

<div id="configure-callback-url-and-logout-url">
  #### コールバック URL とログアウト URL を設定する
</div>

コールバック URL とログアウト URL は、Auth0 がアプリケーションへリダイレクトする際に使用する URL です。Auth0 はユーザーの認証後にコールバック URL を呼び出し、<Tooltip tip="セッション Cookie: 存在すると、ユーザーが認証済みと見なされるようにするエンティティ。" cta="用語集を見る" href="/docs/ja-jp/glossary?term=session+cookie">セッション Cookie</Tooltip> を削除した後にログアウト URL を呼び出します。

コールバック URL とログアウト URL は改ざんされる可能性があるため、Auth0 アプリケーションの設定ページにある **Allowed Callback URLs** と **Allowed Logout URLs** の各フィールドに URL を追加する必要があります。これにより、Auth0 はこれらの URL を有効なものとして認識できるようになります。コールバック URL とログアウト URL が設定されていないと、ユーザーはアプリケーションにログインおよびログアウトできず、エラーが発生します。

[Auth0 アプリケーション](https://manage.auth0.com/#/applications/) の設定ページに移動し、アプリケーションのプラットフォームに応じて、対応する URL を **Allowed Callback URLs** と **Allowed Logout URLs** に追加します。[カスタムドメイン](/docs/ja-jp/customize/custom-domains) を使用している場合は、`{yourAuth0Domain}` を設定ページの値ではなく、カスタムドメインの値に置き換えてください。

<div id="ios">
  ##### iOS
</div>

```swift wrap lines theme={null}
{yourBundleIdentifier}://{yourAuth0Domain}/ios/{yourBundleIdentifier}/callback
```

<div id="macos">
  ##### macOS
</div>

```swift wrap lines theme={null}
{yourBundleIdentifier}://{yourAuth0Domain}/macos/{yourBundleIdentifier}/callback
```

たとえば、iOS のバンドル識別子が `com.example.MyApp` で、Auth0 Domain が `example.us.auth0.com` であれば、この値は次のようになります。

```text wrap lines theme={null}
com.example.MyApp://example.us.auth0.com/ios/com.example.MyApp/callback
```

<Warning>
  [**トークンエンドポイント認証方法**](/docs/ja-jp/get-started/applications/confidential-and-public-applications) が `None` に設定されていることを確認してください。
</Warning>

<div id="configure-custom-url-scheme">
  #### カスタム URL スキームを設定する
</div>

Xcode で、アプリのターゲット設定にある **Info** タブを開きます。**URL Types** セクションで **＋** ボタンをクリックし、新しい項目を追加します。そこで、**Identifier** フィールドに `auth0` を、**URL Schemes** フィールドに `$(PRODUCT_BUNDLE_IDENTIFIER)` を入力します。

これにより、バンドル識別子がカスタム URL スキームとして登録され、コールバック URL やログアウト URL からアプリを開けるようになります。

<div id="web-auth-login-ios-macos">
  ### Web Auth でのログイン (iOS / macOS)
</div>

ログイン画面を表示するファイルで、`Auth0` モジュールをインポートします。

```swift lines theme={null}
import Auth0
```

次に、**Login** ボタンのアクションで [Universal Login](/docs/ja-jp/authenticate/login/auth0-universal-login) ページを表示します。

```swift lines theme={null}
Auth0
    .webAuth()
    .start { result in
        switch result {
        case .success(let credentials):
            print("Obtained credentials: \(credentials)")
        case .failure(let error):
            print("Failed with: \(error)")
        }
    }
```

<div id="using-asyncawait">
  #### async/await を使う
</div>

```swift lines theme={null}
do {
    let credentials = try await Auth0.webAuth().start()
    print("Obtained credentials: \(credentials)")
} catch {
    print("Failed with: \(error)")
}
```

#### Combine を使う

```swift lines theme={null}
Auth0
    .webAuth()
    .start()
    .sink(receiveCompletion: { completion in
        if case .failure(let error) = completion {
            print("Failed with: \(error)")
        }
    }, receiveValue: { credentials in
        print("Obtained credentials: \(credentials)")
    })
    .store(in: &cancellables)
```

<div id="web-auth-logout-ios-macos">
  ### Web Auth ログアウト (iOS / macOS)
</div>

ユーザーをログアウトするには、<Tooltip tip="Universal Login: アプリケーションは、ユーザーの本人確認を行うために、Auth0 の認可サーバーでホストされている Universal Login にリダイレクトされます。" cta="用語集を見る" href="/docs/ja-jp/glossary?term=Universal+Login">Universal Login</Tooltip> のセッション Cookie をクリアした後、アプリケーションからユーザーの資格情報を削除します。

**Logout** ボタンのアクションで `logout()` メソッドを呼び出します。セッション Cookie がクリアされたら、[ユーザーの資格情報を削除](https://github.com/auth0/Auth0.swift/blob/master/EXAMPLES.md#clear-stored-credentials)します。

```swift lines theme={null}
Auth0
    .webAuth()
    .logout { result in
        switch result {
        case .success:
            print("Session cookie cleared")
            // 資格情報を削除する
        case .failure(let error):
            print("Failed with: \(error)")
        }
    }
```

<div id="using-asyncawait">
  #### async/await を使う
</div>

```swift lines theme={null}
do {
    try await Auth0.webAuth().logout()
    print("Session cookie cleared")
    // 資格情報を削除する
} catch {
    print("Failed with: \(error)")
}
```

<div id="using-combine">
  #### Combine を使用する
</div>

```swift lines theme={null}
Auth0
    .webAuth()
    .logout()
    .sink(receiveCompletion: { completion in
        switch completion {
        case .finished:
            print("Session cookie cleared")
            // 資格情報を削除する
        case .failure(let error):
            print("Failed with: \(error)")
        }
    }, receiveValue: {})
    .store(in: &cancellables)
```

<div id="sso-alert-box-ios-macos">
  ### SSOアラートボックス (iOS / macOS)
</div>

<Frame>
  <img src="https://mintcdn.com/translations/mMSz-RNYLuOm2GmQ/docs/images/cdy7uua7fh8z/c7JkfRnkm3HC12OqbNcpf/74cad82d875e50b2dca62088d44c2db9/sso-alert.png?fit=max&auto=format&n=mMSz-RNYLuOm2GmQ&q=85&s=05070055f9e61e5a35452d64e3029872" alt="undefined" width="323" height="200" data-path="docs/images/cdy7uua7fh8z/c7JkfRnkm3HC12OqbNcpf/74cad82d875e50b2dca62088d44c2db9/sso-alert.png" />
</Frame>

Web Auth の使用時にデフォルトで表示されるポップアップアラートボックスについて詳しくは、[FAQ](https://github.com/auth0/Auth0.swift/blob/master/FAQ.md) をご確認ください。

<Callout icon="file-lines" color="#0EA5E9" iconType="regular">
  詳しくは、[How to Create a Seamless Mobile SSO (Single Sign-On) Experience in iOS](https://developer.okta.com/blog/2022/01/13/mobile-sso) もあわせてご覧ください。
</Callout>

<div id="next-steps">
  ## 次のステップ
</div>

主な機能については、[Examples](https://github.com/auth0/Auth0.swift/blob/master/EXAMPLES.md)で確認できます。

* [資格情報を保存する](https://github.com/auth0/Auth0.swift/blob/master/EXAMPLES.md#store-credentials): ユーザーの資格情報を Keychain に安全に保存します。
* [保存済みの資格情報を確認する](https://github.com/auth0/Auth0.swift/blob/master/EXAMPLES.md#check-for-stored-credentials): アプリの起動時に、ユーザーがすでにログインしているかどうかを確認します。
* [保存済みの資格情報を取得する](https://github.com/auth0/Auth0.swift/blob/master/EXAMPLES.md#retrieve-stored-credentials): ユーザーの資格情報を Keychain から取得し、有効期限が切れている場合は自動的に更新します。
* [保存済みの資格情報を消去する](https://github.com/auth0/Auth0.swift/blob/master/EXAMPLES.md#clear-stored-credentials): ログアウト処理を完了するために、ユーザーの資格情報を削除します。
* [ユーザー情報を取得する](https://github.com/auth0/Auth0.swift/blob/master/EXAMPLES.md#retrieve-user-information): `/userinfo` エンドポイントから最新のユーザー情報を取得します。
