I don't understand why the Google-rendered consent form is not showing up. It says that it loads successfully, but then it doesn't show up. (I am in Europe, so my location is not a problem). I have tried both on simulators and real devices, I have manually selected only 12 ad providers.
Here is the code in question:
PACConsentInformation.sharedInstance.debugIdentifiers = ["4FDF7D7F-8F56-4E65-8570-103100943386"]
PACConsentInformation.sharedInstance.debugGeography = PACDebugGeography.EEA
PACConsentInformation.sharedInstance.requestConsentInfoUpdate(forPublisherIdentifiers: ["pub-5765803665103285"]){
(_ error: Error?) -> Void in
if let error = error {
// Consent info update failed.
print(error)
} else {
// Consent info update succeeded. The shared PACConsentInformation
// instance has been updated.
if PACConsentInformation.sharedInstance.consentStatus == PACConsentStatus.unknown {
guard let privacyUrl = URL(string: "http://www.gdgapps.altervista.org/index.html/gdg-privacy.html"),
let form = PACConsentForm(applicationPrivacyPolicyURL: privacyUrl) else {
print("incorrect privacy URL.")
return
}
form.shouldOfferPersonalizedAds = true
form.shouldOfferNonPersonalizedAds = true
form.shouldOfferAdFree = true
form.load {(_ error: Error?) -> Void in
print("Load complete.")
if let error = error {
// Handle error.
print("Error loading form: \(error.localizedDescription)")
} else {
print("Load successful.")
}
}
form.present(from: self) { (error, userPrefersAdFree) in
if error != nil {
// Handle error.
} else if userPrefersAdFree {
// User prefers to use a paid version of the app.
//TODO: buy pro version
} else {
// Check the user's consent choice.
let status = PACConsentInformation.sharedInstance.consentStatus
// TODO: show ads
}
}
} else if PACConsentInformation.sharedInstance.consentStatus == PACConsentStatus.nonPersonalized || PACConsentInformation.sharedInstance.consentStatus == PACConsentStatus.personalized{
print("ads")
self.bannerView.isHidden = false
self.tableBasso.constant = 50
self.bannerView.adUnitID = "ca-app-pub-5765803665103285/9440944250"
self.bannerView.rootViewController = self
let request = GADRequest()
if PACConsentInformation.sharedInstance.consentStatus == PACConsentStatus.nonPersonalized {
let extras = GADExtras()
extras.additionalParameters = ["npa": "1"]
request.register(extras)
}
self.bannerView.load(request)
}
}
}
}
You should present the form after it's loaded. Put form.present inside the completion block of form.load.
form.load { [weak self] error in
print("Load complete.")
if let error = error {
print("Error loading form: \(error.localizedDescription)")
} else {
guard let strongSelf = self else { return }
form.present(from: strongSelf) { error, userPrefersAdFree in
[...]
}
}
}
Related
i have this function to change user email. I'm able to change the email and i can see it in firebase console, but when i go return to my application to see user info, i see only the old email presented.
#IBAction func saveButton(_ sender: Any) {
let currentUser = Auth.auth().currentUser
currentUser?.updateEmail(to: emailTextField.text!) { error in
if let error = error {
print(error)
} else {
self.userDocRef = self.db.collection("users").document(self.auth.currentUser!.uid)
self.userDocRef?.getDocument(completion: {(snapshot, error) in
guard snapshot != nil else { print("Error:", error!); return }
let userData = snapshot!.data()!
self.emailTextField.text = userData["email"]! as? String
})
print("CHANGED")
}
}
}
I want to test faceId/touchId mechanism in my app.
I have following:
func auth(){
let context = LAContext()
var error: NSError?
if context.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: &error) {
var reason: String = Strings.Common.unknownBiometryTip.value
if BiometricService.biometricType() == .touchID {
reason = Strings.Common.touchIDEnterTip.value
}
if BiometricService.biometricType() == .faceID {
reason = Strings.Common.faceIDEnterTip.value
}
context.evaluatePolicy(LAPolicy.deviceOwnerAuthentication, localizedReason: reason) { [weak self] (success, error) in
print("error \(error)")
if let error = error as? LAError, error.errorCode == Int(kLAErrorUserCancel) {
self?.cancelTapped?()
return
}
if success {
self?.authSucceed?()
}
else {
self?.authFailure?()
}
}
}
else {
print("Touch ID neither Face ID not available")
}
}
I actually can't test authFailure block, because when i hit "not-matching face" button in XCode, it shows up pin entry view, and look like anything i type in handled as "correct".
I actually want callback anytime user have "wrong" face/touchId/pin entry attempt, but i can't figure out how to get it.
I'm trying to register a user and verify their email. I've done it and verified it on the email but on Auth.auth().currentUser.isEmailVerified
if (Auth.auth().currentUser?.isEmailVerified)! {
// present another vc
} else {
print("not verified yet") //Always prints that
}
here's my sendVerificationEmail function:
if let user = Auth.auth().currentUser {
user.sendEmailVerification(completion: { (error) in
if let error = error {
debugPrint(error)
return
}
print("Sent VerificationMail")
})
} else {
print("no user logged in")
}
here i register the user:
func registrateUser(email: String, password: String, completion: #escaping (Bool) -> Void) {
Auth.auth().createUser(withEmail: email, password: password) { (result, error) in
if let error = error {
debugPrint(error)
completion(false)
return
}
result?.user.sendEmailVerification(completion: { (error) in
if let error = error {
debugPrint(error)
completion(false)
return
}
completion(true)
})
}
}
You need to reload user profile before checking if email is verified.
Use following code:
Auth.auth().currentUser?.reload(completion: { (error) in
guard error == nil else {
// handle error
print(error!.localizedDescription)
return
}
// your code below
if (Auth.auth().currentUser?.isEmailVerified)! {
// present another vc
} else {
print("not verified yet") //Always prints that
}
})
I'm implementing a RequestRetrier with Alamofire to refresh the accessToken of a given user.
func should(_ manager: SessionManager, retry request: Request, with error: Error, completion: #escaping RequestRetryCompletion) {
lock.lock() ; defer { lock.unlock() }
if let response = request.task?.response as? HTTPURLResponse, response.statusCode == 401 {
requestsToRetry.append(completion)
if !isRefreshing {
refreshToken(completion: { [weak self] succeded, accessToken in
guard let strongSelf = self else { return }
strongSelf.lock.lock() ; defer { strongSelf.lock.unlock() }
strongSelf.requestsToRetry.forEach{ $0(succeded, 0.0) }
strongSelf.requestsToRetry.removeAll()
})
}
} else {
completion(false, 0.0)
}
}
When strongSelf.lock.lock() ; defer { strongSelf.lock.unlock() } is called it doesn't continue the execution so I have an infinite loop. I tried checking the result of strongSelf.lock.try() and returns false.
This is happening when I sign in with a wrong password so the server returns a 401.
This is my refreshToken code
guard !isRefreshing else { return }
// ... Get user ... //
if let user = user {
isRefreshing = true
signIn(user: userDTO)
.subscribe(onNext: { [weak self] userSession in
guard let strongSelf = self else { return }
// ... Save accessToken ... //
completion(true, userSession.accessToken)
strongSelf.isRefreshing = false
}, onError: { [weak self] error in
guard let strongSelf = self else { return }
// ... Log out user ... //
completion(false, nil)
strongSelf.isRefreshing = false
})
.disposed(by: bag)
} else {
completion(false, nil)
}
As seen in the Github issue https://github.com/Alamofire/Alamofire/issues/2544 I could fix this by changing:
private let lock = NSLock()
to
private let lock = NSRecursiveLock()
The difference between them is that the recursive lock can by unlocked if the same thread is trying to lock.
I'm new in programming and still learning in swift language.
I hope you could help me with this issue.
when I try to signup the Userinfo shows in database but when I try to login the user info disappears why is that so? can't figure it out.
Here is how i signup
#objc func handleSignUp() {
guard let username = usernameField.text else { return }
guard let email = emailField.text else { return }
guard let pass = passwordField.text else { return }
guard let image = profileImageView.image else { return }
setContinueButton(enabled: false)
continueButton.setTitle("", for: .normal)
activityView.startAnimating()
Auth.auth().createUser(withEmail: email, password: pass) { user, error in
if error == nil && user != nil {
print("User created!")
// 1. Upload the profile image to Firebase Storage
self.uploadProfileImage(image) { url in
if url != nil {
let changeRequest = Auth.auth().currentUser?.createProfileChangeRequest()
changeRequest?.displayName = username
changeRequest?.photoURL = url
changeRequest?.commitChanges { error in
if error == nil {
print("User display name changed!")
self.saveProfile(username: username, profileImageURL: url!) { success in
if success {
self.dismiss(animated: true, completion: nil)
} else {
self.resetForm()
}
}
} else {
print("Error: \(error!.localizedDescription)")
self.resetForm()
}
}
} else {
self.resetForm()
}
}
} else {
self.resetForm()
}
}
}
And here is how I log in
#objc func handleSignIn() {
guard let email = emailField.text else { return }
guard let pass = passwordField.text else { return }
setContinueButton(enabled: false)
continueButton.setTitle("", for: .normal)
activityView.startAnimating()
Auth.auth().signIn(withEmail: email, password: pass) { user, error in
if error == nil && user != nil {
self.dismiss(animated: false, completion: nil)
_ = Auth.auth().currentUser
} else {
print("Error logging in: \(error!.localizedDescription)")
self.resetForm()
}
}
I don't know what seems to be the problem
I'm not familiar with firebase but according to their documentation this is "da way" :
Auth.auth().signIn(withEmail: email, password: password) { (user, error) in
if let error = error {
print("ERROR [ signIn ] => \(error)")
// self.resetForm()
} else {
if let user = user {
let uid = user.uid
let email = user.email
print("Authorised User => \(uid) ... \(email)")
}
}
}
If after trying the above code and you see this output ERROR [ signIn ] => whatever error...", then it's your signIn that failed. If you see this output Authorised User => SomeUID ... SomeEMAIL, then you signed in successfully and your user's data is still in the database.
I hope this helps you find "da way"
If all else fails:
You can change browsers and check your database again ? Try a couple of browsers. This worked for some people
Good luck!