2016-04-21 83 views
5

應用程序配置爲在後臺接收位置更新以保持應用程序處於活動狀態,並且在應用程序處於後臺時成功接收更新。如何在應用程序處於後臺時接收達爾文通知

達爾文通知也已配置,並且只有當應用程序是前臺的當前應用程序時纔會收到通知。只要應用程序放在後臺,它就會停止接收達爾文通知。

有關如何在應用程序處於後臺時接收達爾文通知的任何想法?

下面的代碼片段。

建築應用中Swift2

在appdeligate

let callback: @convention(c) 
    (CFNotificationCenter!, UnsafeMutablePointer<Void>, CFString!, UnsafePointer<Void>,  CFDictionary!) -> Void = { 
     (center, observer, name, object, userInfo) in 

    //Execute callback code 

    } 


    let exCb: CFNotificationCallback = unsafeBitCast(callback, CFNotificationCallback.self) 

CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(),nil ,exCb,"com.apple.springboard.hasBlankedScreen" as CFString,nil ,CFNotificationSuspensionBehavior.DeliverImmediately) 

在視圖 - 控制

locationManager = CLLocationManager() 
    locationManager.delegate = self 
    locationManager.desiredAccuracy = kCLLocationAccuracyBest 
    //locationManager.requestWhenInUseAuthorization() 
    locationManager.requestAlwaysAuthorization() 
    locationManager.startUpdatingLocation() 
+0

你有沒有嘗試過啓用後臺模式'位置更新'並檢查委託方法'locationManager:didUpdateLocations:'是否被稱爲 –

+0

@ Rich16你有沒有得到這個工作?如果是這樣,它仍然在iOS 10上工作嗎? – simonthumper

回答

0

請確保您已激活Background ModesLocation updates能力。

Location updates capability

這將使您的應用程序接收在後臺位置的變化,並與你的邏輯進行。 這種功能的偉大庫是INTULocationManager