簡體   English   中英

如何在應用程序處於后台時接收Darwin通知

[英]How to Receive Darwin Notifications when app is in background

應用程序配置為在后台接收位置更新,以便在應用程序處於后台時保持應用程序處於活動狀態並成功接收更新。

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)

在viewcontroller中

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

確保您已在Background Modes激活了Location updates功能。

位置更新功能

這將允許您的應用在后台接收位置更改並繼續您的邏輯。 這種功能的一個很棒的庫是INTULocationManager

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM