[SWIFT 3] Probleme geofencing / requête

Bonjour,


 


De retour pour un nouveau soucis qui me prends la tête :D


 


J'ai une application où il y a une map, avec des zones de geofencing..


 


Quand on rentre dans la zone, je veux déclencher une action (l'envoi d'un requête via Alamofire).


Quand je test en locale, aucun soucis, la requête par bien.


Quand je test en condition réelle, c'est à  dire avec un téléphone en 3G/4G, il détecte bien l'entrée dans la zone mais il n'envoi jamais la requête (pourtant il passe bien dedans).


 


Est-ce une limitation d'iOs ? de bloquer les requêtes d'une appli si elle n'est pas ouverte ?


 


En gros, mon application peut être fermée (complètement), elle détecte quand même l'entée dans une zone (car j'affiche une notification quand on entre dedans, l'application est fermée et pourtant j'ai bien la notification).


 


Dans mon AppDelegate, j'ai ça :



func handleEvent(forRegion region: CLRegion!) {
print("############")
print("Geofence triggered!")

if(debugLog){
fonctions().WriteLogInFile(text:"DANS HandleEvent")
}

if UIApplication.shared.applicationState == .active {
//print("application active !")
if let message = note(fromRegionIdentifier: region.identifier){
// Affiche la box - DEBUT
let appearance = SCLAlertView.SCLAppearance(
showCloseButton: false
)
let alert = SCLAlertView(appearance : appearance)
// bouton OK
_ = alert.addButton("OK", backgroundColor: uiColorApplication){
}
// Titre de la box
_ = alert.showEdit("ATTENTION",subTitle: "Vous êtes dans une zone à  risque ! \n\n"+message, colorStyle: uintColorApplicationShowEdit!)
// Affiche la box - FIN
}

}
else {
//print("LOCAL NOTIF")
if let message = note(fromRegionIdentifier: region.identifier){
let content = UNMutableNotificationContent()
content.title = "ATTENTION"
content.body = message
content.sound = .default()

let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 1, repeats: false)
let request = UNNotificationRequest(identifier: "NotifLocale", content: content, trigger: trigger)
UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)
}
}

if(debugLog){
fonctions().WriteLogInFile(text:"FIN HandleEvent")
}
}


extension AppDelegate: CLLocationManagerDelegate {

// Zone entrante
func locationManager(_ manager: CLLocationManager, didEnterRegion region: CLRegion) {
if region is CLCircularRegion {
if(debugLog){
fonctions().WriteLogInFile(text:"AVANT UpdateHistoriqueIncident")
}
updateHistoriqueIncident(type: "Entrant", region : region)
if(debugLog){
fonctions().WriteLogInFile(text:"AVANT HandleEvent")
}
handleEvent(forRegion: region)
}
}
}


func updateHistoriqueIncident(type : String, region : CLRegion){

if(debugLog){
fonctions().WriteLogInFile(text:"DANS UpdateHistoriqueIncident")
}

if let laGeotification = getGeotification(fromRegionIdentifier: region.identifier){ // On recupère la description
//print(type+" dans : "+String(laGeotification.note))

let Paramètres = [
"Token": Token,
"IdZone" : String(laGeotification.id),
"Type" : type
]

if(debugLog){
fonctions().WriteLogInFile(text:"DANS UpdateHistoriqueIncident - GEO OK")
}

if(!Token.isEmpty){
if(debugLog){
fonctions().WriteLogInFile(text:"DANS UpdateHistoriqueIncident - AVANT REQUETE")
fonctions().WriteLogInFile(text:urlMessenger+"/api/incidents/updateZoneHistorique")
fonctions().WriteLogInFile(text:String(laGeotification.id))
fonctions().WriteLogInFile(text:type)
}
_ = Alamofire.request(monUrl+"/api/incidents/updateZoneHistorique", method : .post, parameters: Paramètres)
if(debugLog){
fonctions().WriteLogInFile(text:"DANS UpdateHistoriqueIncident - APRES REQUETE")
}
}
else{
if(debugLog){
fonctions().WriteLogInFile(text:"DANS UpdateHistoriqueIncident - TOKEN VIDE - ELSE !")
}
}

}
}

Quand je suis en condition réelle, je passe bien dedans mais la requête n'est jamais envoyée..


Avez-vous une idée de pourquoi ça ne passe pas ? :/


Réponses

  • zoczoc Membre

    Quand iOS détecte une entrée/sortie de zone, il lance ton application en background. Si à  ce moment là  tu veux faire une requête réseau, je pense qu'il faut créer une "background task" pour ça. Tu peux prendre exemple sur ça:


     


    https://stackoverflow.com/questions/11864553/ios-http-request-while-in-background


     


    C'est de l'objective C, mais l'idée y est...

  • Je rentre de vacance et je vois une réponse, pfiouf, j'y croyais plus :p


     


    J'ai regardé mais en objective C, ça ne me parle pas trop..


    Quelqu'un aurait un exemple en Swift ?


    J'ai trouvé quelques trucs mais j'ai un peu de mal à  comprendre les "background task".. comment ça fonctionne, etc etc..


Connectez-vous ou Inscrivez-vous pour répondre.