小编Ric*_*h16的帖子

如何在应用程序处于后台时接收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)
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)

notifications darwin ios swift2 cfnotification

5
推荐指数
1
解决办法
1049
查看次数

标签 统计

cfnotification ×1

darwin ×1

ios ×1

notifications ×1

swift2 ×1