I'm working on Apple Sign In in Simulator iOS 13.4.1 and XCode Version 11.4.1.
I used to get privaterelay.appleid.com when Hide Email before but after changing my Laptop and try to test Apple Sign In in Simulator again, I got nil result. For other email when I didn't activate Hide Email it's still working and not give me any nil result.
Here my code:
// Create class for object first
class AppleUser{
var id: String?
var firstName: String?
var lastName: String?
var email: String?
@available(iOS 13.0, *)
init(credentials: ASAuthorizationAppleIDCredential) {
id = credentials.user
firstName = credentials.fullName?.givenName ?? ""
lastName = credentials.fullName?.familyName ?? ""
email = credentials.email ?? ""
}
}
extension AppleUser: CustomDebugStringConvertible{
var debugDescription: String{
return """
ID: \(id ?? "")
First Name: \(firstName ?? "")
Last Name: \(lastName ?? "")
Email: \(email ?? "")
"""
}
}
// on ViewController
@objc
func didTapAppleButton(){
if #available(iOS 13.0, *) {
let provider = ASAuthorizationAppleIDProvider()
let request = provider.createRequest()
request.requestedScopes = [.fullName, .email]
let controller = ASAuthorizationController(authorizationRequests: [request])
controller.delegate = self
controller.presentationContextProvider = self
controller.performRequests()
} else {
// failed
}
}
extension ViewController: ASAuthorizationControllerDelegate, ASAuthorizationControllerPresentationContextProviding{
@available(iOS 13.0, *)
func presentationAnchor(for controller: ASAuthorizationController) -> ASPresentationAnchor {
return view.window!
}
func authorizationController(controller: ASAuthorizationController, didCompleteWithAuthorization authorization: ASAuthorization) {
switch authorization.credential {
case let credentials as ASAuthorizationAppleIDCredential:
let user = AppleUser(credentials: credentials)
print(user)
/*
Print result when Hide Email
ID: 123215213243.e12324213.12314213 <some random Int>
First Name: My First Name
Last Name: My Last Name
Email: <nil>
Print result when normal Email used
ID: 123215213243.e12324213.12314213 <some random Int>
First Name: My First Name
Last Name: My Last Name
Email: my_email@icloud.com
*/
default:
// failed
}
}
}