ios - 当应用程序的 "location"和 "background app refresh"权限更改时在后台收到通知

标签 ios cllocationmanager background-process nsnotificationcenter user-permissions

有谁知道当应用权限发生变化时,应用是否可以在后台收到系统通知?我对在用户更改“位置”权限和“后台应用程序刷新”权限时收到通知特别感兴趣。

到目前为止,我已经研究了这个回调方法:

locationManager(manager: CLLocationManager!, didChangeAuthorizationStatus status: CLAuthorizationStatus)

但是,应用程序不会在后台收到通知。

我还尝试为这个通知添加一个观察者:

UIApplicationBackgroundRefreshStatusDidChangeNotification

但我也无法让它工作。

这是我的代码:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {        
    NSNotificationCenter.defaultCenter().addObserver(self, selector: "checkBackgroundPermission", name: UIApplicationBackgroundRefreshStatusDidChangeNotification, object: nil)

    locationManager = CLLocationManager()
    locationManager.delegate = self
    locationManager.pausesLocationUpdatesAutomatically = false

    if(locationManager.respondsToSelector("requestAlwaysAuthorization")) {
        locationManager.requestAlwaysAuthorization()
    }

    if launchOptions?[UIApplicationLaunchOptionsLocationKey] != nil {
        locationManager = CLLocationManager()
        locationManager.delegate = self
        locationManager.pausesLocationUpdatesAutomatically = false

        let prefs:NSUserDefaults = NSUserDefaults.standardUserDefaults()
        let uuidString = prefs.stringForKey("UUID") as String!
        let uuid : NSUUID? = NSUUID(UUIDString: uuidString as String)

        beaconRegionGeneral = CLBeaconRegion(proximityUUID: NSUUID(UUIDString: uuidString), identifier: "general")

        locationManager.startRangingBeaconsInRegion(beaconRegionGeneral)
        setIsRanging(true)
    }


    if(application.respondsToSelector("registerUserNotificationSettings:")) {
        application.registerUserNotificationSettings(
            UIUserNotificationSettings(
                forTypes: UIUserNotificationType.Alert | UIUserNotificationType.Sound,
                categories: nil
            )
        )
    }

    return true
}

func checkBackgroundPermission() {
    sendLocalNotificationWithMessage("checkBackgroundPermission", playSound: false)
    println("checkBackgroundPermission")
}

func locationManager(manager: CLLocationManager!, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
    sendLocalNotificationWithMessage("didChangeAuthorizationStatus", playSound: true)
    println("didChangeAuthorizationStatus")
}

最佳答案

您可以使用以下委托(delegate)方法(来自 CLLocationManagerDelegate 协议(protocol))来获取通知。

 optional func locationManager(_ manager: CLLocationManager!, didChangeAuthorizationStatus status: CLAuthorizationStatus)

关于ios - 当应用程序的 "location"和 "background app refresh"权限更改时在后台收到通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29857711/

相关文章:

ios - 后台任务结束时ios有通知吗?

swift - 如何在swift的后台进程子函数中将值返回给函数

iphone - 这个 applicationDidEnterBackground 方法有什么问题?

iOS - 如何在应用程序处于后台时检测网络变化

iphone - ERROR : You are not the current participant. 当我是当前参与者时。什么?

ios - Objective-C ,iOS 7 - CLLocationManager : didUpdateLocations is being called only once

ios - iOS:降低背景位置更新的频率

ios - swift 。在模拟器上播放良好的 mp3 文件,但在设备上播放时没有音量。 AV播放器

iphone - 为 UIPickerView 生成日期逻辑

iOS 9 即使应用程序终止也如何获取位置