How to Integrate the New ‘Sign in with Apple’ Button in Your iOS App?

sign-in-with-apple

Want to integrate Sign in with Apple feature in your iOS mobile application? Want to make log-in of your mobile app simpler and safer for your user? Check out this Apple iOS tutorial written by our experienced iOS app developer.

Being an iOS app development company, we keep eye on upcoming trends and technologies from Apple. This article is the output of it. Seeing WWC 2019, our one of our experienced iOS developers wrote this article on how to integrate ‘Sign in with Apple’ feature into your iOS application.

Signing in with Google and Facebook has been a norm since as long as one can remember. This changed on June 3, 2019. During WWDC 2019 at San Jose, Apple announced the new ‘Sign in with Apple’. This feature will only be compatible with versions iOS13 and later.

Apple is making its sign-in required whenever a third-party (whether it’s Facebook, Google, Instagram, Snapchat or any other) log-in option is provided, giving users a private choice.

Why ‘Sign in with Apple’?

In one word- privacy

Amidst scandals revolving privacy and security breaches by Google and Facebook, this announcement comes as a breath of fresh air.

“‘Sign in with Apple’ is a fast, easy way to sign in, without all the tracking,” said Craig Federighi, Apple’s software engineering chief, probably taking a dig at Apple’s competitors. The only data that is collected is the user’s name and email address. The user can even choose to hide their email address in the app.

What else?

1. Security

A user account is always secure as every account using ‘Sign In with Apple id’ gets automatically protected with two-factor authenticationThis is done by adding Touch ID or Face ID as a second layer of protection after the username/password combination.

2. Multiple platforms

It works natively on iOS, macOS, tvOS, and watchOS. You can also deploy it on your website and in versions of your apps running on other platforms. For apps on Android devices and web, users are sent to a ‘web view’ of the app where they need to click on Sign in with Apple and enter their AppleID and password to complete their sign in.

3. Prevent fraud

Using advanced machine learning it can send alerts if the new account on your app is (probably) not a real person.The developer can take this alert into consideration while processing their own additional anti-fraud measures or while providing permission to features in their apps

4. Non-repetitive logins

If the user already has an account with the app, they will be alerted and asked if they wish to log in with the existing email instead.

Before we take you towards step by step guide, please allow us to update that if you are not very much into the technical part, feel free to get in touch with our technical team directly by filling the form in the footer. State your requirements and we will allocate a dedicated iOS developer who can help you out. So far, we have developed a lot of iOS applications in the different categories which require the log-in feature.

Steps to integrate ‘Sign in with Apple’ feature in your iOS app

STEP 1– Create a New Project in Xcode

New project

new-project

STEP 2– Go to “Target” and click on “Capability”

STEP 3– Add the “Sign in with Apple” Capability in your project

sign in with apple

STEP 4 – Go to the “ViewController.swift” and Import ASAuthenticationServices framework (framework for “Sign In with Apple”)

import AuthenticationServices

This provides the ASAuthorizationAppleID button.

STEP 5– Add the “Sign in with Apple” button in ViewController after adding the target for TouchUpInside action and add action to the login button click.

override func viewDidLoad() {

        super.viewDidLoad()     

        // Do any additional setup after loading the view.

        self.setupSOAppleSignIn()

    }

       func setupSOAppleSignIn() {

        let btnAuthorization = ASAuthorizationAppleIDButton()

        btnAuthorization.frame = CGRect(x: 0, y: 0, width: 200, height: 40)

        btnAuthorization.center = self.view.center

        btnAuthorization.addTarget(self, action: #selector(actionHandleAppleSignin), for: .touchUpInside)

        self.view.addSubview(btnAuthorization)

    }

@objc func actionHandleAppleSignin() {

        let appleIDProvider = ASAuthorizationAppleIDProvider()

        let request = appleIDProvider.createRequest()

        request.requestedScopes = [.fullName, .email]

        let authorizationController = ASAuthorizationController(authorizationRequests: [request])

        authorizationController.delegate = self

        authorizationController.presentationContextProvider = self

        authorizationController.performRequests()

    }

 

STEP 6– Handle ASAuthorizationController Delegate and Presentation Context for success / failure response.

You can create an extension as well as add a source in an existing class.

extension ViewController: ASAuthorizationControllerDelegate {

     // ASAuthorizationControllerDelegate function for authorization failed

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

        print(error.localizedDescription)

    }

       // ASAuthorizationControllerDelegate function for successful authorization

    func authorizationController(controller: ASAuthorizationController, didCompleteWithAuthorization authorization: ASAuthorization) {

        if let appleIDCredential = authorization.credential as? ASAuthorizationAppleIDCredential {

            // Create an account as per your requirement

            let appleId = appleIDCredential.user

            let appleUserFirstName = appleIDCredential.fullName?.givenName

            let appleUserLastName = appleIDCredential.fullName?.familyName

            let appleUserEmail = appleIDCredential.email

            //Write your code

        } else if let passwordCredential = authorization.credential as? ASPasswordCredential {

            let appleUsername = passwordCredential.user

            let applePassword = passwordCredential.password

            //Write your code

        }

    }

}

extension ViewController: ASAuthorizationControllerPresentationContextProviding {

    //For present window

    func presentationAnchor(for controller: ASAuthorizationController) -> ASPresentationAnchor {

        return self.view.window!

    }

}

STEP 7– You need to enable Sign in with Apple in your developer account.

Go to  “Certificates, Identifiers & Profile” section and then click on the “Keys” option.

create key

Click on “Create a key” option. Enter the name of the key and enable the “Sign in with Apple” option

register key

Done.

We hope you found this tutorial helpful. With the help of this iPhone app tutorial, we have learned how to integrate Sign in with Apple button for login into an iOS app. You can get the source of this sign-in with Apple example from Github.

Being an iPhone app development company, thriving in iOS app development, our developers are always eager to help other developers. We have developed over 3500+ mobile applications over the past 9 years for both iOS and Android platforms, and we continue to grow.

If you still have any doubts in iOS app development, Android app development, or any mobile app development in general, we are happy to guide you.

This page was last edited on June 28th, 2019, at 9:36.
 
 

Have an App Idea?

Get your free consultation now

Leave a Reply

Your email address will not be published. Required fields are marked *