Skip to content

Instantly share code, notes, and snippets.

@paigeshin
Created July 18, 2023 03:54
Show Gist options
  • Save paigeshin/0b3b49218d2281a70f363451c6deb94f to your computer and use it in GitHub Desktop.
Save paigeshin/0b3b49218d2281a70f363451c6deb94f to your computer and use it in GitHub Desktop.
import Foundation
import UserMessagingPlatform
import UIKit
protocol GDPRServiceProtocol {
var consentStatus: UMPConsentStatus { get }
func loadForm(from root: UIViewController) async throws -> UMPConsentStatus
}
final class GDPRService: GDPRServiceProtocol {
var consentStatus: UMPConsentStatus {
UMPConsentInformation.sharedInstance.consentStatus
}
init() {
Task {
do {
try await self.request()
} catch {
Log.error(error)
}
}
}
private func request() async throws {
// Create a UMPRequestParameters object.
let parameters = UMPRequestParameters()
let debugSettings = UMPDebugSettings()
#if DEBUG
debugSettings.geography = UMPDebugGeography.EEA
parameters.debugSettings = debugSettings
#endif
// Set tag for under age of consent. Here false means users are not under age.
parameters.tagForUnderAgeOfConsent = false
// Request an update to the consent information.
try await UMPConsentInformation.sharedInstance.requestConsentInfoUpdate(with: parameters)
}
@MainActor
func loadForm(from root: UIViewController) async throws -> UMPConsentStatus {
try await self.request()
guard self.consentStatus == .required else { return self.consentStatus }
let formStatus = UMPConsentInformation.sharedInstance.formStatus
if formStatus == UMPFormStatus.available {
let form = try await UMPConsentForm.load()
try await form.present(from: root)
}
return self.consentStatus
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment