Skip to content

Narlei Moreira

iOS Engineer

  • Sobre
  • Blog iOS
  • Open Source
  • Destaques na Mídia
  • Github
  • Linkedin
  • Twitter
  • Dribbble
  • Instagram
  • Email

Sign In With Apple: Let’s code!

Narlei Moreiraon20 de julho, 202022 de julho, 2020Leave a Comment on Sign In With Apple: Let’s code!

The new Authentication system of Apple available to iOS 13 bring a security to users. Here you will learn how implement that.

Important:

Only available to iOS 13.0+, macOS 10.15+, tvOS 13.0+, watchOS 6.0+.

You need the last Xcode 11 Beta.

The Apple ID Account need 2FA.

1. Add Button Sign in

Create a new empty project (Single View App) and in the ViewController.swift:

import AuthenticationServices

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // Add Sign in Button
        let buttonAuth = ASAuthorizationAppleIDButton(authorizationButtonType: .default, authorizationButtonStyle: .black)
        buttonAuth.addTarget(self, action: #selector(signInAppleID), for: .touchUpInside)
        view.addSubview(buttonAuth)
        buttonAuth.center = view.center
    }
}

This button will be showed:

Other buttons avaliable:

let buttonAuth = ASAuthorizationAppleIDButton(
              authorizationButtonType: .continue, 
             authorizationButtonStyle: .black)
let buttonAuth = ASAuthorizationAppleIDButton(
                authorizationButtonType: .signUp, 
               authorizationButtonStyle: .black)

2. Add the sign in function

You can choice for scopes, like .email, .fullName.

@objc func signInAppleID() {
    let appleIDProvider = ASAuthorizationAppleIDProvider()
    let authRequest = appleIDProvider.createRequest()
    authRequest.requestedScopes = [.email, .fullName]

    let authController = ASAuthorizationController(authorizationRequests: [authRequest])
    authController.presentationContextProvider = self
    authController.delegate = self
    authController.performRequests()
}

3. Getting data

Implementing the delegate, will return appleIDCredential, you can use the user informations (email, name) OR use the authorizationCode to your backend to validate the user, like oAuth2.

extension ViewController: ASAuthorizationControllerDelegate, ASAuthorizationControllerPresentationContextProviding {

    func authorizationController(controller: ASAuthorizationController, didCompleteWithAuthorization authorization: ASAuthorization) {
        guard let appleIDCredential = authorization.credential as? ASAuthorizationAppleIDCredential else { return }

        print("User: \(appleIDCredential.user)")
        print("Email: \(appleIDCredential.email!)")
        print("Name: \(appleIDCredential.fullName!)")
        print("Code: \(appleIDCredential.authorizationCode!)")
    }

    func presentationAnchor(for controller: ASAuthorizationController) -> ASPresentationAnchor {
        return view.window!
    }

    func authorizationController(controller: ASAuthorizationController, didCompleteWithError error: Error) {
        // Fall back to login UI.
    }
}

4. Capabilities

Go to your app target -> tab Signing & Capabilities and add Sign in with Apple: (Command + Shift + L)

Done! Just run your project!

I hope this helps you, if you have questions of suggestions, comment here or call me in my social networks.

  • Twitter
  • Instagram
  • LinkedIn
  • GitHub

Partilhar isto:

  • Twitter
  • Facebook
  • LinkedIn
  • WhatsApp
CategoriesiOS DevelopmentTagsappleiossign in with appleswift

Navegação de Post

iOS Development

How to create a Settings manager in Swift 5.1

Think that you need to work with UserDefaults to save if user is logged in, or if onboarding is already…

Partilhar isto:

  • Twitter
  • Facebook
  • LinkedIn
  • WhatsApp
SOBRE

Olá, meu nome é Narlei Moreira.

Sou um Desenvolvedor iOS apaixonado por soluções que mudam a vida do usuário. Penso que essa profissão me permite mudar a vida das pessoas, tornando o dia a dia delas mais prático, dando a elas tempo para viver as coisas importantes da vida.

Partilhar isto:

  • Twitter
  • Facebook
  • LinkedIn
  • WhatsApp

Entre em contato

  • LinkedIn
  • GitHub
  • Dribbble
  • Twitter
  • Instagram

Pesquisar

Recent Posts

  • iOS Development

    Deleting multiple remote branches with a pattern

  • iOS Development

    Productivity: you need a clipboard history manager!

  • iOS Development

    Increase the ratings of your apps in the AppStore/PlayStore

Entre em contato

  • LinkedIn
  • GitHub
  • Dribbble
  • Twitter
  • Instagram

Pesquisar

Posts recentes

  • Deleting multiple remote branches with a pattern
  • Productivity: you need a clipboard history manager!
  • Increase the ratings of your apps in the AppStore/PlayStore
  • Improve your project using Makefile
  • Perdendo o medo de falar em público
  • Developer tools: how to be more productive?
  • Getting start with Buck build System
  • Apple M1 – Installing HomeBrew and ZSH
  • Apple M1 – Xcode error when build in simulator
  • Apple M1 – Install fastlane

Comentários

    Arquivos

    • maio 2022
    • março 2022
    • janeiro 2022
    • fevereiro 2021
    • janeiro 2021
    • outubro 2020
    • julho 2020
    • junho 2019
    • maio 2019
    • janeiro 2018
    • setembro 2017

    Categorias

    • Destaques na Mídia
    • iOS Development
    • Português
    • SOBRE

    Meta

    • Acessar
    • Feed de posts
    • Feed de comentários
    • WordPress.org
    Proudly powered by WordPress | Theme: Justread by GretaThemes.