Sign in with Apple

Sarinthon Mangkorn-ngam
3 min readJun 14, 2020

--

ง่าย สะดวก และปลอดภัยด้วย Third-party services for authentication with Apple

เริ่มจากเข้าไปที่ Project setting -> Signing & Capabilities จากนั้นเลือก “Sign in with Apple”

จากนั้นเลือก Team แต่ต้องเป็น “Apple Developer Program” เนื่องจาก Sign in with Apple ไม่รองรับ Enterprise Developer Program

มาเขียนโค้ดกันเริ่มจาก

import AuthenticationServices

จากนั้นให้ทำการ Setup view ตามโค้ดด้านล่าง

func setupView() {     let authorizationButton = ASAuthorizationAppleIDButton()     authorizationButton.frame = buttonContainer.bounds     authorizationButton.addTarget(         self,         action: #selector(loginWithApple),         for: UIControl.Event.touchUpInside     )     buttonContainer.addSubview( authorizationButton )}

รับ Action และ Create request ดังด้านล่าง

@objc func loginWithApple() {    let appleIDProvider = ASAuthorizationAppleIDProvider()    let request = appleIDProvider.createRequest()    request.requestedScopes = [.fullName, .email]    
let
authorizationController = ASAuthorizationController(
authorizationRequests: [request] ) authorizationController.delegate = self authorizationController.presentationContextProvider = self authorizationController.performRequests()}

จากโค้ดด้านบนมีการขอ fullName และ email จากผู้ใช้งาน ถัดไปมารับ events จากการ Create request กัน

extension ViewController: ASAuthorizationControllerDelegate {    func authorizationController(controller: ASAuthorizationController, didCompleteWithError error: Error) {       //Handle error here    }    func authorizationController(controller: ASAuthorizationController, didCompleteWithAuthorization authorization: ASAuthorization) {       if let appleIDCredential = authorization.credential as? ASAuthorizationAppleIDCredential {         let userIdentifier = appleIDCredential.user         let userFirstName = appleIDCredential.fullName?.givenName         let userLastName = appleIDCredential.fullName?.familyName         let userEmail = appleIDCredential.email         print(“””            userID: \(userIdentifier)            FirstName: \(userFirstName ?? “-”)            LastName: \(userLastName ?? “-”)            email: \(userEmail ?? “-”)         “””)       } else if let passwordCredential = authorization.credential as? ASPasswordCredential {           let username = passwordCredential.user           let password = passwordCredential.password           print(“””              username: \(username)              password: \(password)           “””)       }    }}extension ViewController: ASAuthorizationControllerPresentationContextProviding {      func presentationAnchor(for controller: ASAuthorizationController) -> ASPresentationAnchor {          return self.view.window!      }}

รันแอป

จากรูปข้างบนจะเห็นว่าผู้ใช้งานสามารถแก้ไข Name และสามารถเลือก Share email หรือ hide email ก็ได้ หลังจาก Sign in with Apple ไปแล้วจะได้ผลลัพธ์ดังด้านล่าง

ผลลัพธ์

จะเห็นว่า Apple สร้าง email ใหม่ขึ้นมาให้เราทำให้ Application ที่เรา sign in ไปไม่ได้ email จริง ๆ ของเราไป

มากไปกว่านี้เรายังสามารถ “Manage the apps you use with Sign in with Apple” ได้อีกด้วย โดยสามารถเข้าไปได้ที่

  1. Setting app และ tap [your name]
  2. Tap Password & Security
  3. Tap Apps Using Your App ID

เท่านี้เราก็สามารถ Stop Using App ID ที่เราได้ sign in แอปต่าง ๆ ได้เมื่อไรก็ได้

--

--

No responses yet