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

03. Add Sign in with Apple to Your iOS App

Episode complete

Play next episode

Next
About this episode
Leave a rating/review
See forum comments
Cinema mode Mark complete Download course materials
Previous episode: 02. Add Sign in with Apple to Your Vapor App Next episode: 04. Connect Your iOS App to Your Vapor App

Get immediate access to this and 4,000+ other videos and books.

Take your career further with a Kodeco Personal Plan. With unlimited access to over 40+ books and 4,000+ professional videos in a single subscription, it's simply the best investment you can make in your development career.

Learn more Already a subscriber? Sign in.

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

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