2016-08-23 5 views
0

Ich versuche, eine Beacon-Hintergrunderkennung in meiner App auszuführen. Ich habe eine spezielle Klasse für diese und starten Sie es in meinem Viewcontroller:Swift 2 Beacon Scan im Hintergrund

override func viewDidLoad() { 
     super.viewDidLoad() 
     beaconSharedInstance 
     .... 
    } 

und hier ist der Code meiner Klasse:

let beaconSharedInstance = iBeacon(); 

class iBeacon: NSObject, UIApplicationDelegate, CLLocationManagerDelegate { 

var locationManager: CLLocationManager! 
var beaconRegion:CLBeaconRegion! 

override init(){ 

    super.init() 

    let uuidString = "FDA50693-A4E2-4FB1-AFCF-C6EB07640978" 
    let beaconIdentifier = "uby-06B524" 
    let beaconUUID:NSUUID = NSUUID(UUIDString: uuidString)! 
    beaconRegion = CLBeaconRegion(proximityUUID: beaconUUID, identifier: beaconIdentifier) 

    locationManager = CLLocationManager() 
    locationManager.delegate = self 

    if(CLLocationManager.authorizationStatus() != CLAuthorizationStatus.AuthorizedAlways){ 
     locationManager.requestAlwaysAuthorization() 
    } 

    locationManager.allowsBackgroundLocationUpdates = true 
    locationManager!.startRangingBeaconsInRegion(beaconRegion) 
} 


func locationManager(manager: CLLocationManager, 
        didRangeBeacons beacons: [CLBeacon], 
            inRegion region: CLBeaconRegion) { 
    print("didRangeBeacons"); 

    if(beacons.count > 0) { 

     print("Discovered beacon \(beacons[0].rssi) dBM name: \(beacons[0].proximityUUID)") 
    } 


} 

}

diese Klasse perfekt im Vordergrund arbeitet , aber sobald meine Anwendung inaktiv wird, kann ich in den Log-Dateien sehen:

Ending background task 

und der CLLocatioManager stoppt ...

Ich setze Standort-Updates im Hintergrund-Modus meines Projekts sowie NSLocationAlwaysUsageDescription in meiner Datei info.plist, aber ich habe immer noch das gleiche Verhalten, mein Thread stoppt, wenn App inaktiv ist.

Ich habe auch versucht, mein Thema wie folgt zu starten:

let qualityOfServiceClass = DISPATCH_QUEUE_PRIORITY_DEFAULT 
    let backgroundQueue = dispatch_get_global_queue(qualityOfServiceClass, 0) 
    dispatch_async(backgroundQueue, { 
     print("Running beacon detection in the background queue") 
     beaconSharedInstance 
    }) 

aber in diesem Fall CLLocationManager nie beginnt ... eine Idee?

+0

Woher kommt das Protokoll "ending background task"? Verwenden Sie 'beginBackgroundTaskWithExouarininHandler'? – Paulw11

Antwort

0

Die Hintergrundaufgabe muss eine Schleife haben, andernfalls wird sie einfach beendet. Versuchen Sie:

dispatch_async(backgroundQueue, { 
    print("Running beacon detection in the background queue") 
    beaconSharedInstance 
    while(true) { 
     NSThread.sleepForTineInterval(1) 
     print("running...") 
    } 
}) 
+0

bist du sicher? weil, wenn meine App im Vordergrund ist, Scan wird jede Sekunde durchgeführt ... – tiamat

+0

durch Hinzufügen Ihres Codes, ich habe nur ausgeführt ... läuft ... läuft ... läuft ... läuft ... läuft ... läuft ... läuft ... läuft ... läuft ... aber CLLocationManager beginnt immer – tiamat

+0

Richtig, das ist gut - es bedeutet, dass die Hintergrundaufgabe tatsächlich ausgeführt wird und es bleibt Laufen. Siehst du nicht "didRangeBeacons"? Wenn nicht, könnte das nahelegen, dass sich keine Baken in der Nähe befinden. Ein weiterer Gedanke: Es ist möglich, dass die Init-Sequenz ein Problem ist. Versuchen Sie, 'beaconSharedInstance = iBeacon();' kurz vor der while (true) zu verschieben, und wee, was passiert. – davidgyoung

Verwandte Themen