Implementing delegate methods and button actions

Now, your app is displaying a Sign in with Apple button on the screen when it is launched, but it doesn't work yet. To implement the functionality of the Sign in with Apple button, you'll need to make SignInViewController conform to the ASAuthorizationControllerDelegate and ASAuthorizationControllerPresentationContextProviding protocols.

ASAuthorizationControllerDelegate has two optional methods that you will implement:

  • authorizationController(controller: didCompleteWithAuthorization:): This will be executed if the user enters the correct Apple ID and password.
  • authorizationController(controller: didCompleteWithError:): This will be executed if the process was unsuccessful.

ASAuthorizationControllerPresentationContextProviding has a required method, presentationAnchor(for:), which returns the view that will be used to display the various Sign in with Apple dialogs. To implement this, follow these steps:

  1. Click on SignInViewController.swift in the Project navigator.
  2. Add the following code after the class declaration to make SignInViewController conform to the ASAuthorizationControllerDelegate and ASAuthorizationControllerPresentationContextProviding protocols:
class SignInViewController: UIViewController, ASAuthorizationControllerDelegate, ASAuthorizationControllerPresentationContextProviding {

An error will appear; just ignore it for now.

  1. Add the following code just before the final curly brace:
// MARK: - SignInWithApple

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

switch authorization.credential {

case let credentials as ASAuthorizationAppleIDCredential:
print(credentials.user)
print(credentials.fullName?.givenName ?? "No given name provided")
print(credentials.fullName?.familyName ?? "No family name provided")
print(credentials.email ?? "No email provided")

default:
break
}
}

func authorizationController(controller: ASAuthorizationController, didCompleteWithError error: Error) {
print("AppleID Authentication failed", error)
}

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

Let's break this down:

  • authorizationController(controller:didCompleteWithAuthorization:): This is executed when the user successfully enters their Apple ID and password and you get an ASAuthorizationAppleIDCredential. For now, you will just print the contents of this credential, which are as follows:
  • user: A unique identifier string
  • fullName.givenName: A string containing the user's given name
  • fullName.familyName: A string containing the user's family name
  • email: A string containing the user's email
    Note that with the exception of user, all these are optional, so you can provide some default strings if you don't get any values from the credential.
  • authorizationController(controller:didCompleteWithError:): This is executed if the process fails. For now, you'll just write an error message to the Debug area.
  • presentationAnchor(for:): This determines which view is used to display the various dialogs. Here, you set it to the view of SignInViewController.

The error should disappear at this point. Next, you need a property to store the user's given name, which will be passed to ExploreViewController at a later date.

  1. Add the following property just after the class declaration:
var appleIDGivenName: String?
  1. Modify authorizationController(controller:didCompleteWithAuthorization:), as follows:
func authorizationController(controller: ASAuthorizationController, didCompleteWithAuthorization authorization: ASAuthorization) {

switch authorization.credential {

case let credentials as ASAuthorizationAppleIDCredential:
print(credentials.user)
print(credentials.fullName?.givenName ?? "No given name provided")
print(credentials.fullName?.familyName ?? "No family name provided")
print(credentials.email ?? "No email provided")
appleIDGivenName = credentials.fullName?.givenName
performSegue(withIdentifier: "signin", sender: nil)

default:
break
}
}

This sets appleIDGivenName to the user's given name and performs the signin segue.

Now, you'll implement the button action.

  1. Add the following code after displaySignInWithAppleButton():
@objc func appleIDButtonTapped() {
let provider = ASAuthorizationAppleIDProvider()
let request = provider.createRequest()
request.requestedScopes = [.fullName, .email]
let controller = ASAuthorizationController(authorizationRequests: [request])
controller.delegate = self
controller.presentationContextProvider = self
controller.performRequests()
}

This creates an ASAuthorizationAppleIDProvider request that will ask the user for their full name and email, set SignInViewController as the delegate and presentation context provider, and execute the request.

  1. Modify displaySignInWithAppleButton() by assigning the button action, as follows:
func displaySignInWithAppleButton(){
let appleIDButton = ASAuthorizationAppleIDButton()
appleIDButton.translatesAutoresizingMaskIntoConstraints = false
appleIDButton.addTarget(self, action: #selector(appleIDButtonTapped), for: .touchUpInside)
view.addSubview(appleIDButton)
NSLayoutConstraint.activate([
appleIDButton.centerXAnchor.constraint(equalTo: view.centerXAnchor),
appleIDButton.centerYAnchor.constraint(equalTo: view.centerYAnchor)
])
}
  1. Build and run the app and tap the Sign in with Apple button, as shown in the following screenshot:

  1. You'll see a prompt that asks you to enter your Apple ID settings. Tap Settings:

  1. Go through the next few screens to enter your Apple ID username and password. Since this is running in the simulator, tap Don't Merge with iCloud when asked. After you have signed in successfully, stop the app:

  1. Run the app and tap the Sign in with Apple button again.
  1. You will see the following screen. Tap Continue:

  1. Tap the radio button next to Share my emailThen, tap Continue with Password:

  1. Enter the password for the Apple ID and tap Continue.
  1. The Explore screen will appear.
  1. If you look at the Debug area, you will see something similar to the following:

This shows that the process was successful and that the credentials for the user are now in the app.

Now, you can sign in successfully, but you still need to pass the user credentials to the Explore screen. You will modify ExploreViewController to get the necessary credentials and display the user's given name in the Explore screen in the next section.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.223.172.252