应用程序配置为在后台接收位置更新,以便在应用程序处于后台时保持应用程序处于活动状态并成功接收更新.
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) 我需要发送一个对象CFNotificationCenterGetDarwinNotifyCenter()然后,我注意到每当我使用发送通知时
const void *tkeys[1] = { @"testKey" };
const void *tvalues[1] = { @"testValue" };
CFDictionaryRef userInfo = CFDictionaryCreate(NULL, tkeys, tvalues, 1, NULL, NULL);
CFNotificationCenterPostNotification(CFNotificationCenterGetDarwinNotifyCenter(),
CFSTR("foo"),
object,
userInfo,
true);
Run Code Online (Sandbox Code Playgroud)
然后我发现在观察者回调中,userInfo dict为NULL.这里有什么我想念的吗?是否有其他方式可以发送userInfo对象?我正在查看http://nshipster.com/inter-process-communication/和分布式通知部分,但每当我使用时:
CFNotificationCenterRef distributedCenter =
CFNotificationCenterGetDistributedCenter();
Run Code Online (Sandbox Code Playgroud)
然后Invalid Use of Function每当我尝试它时都会出现错误错误.