Skip to content

Commit c70092b

Browse files
committed
make consent a toggle
1 parent d8e08c6 commit c70092b

3 files changed

Lines changed: 22 additions & 8 deletions

File tree

iOS_SDK/OneSignalSwiftUIExample/OneSignalSwiftUIExample/Services/OneSignalService.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ final class OneSignalService {
6969
}
7070

7171
func revokeConsent() {
72+
// Must set consent as required first, then revoke it
73+
OneSignal.setConsentRequired(true)
7274
OneSignal.setConsentGiven(false)
7375
}
7476

iOS_SDK/OneSignalSwiftUIExample/OneSignalSwiftUIExample/ViewModels/OneSignalViewModel.swift

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@ final class OneSignalViewModel: ObservableObject {
6060
// Location
6161
@Published var isLocationShared: Bool = false
6262

63+
// Consent
64+
@Published var consentGiven: Bool = true
65+
6366
// UI State
6467
@Published var showingAddSheet: Bool = false
6568
@Published var addItemType: AddItemType = .email
@@ -98,9 +101,14 @@ final class OneSignalViewModel: ObservableObject {
98101

99102
// MARK: - Consent
100103

101-
func revokeConsent() {
102-
service.revokeConsent()
103-
showToast("Consent revoked")
104+
func toggleConsent() {
105+
consentGiven.toggle()
106+
if consentGiven {
107+
service.setConsentGiven(true)
108+
} else {
109+
service.revokeConsent()
110+
}
111+
showToast(consentGiven ? "Consent given" : "Consent revoked")
104112
}
105113

106114
// MARK: - User Management

iOS_SDK/OneSignalSwiftUIExample/OneSignalSwiftUIExample/Views/Sections/AppInfoSection.swift

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,16 @@ struct AppInfoSection: View {
3737

3838
CardContainer {
3939
InfoRow(label: "App-Id:", value: viewModel.appId, isMonospaced: true)
40+
CardDivider()
41+
ToggleRow(
42+
title: "Privacy Consent",
43+
subtitle: "Grant or revoke privacy consent",
44+
isOn: Binding(
45+
get: { viewModel.consentGiven },
46+
set: { _ in viewModel.toggleConsent() }
47+
)
48+
)
4049
}
41-
42-
ActionButton(title: "Revoke Consent") {
43-
viewModel.revokeConsent()
44-
}
45-
.padding(.top, 12)
4650
}
4751
}
4852
}

0 commit comments

Comments
 (0)