I’ve having an issue with my Application. I have setup OneSignal following the tutorial on OneSignal website. Everything is working and now i’m looking to retrieve the unique “player_id” of the device.
All codes I’m finding or the documentation isn’t working. Here is a code I have found :
private func getPlayerId() {
OneSignal.getDeviceState { state in
if let playerId = state?.userId {
self.playerId = playerId
print("Player ID: (playerId)")
} else {
print("Player ID not available")
}
}
}
Full code :
import SwiftUI
import OneSignalFramework
struct FormView: View {
@State private var name: String = UserDefaults.standard.string(forKey: "Nom") ?? ""
@State private var playerId: String = ""
@Environment(.presentationMode) var presentationMode
var body: some View {
VStack {
Text("Veuillez entrer votre nom")
.font(.headline)
.padding()
TextField("Nom", text: $name)
.textFieldStyle(RoundedBorderTextFieldStyle())
.padding()
HStack {
Button(action: {
saveName()
presentationMode.wrappedValue.dismiss()
}) {
Text("Enregistrer")
.padding()
.background(Color.blue)
.foregroundColor(.white)
.cornerRadius(8)
}
Button(action: {
presentationMode.wrappedValue.dismiss()
}) {
Text("Retour")
.padding()
.background(Color.gray)
.foregroundColor(.white)
.cornerRadius(8)
}
}
.padding()
}
.onAppear {
getPlayerId()
}
.navigationTitle("Inscription")
.navigationBarTitleDisplayMode(.inline)
}
private func getPlayerId() {
OneSignal.getDeviceState { state in
if let playerId = state?.userId {
self.playerId = playerId
print("Player ID: (playerId)")
} else {
print("Player ID not available")
}
}
}
private func saveName() {
// Enregistrer le nom localement
UserDefaults.standard.set(name, forKey: "Nom")
print("Nom enregistré : (name)")
// Envoyer le nom à une URL en arrière-plan
sendNameToServer(name: name)
}
private func sendNameToServer(name: String) {
// Encoder le nom en Base64
let data = name.data(using: .utf8)
let base64EncodedName = data?.base64EncodedString() ?? ""
// Construire l'URL avec le nom encodé en Base64
let urlString = "https://mywebsiteaaa.com/nux.php?accueil:::::::::::::(base64EncodedName):(playerId)"
// Créer l'URL complète
guard let url = URL(string: urlString) else {
print("URL invalide")
return
}
// Créer une requête HTTP
var request = URLRequest(url: url)
request.httpMethod = "POST"
// Créer une tâche de session pour envoyer les données
let task = URLSession.shared.dataTask(with: request) { data, response, error in
if let error = error {
print("Erreur lors de l'envoi des données : (error)")
return
}
guard let httpResponse = response as? HTTPURLResponse, (200...299).contains(httpResponse.statusCode) else {
print("Réponse inattendue du serveur")
return
}
// Traitement des données de réponse si nécessaire
if let data = data, let responseString = String(data: data, encoding: .utf8) {
print("Réponse du serveur : (responseString)")
}
}
// Démarrer la tâche
task.resume()
}
}
struct FormView_Previews: PreviewProvider {
static var previews: some View {
FormView()
}
}
I get error in Xcode : Type 'OneSignal' has no member 'getDeviceState'
Below a screenshot on my Xcode to have a better view of the situation :
2