Swift Apple 登入 簡單三步驟 看我就對了 Sign in with Apple

簡介

Sign In with Apple 在 2019 的 WWDC 發布,近幾年的第三方登入在Swift可說是必學技能,更何況最年你只要有第三方登入就一定要添加apple登入,不然就會被拒絕上架,也就是Guideline 4.8 – Design – Sign in with Apple,所以今天就來教各位添加swfit apple登入。

Guideline 4.8 – Design – Sign in with Apple

白話翻譯 : 你有用別人的登入就要用apple登入,不然不給你上架????

Your app uses a third-party login service, but does not offer Sign in with Apple. Apps that use a third-party login service for account authentication need to offer Sign in with Apple to users as an equivalent option.

Next Steps

Please revise your app to offer Sign in with Apple as an equivalent option for account authentication.

Resources

  • Get Sign in with Apple sample code.
  • View Sign in with Apple design requirements.
  • Learn about the benefits of Sign in with Apple .

Please see attached screenshots for details.

一、 apple 登入基本設定

1. apple developer添加Sign in with Apple權限

apple developer

點擊你的專案

apple登入添加教學範例

把Sign in with Apple 開啟

apple登入添加教學範例

2. Xcode添加Sign in with Apple權限

至Info點擊Capbility

apple登入添加教學範例

搜尋Sign in with Apple新增

apple登入添加教學範例

完成後會顯示權限

apple登入添加教學範例

二、 apple登入程式碼範例

1. 宣告AuthenticationServices

import AuthenticationServices

2. 新增apple登入原生按鈕

let button = ASAuthorizationAppleIDButton(authorizationButtonType: .default, authorizationButtonStyle: .black)
button.frame = CGRect.init(x: 20, y: 100, width: 100, height: 100)
button.cornerRadius = 8.0
button.addTarget(self, action: #selector(appleLoginButtonTapped), for: .touchUpInside)
self.view.addSubview(button)

3. 設置apple登入樣式

  • black : 黑
  • white : 無邊匡白
  • whiteOutline : 有邊匡白
寬度很長會自動顯示登入字樣

apple登入樣式

寬度窄自動只保留logo

apple登入樣式

4. 新增apple登入事件

@objc func appleLoginButtonTapped() {
    if #available(iOS 13.0, *) {
        let provider = ASAuthorizationAppleIDProvider()
        let request = provider.createRequest()
        request.requestedScopes = [.email, .fullName]
        let controller = ASAuthorizationController(authorizationRequests: [request])
        controller.delegate = self
        controller.presentationContextProvider = self
        controller.performRequests()
    }
}

5. 監聽apple登入Delegate

extension HomeViewController: ASAuthorizationControllerDelegate {
    @available(iOS 13.0, *)
    func authorizationController(controller: ASAuthorizationController, didCompleteWithAuthorization authorization: ASAuthorization) {
        guard let credential = authorization.credential as? ASAuthorizationAppleIDCredential else {
            return
        }
        // upload credential to api
    }
    @available(iOS 13.0, *)
    func authorizationController(controller: ASAuthorizationController, didCompleteWithError error: Error) {
        // Show error
    }
}

extension HomeViewController: ASAuthorizationControllerPresentationContextProviding {
    @available(iOS 13.0, *)
    func presentationAnchor(for controller: ASAuthorizationController) -> ASPresentationAnchor {
        return self.view.window!
    }
}

三、 最後執行DEMO

apple登入樣式

更多swift第三方登入文章

Swift Google登入 Firebase第三方登入串接 👐

Categorized in: