LoginSignup
10
6

More than 3 years have passed since last update.

signin with apple 実装するときの注意点まとめ

Last updated at Posted at 2020-01-20

対応バージョン

iOS13以上、iOS13以下は対応しないため、別々のデザイン準備する必要があります。

取得できるユーザーデータ

  • email
    • ユーザーの選択でランダム生成されたメール渡せる可能性があります
  • full name
    • ユーザーの編集可能で、apple id登録された名前とは限らない

sign in 初回と2回目の違い

sign in 初回はメールと名前のユーザーデータ取れますが、その以後sign inする場合は取得できなくなります。
初回取得成功後key chainなどに保存し、登録成功後削除したほうが一番無難。

 let request = ASAuthorizationAppleIDProvider().createRequest()
 request.requestedScopes = [.fullName, .email]
 let controller = ASAuthorizationController(authorizationRequests: [request])
 controller.delegate = self
 controller.presentationContextProvider = self
 controller.performRequests()

extension AppleSigninService: ASAuthorizationControllerDelegate {
    func authorizationController(controller: ASAuthorizationController, didCompleteWithAuthorization authorization: ASAuthorization) {
        guard let credentials = authorization.credential as? ASAuthorizationAppleIDCredential else {
            let error = NSError(domain: "jp.huiping192.signInWithApple", code: -1000, userInfo: nil)
            signInSubject?.onError(error)
            return
        }

        // アプリ起動時ログイン状態チェックのためcredentials.user keychainに保持する
          saveUserId(credentials.user)
        // mailとnameある場合一時保持、登録成功後に削除
          saveUserInfo(nickname: credentials.fullName?.nickname, email: credentials.email)
    }

    func authorizationController(controller: ASAuthorizationController, didCompleteWithError error: Error) {
        // error handing
    }
}

AppleID使用停止検知

「設定」 -> 「{Apple ID}」->「パスワードとセキュリティ」-> 「Apple IDを使用中のApp」 -> 「{app name}」-> 「Apple IDの使用を停止する」

ユーザーは上の手順でSign in with Apple使用停止することが可能なので、アプリ側にチェックする必要があります。

  • アプリ起動時

ASAuthorizationAppleIDProvider().getCredentialState(forUserID: appleUserID) { credentialState, _ in
            if credentialState != .authorized {
                // logout
                User.current.logout()
            }
 }
  • アプリ起動中
NotificationCenter.default.rx.notification(ASAuthorizationAppleIDProvider.credentialRevokedNotification).subscribe(onNext: { _ in
                // logoutしとく
                User.current.logout()
            }).disposed(by: disposeBag)

AppleID使用停止後再度登録

新しいユーザーを扱い、token、ユーザー情報新しく取得できる。

10
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
10
6