如何在应用程序处于后台时接收Darwin通知

Ric*_*h16 5 notifications darwin ios swift2 cfnotification

应用程序配置为在后台接收位置更新,以便在应用程序处于后台时保持应用程序处于活动状态并成功接收更新.

Darwin通知也已配置,仅在应用程序是前台的当前应用程序时才会收到.一旦应用程序被置于后台,它就会停止接收达尔文通知.

有关如何在应用程序处于后台时收到Darwin通知的任何想法?

代码片段如下.

在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)
Run Code Online (Sandbox Code Playgroud)

在viewcontroller中

    locationManager = CLLocationManager()
    locationManager.delegate = self
    locationManager.desiredAccuracy = kCLLocationAccuracyBest
    //locationManager.requestWhenInUseAuthorization()
    locationManager.requestAlwaysAuthorization()
    locationManager.startUpdatingLocation()
Run Code Online (Sandbox Code Playgroud)

Kir*_*lex 0

确保您已激活Location updates中的功能Background Modes

位置更新能力

这将允许您的应用程序在后台接收位置更改并继续执行您的逻辑。此类功能的一个很棒的库是INTULocationManager