Server-Side Sign in with Apple

Nov 15 2022 Swift 5.6, macOS 12, iOS 15, Xcode 13.3

Part 1: Add Sign in with Apple to an iOS Project

3. Add Sign in with Apple to Your iOS App

Episode complete

Play next episode

Next
Save for later
About this episode
See forum comments
Cinema mode Mark complete Download course materials
Previous episode: 2. Add Sign in with Apple to Your Vapor App Next episode: 4. Connect Your iOS App to Your Vapor App

This video Add Sign in with Apple to Your iOS App was last updated on Nov 15 2022

Heads up... You've reached locked video content where the transcript will be shown as obfuscated text.

You can unlock the rest of this video course, and our entire catalogue of books and videos, with a kodeco.com Professional subscription.

In this video we’re going to add Sign in with Apple to your iOS app and learn how to show the button on the login screen. Please note that you’ll need an Apple developer account to test this out.

Adding the capability

Open the TILiOS project in Xcode and navigate to the TILiOS target. Change the bundle identifier to something unique to your project. For example, this video will use dev.timc.siwa-demo.TILiOS. Next click on the Signing & Capabilities tab and select your development team to ensure the app is signed correctly.

Displaying the button

Now that you can do so, it’s time to show the Sign in with Apple button on the log in screen. Open LoginView.swift and at the top of the file, import AuthenticationServices:

import AuthenticationServices
SignInWithAppleButton(.signIn) { request in
  
} onCompletion: { result in

}
request.requestedScopes = [.fullName, .email]
@MainActor
func handleSIWA(result: Result<ASAuthorization, Error>) async throws -> Token {

}
switch result {
case .failure(let error):
  self.showingLoginErrorAlert = true
  print("Error \(error)")
  throw error
}
case .success(let authResult):
  if let credential = authResult.credential as? ASAuthorizationAppleIDCredential {

  } else {
    self.showingLoginErrorAlert = true
    throw AuthError.badResponse
  }
guard
    let identityToken = credential.identityToken,
    let tokenString = String(data: identityToken, encoding: .utf8)
else {
    print("Failed to get token from credential")
    self.showingLoginErrorAlert = true
    throw AuthError.badResponse
}
let name: String?
if let nameProvided = credential.fullName {
    name = "\(nameProvided.givenName ?? "") \(nameProvided.familyName ?? "")"
} else {
    name = nil
}
throw AuthError.notLoggedIn
Task {
    let apiToken = try await handleSIWA(result: result)
}
auth.token = apiToken.value