gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 04:56:12 28 4
gpt4 key购买 nike

应用程序配置为在后台接收位置更新,以保持应用程序处于事件状态,并且当应用程序在后台时成功接收更新。

Darwin 通知也已配置,仅当该应用程序是前台的当前应用程序时才会收到通知。一旦应用程序进入后台,它就会停止接收 Darwin 通知。

关于如何在应用程序处于后台时接收 Darwin 通知有什么想法吗?

下面的代码片段。

在 Swift2 中构建应用

在应用委托(delegate)中

    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)

在 View Controller 中

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

最佳答案

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

Location updates capability

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

关于ios - 如何在应用程序处于后台时接收 Darwin 通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36757514/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com