I wrote as follows.
An error occurred on line 14 because of forced unwrapping.
extension SignInViewController: ASAuthorizationControllerDelegate {
@available(iOS 13.0, *)
func authorizationController(controller: ASAuthorizationController, didCompleteWithAuthorization authorization: ASAuthorization) {
var name: String? = nil
if let appleIDCredential = authorization.credential as?
ASAuthorizationAppleIDCredential {
if let fname = appleIDCredential.fullName?.familyName { name = fname }
if let gname = appleIDCredential.fullName?.givenName {name = name == nil ? gname : name!+gname}
authenticateWithApple(token:
appleIDCredential.identityToken?.toString, code: appleIDCredential.authorizationCode?.toString, name: name)
}
}
func authenticateWithApple(token: String?, code: String?, name: String?) {
authenticationScenario.signOut(from: .apple)
let param = [LQAAccountAppleTokenKey:token!, LQAAccountAppleAuthorizationCode:code!]
...
}
}
I fixed it by rewriting as below, but I would like to know why nil is included in identityToken or authorizationCode.
guard let identityToken = appleIDCredential.identityToken, let idTokenString = String(data: identityToken, encoding: .utf8) else { return }
guard let authorizationCode = appleIDCredential.authorizationCode, let authCodeString = String(data: authorizationCode, encoding: .utf8) else { return }