【Flutter】sign_in_with_appleでApple IDでサインイン

Flutter

sign_in_with_appleとは

sign_in_with_appleは、Apple IDを使用してサインインする機能をFlutterアプリに統合するためのパッケージです。
Apple IDによるログインと、ユーザーのキーチェーンに保存された資格情報の取得をサポートします。

sign_in_with_apple | Flutter package
Flutter bridge to initiate Sign in with Apple (on iOS, macOS, and Android). Includes support for keychain entries as wel...

使い方

sign_in_with_appleのインストール

sign_in_with_apple install | Flutter package
Flutter bridge to initiate Sign in with Apple (on iOS, macOS, and Android). Includes support for keychain entries as wel...

基本的な使用方法

  1. SignInWithAppleButtonの追加
  • SignInWithAppleButtonウィジェットを使用して、Apple IDでのサインインボタンを追加します。
import 'package:flutter/material.dart';
import 'package:sign_in_with_apple/sign_in_with_apple.dart';

SignInWithAppleButton(
  onPressed: () async {
    final credential = await SignInWithApple.getAppleIDCredential(
      scopes: [
        AppleIDAuthorizationScopes.email,
        AppleIDAuthorizationScopes.fullName,
      ],
    );
    print(credential);
    // 取得したcredentialをサーバーに送信して、Appleで検証し、新しいセッションを作成します
  },
),

サーバーとの統合

Apple IDでのサインインを完全に統合するためには、受け取った資格情報をAppleのサーバーで検証し、それに基づいて新しいセッションを作成する必要があります。
さらに、取得したリフレッシュトークンを使用して定期的にセッションを検証し、Apple側で認証が取り消された場合はセッションを無効にする必要があります。

前提条件

Appleの開発者プログラムに有料で参加している必要があります。
Apple IDでのサインインは、無料のApple IDでは利用できません。

Apple Developer Program - Apple Developer
Apple Developer Programに加入すれば、iPhone、iPad、Mac、Apple TV、Apple Vision Pro、Apple Watch用App Storeで、世界中のユーザーとつながることができます。

設定

  1. App IDの登録
  • Apple Developerアカウントで新しいApp IDを作成し、Apple IDでのサインイン機能を有効にします。
  1. Service IDの作成 (WebおよびAndroidのみ)
  • WebやAndroidでサインイン機能を使用する場合は、Service IDを作成し、ドメインとリターンURLを設定します。
  1. 認証キーの作成
  • Appleのサーバーと通信するための認証キーを作成し、キーIDをメモします。

プラットフォーム別設定

iOS

  1. XcodeでSign in with Apple機能をアプリのCapabilitiesに追加します。
  2. 必要に応じてプロビジョニングプロファイルを更新します。

Android

  1. AndroidManifest.xmlに以下の設定を追加します。
<activity
    android:name="com.aboutyou.dart_packages.sign_in_with_apple.SignInWithAppleCallback"
    android:exported="true">
    <intent-filter>
        <action android:name="android.intent.action.VIEW"/>
        <category android:name="android.intent.category.DEFAULT"/>
        <category android:name="android.intent.category.BROWSABLE"/>
        <data android:scheme="signinwithapple"/>
        <data android:path="callback"/>
    </intent-filter>
</activity>
  1. Chrome Custom Tabを使用してサインインページからアプリに戻るために、launchModesingleTaskまたはsingleTopに設定します。

Web

  1. index.html<head>タグに以下のスクリプトを追加します。
<script type="text/javascript" src="https://appleid.cdn-apple.com/appleauth/static/jsapi/appleid/1/en_US/appleid.auth.js"></script>
  1. Apple Developerポータルで設定したドメインとリターンURLを追加します。
タイトルとURLをコピーしました