Sunday, October 25, 2026
HomeiOS Developmentios - I can not delete the consumer logged in with apple

ios – I can not delete the consumer logged in with apple

[ad_1]

We’re implementing the brand new pointers imposed by Apple on consumer deletion. I’m utilizing firebase for auth. I’m making a consumer account. no drawback right here. however I can not delete customers logged in with apple. i get error. I’m utilizing this api to delete, token revoke API.
I am new to swift. I could not discover the issue

that is my login code. it is working appropriate :

class SignUpNewUser: NSObject {
    /// apple
    var currentNonce: String? // fileprivate
}

extension SignUpNewUser : ASAuthorizationControllerPresentationContextProviding{
    
    func Apple() {
        let nonce = randomNonceString()
        currentNonce = nonce
        let appleIDProvider = ASAuthorizationAppleIDProvider()
        let request = appleIDProvider.createRequest()
        request.requestedScopes = [.fullName, .email]
        request.nonce = sha256(nonce)
        
        let authorizationController = ASAuthorizationController(authorizationRequests: [request])
        authorizationController.delegate = self
        authorizationController.presentationContextProvider = self
        authorizationController.performRequests()
    }
    
    non-public func sha256(_ enter: String) -> String {
        let inputData = Knowledge(enter.utf8)
        let hashedData = SHA256.hash(information: inputData)
        let hashString = hashedData.compactMap {
            return String(format: "%02x", $0)
        }.joined()
        
        return hashString
    }
    
    non-public func randomNonceString(size: Int = 32) -> String {
        precondition(size > 0)
        let charset: Array<Character> =
            Array("0123456789ABCDEFGHIJKLMNOPQRSTUVXYZabcdefghijklmnopqrstuvwxyz-._")
        var consequence = ""
        var remainingLength = size
        
        whereas remainingLength > 0 {
            let randoms: [UInt8] = (0 ..< 16).map { _ in
                var random: UInt8 = 0
                let errorCode = SecRandomCopyBytes(kSecRandomDefault, 1, &random)
                if errorCode != errSecSuccess {
                    fatalError("Unable to generate nonce. SecRandomCopyBytes failed with OSStatus (errorCode)")
                }
                return random
            }
            randoms.forEach { random in
                if size == 0 {
                    return
                }
                
                if random < charset.rely {
                    consequence.append(charset[Int(random)])
                    remainingLength -= 1
                }
            }
        }
        return consequence
    }
}



extension SignUpNewUser: ASAuthorizationControllerDelegate {
    
    
    func authorizationController(controller: ASAuthorizationController, didCompleteWithAuthorization authorization: ASAuthorization) {
        
        guard let appleIDCredential = authorization.credential as? ASAuthorizationAppleIDCredential else {
            dataSource?.endSignUpWithError(error: eLogin.authorizationNotFound.error(), methodology: .apple)
            return
        }
        
        guard let nonce = currentNonce else {
            fatalError("Invalid state: A login callback was obtained, however no login request was despatched.")
        }
        
        // JWT
        guard let appleIDToken = appleIDCredential.identityToken else {
            dataSource?.endSignUpWithError(error: eLogin.identityToken.error(), methodology: .apple)
            return
        }
        
        guard let idTokenString = String(information: appleIDToken, encoding: .utf8) else {
            mylog(eLogin.serializeToken.error(), appleIDToken.debugDescription)
            dataSource?.endSignUpWithError(error: eLogin.serializeToken.error(), methodology: .apple)
            return
        }
        
        let credential = OAuthProvider.credential(withProviderID: "apple.com", idToken: idTokenString, rawNonce: nonce)
        
        @AppStorage("JWt_client_secret") var client_secret: String = ""
        @AppStorage("apltoken") var apple_token: String = ""
        client_secret = nonce
        apple_token = idTokenString

        signInFirebaseAccount(credential, appleIDCredential:appleIDCredential)
    }
    
    
    
    /// signup tamam firebase bağla hesabı
    non-public func signInFirebaseAccount(_ credential: AuthCredential, appleIDCredential: ASAuthorizationAppleIDCredential) {
        Auth.auth().signIn(with: credential) { (authResult, error) in
            
            if let error = error {
                self.dataSource?.endSignUpWithError(error: error , methodology: .apple)
            }else{
                guard let consumer = authResult?.consumer else {
                    self.dataSource?.endSignUpWithError(error: eLogin.kayitHatasi.error(), methodology: .apple)
                    return
                }

                // JWT
                guard let appleIDToken = appleIDCredential.identityToken else {
                    print("hata 5")
                    return
                }
                
                guard let idTokenString = String(information: appleIDToken, encoding: .utf8) else {
                    print("hata 6")
                    return
                }
                guard let nonce = self.currentNonce else {
                    fatalError("Invalid state: A login callback was obtained, however no login request was despatched.")
                }

                @AppStorage("JWt_client_secret") var client_secret: String = ""
                @AppStorage("apltoken") var apple_token: String = ""
                client_secret = nonce
                apple_token = idTokenString

                
                self.createNewUser(consumer: consumer, methodology: .apple)
                
            }
        }
    }

}

i’m utilizing this code to delete account. this isn’t working. I am including the console picture I acquired after working the code.

extension SignUpNewUser{
    func deleteAppleAccount() {
        @AppStorage("JWt_client_secret") var client_secret: String = ""
        @AppStorage("apltoken") var apple_token: String = ""


        let urlString = "https://appleid.apple.com/auth/revoke"
        let url = NSURL(string: urlString)!

        let paramString: [String : Any] = [
            "client_id": "com.MyCompany.Name",
            "client_secret": client_secret,
            "token": apple_token
        ]

        
        let request = NSMutableURLRequest(url: url as URL)
        request.httpMethod = "POST"
        request.httpBody = strive? JSONSerialization.information(withJSONObject:paramString, choices: [.prettyPrinted])
        request.setValue("utility/x-www-form-urlencoded", forHTTPHeaderField: "Content material-Kind")
//                request.setValue("utility/json", forHTTPHeaderField: "Settle for")

        
        let process =  URLSession.shared.dataTask(with: request as URLRequest)  { (information, response, error) in
                guard
                    let response = response as? HTTPURLResponse,
                    error == nil
                else {                                                               // examine for elementary networking error
                    print("error", error ?? URLError(.badServerResponse))
                    return
                }
                
                guard (200 ... 299) ~= response.statusCode else {                    // examine for http errors
                    print("statusCode must be 2xx, however is (response.statusCode)")
                    print("response = (response)")
                    return
                }

                
            if let error = error {
                print(error)
            }else{
                print("deleted accont")
            }
        }
        process.resume()
    }

}

console output:

statusCode must be 2xx, however is 400
response = <NSHTTPURLResponse: 0x2815e5e00> { URL: https://appleid.apple.com/auth/revoke } { Standing Code: 400, Headers {
    "Cache-Management" =     (
        "no-store"
    );
    Connection =     (
        "keep-alive"
    );
    "Content material-Size" =     (
        26
    );
    "Content material-Kind" =     (
        "utility/json;charset=ISO-8859-1"
    );
    Date =     (
        "Sat, 28 Might 2022 12:07:51 GMT"
    );
    Pragma =     (
        "no-cache"
    );
    Server =     (
        Apple
    );
} }

the place am i going unsuitable? I could not discover any tutorial about it. Account deletion possibility will probably be obligatory after the thirtieth of the month. I would like to repair this now 🙁

[ad_2]

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments