当应用程序在后台运行时如何接收“达尔文通知”

5

该应用程序已配置为在后台接收位置更新,以保持应用程序活动,并在应用程序处于后台时成功接收更新。

Darwin通知也已配置,并且仅在应用程序在前台时才会接收到。一旦将应用程序置于后台,它就停止接收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)

在视图控制器中

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

你尝试启用后台模式“位置更新”并检查委托方法locationManager:didUpdateLocations:是否被调用了吗? - Sunil Sharma
@Rich16,你最后解决了这个问题了吗?如果是这样,它在iOS 10上还能工作吗? - simonthumper
1个回答

0

请确保您已在后台模式中激活了位置更新功能。

Location updates capability

这将允许您的应用在后台接收位置更改并继续执行您的逻辑。 这种功能非常好的库是INTULocationManager


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接