Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/110.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用地理围栏/后台位置更新触发通知时,iOS蓝色状态栏_Ios_Core Location_Uilocalnotification - Fatal编程技术网

使用地理围栏/后台位置更新触发通知时,iOS蓝色状态栏

使用地理围栏/后台位置更新触发通知时,iOS蓝色状态栏,ios,core-location,uilocalnotification,Ios,Core Location,Uilocalnotification,我有一个应用程序代码,可以在后台更新位置 locationManager.requestAlwaysAuthorization() locationManager.delegate = self locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters // less batery ussage locationManager.pausesLocationUpdatesAutomatically = false loc

我有一个应用程序代码,可以在后台更新位置

locationManager.requestAlwaysAuthorization()
locationManager.delegate = self

locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters // less batery ussage
locationManager.pausesLocationUpdatesAutomatically = false
locationManager.allowsBackgroundLocationUpdates = true
我想根据用户的地理位置触发本地通知

notificationCenter.requestAuthorization(options: [.alert, .badge, .sound]) { [weak self] (granted, error) in
            guard let self = self else { return }
            if granted {
                print("NotificationCenter Authorization Granted!")

                self.notificationCenter.removePendingNotificationRequests(withIdentifiers: [model.id])

                let content = UNMutableNotificationContent()
                content.title = model.name
                content.body = model.desc

                content.categoryIdentifier = "Location Notification"
                content.userInfo["model_id"] = model.id
                content.sound = UNNotificationSound.default

                let region = self.makeRegion(for: model)

                let trigger = UNLocationNotificationTrigger(region: region, repeats: true)
                let request = UNNotificationRequest(identifier: restaurant.id, content: content, trigger: trigger)

                self.notificationCenter.add(request)
            }
        }

locationManager.startUpdatingLocation()
现在我有一个蓝色的状态栏指示器,当应用程序在前台或后台时,它会一直更新位置。我认为一直出现对最终用户来说是一种淫秽

我听说这将在核心位置的用户授权使用时显示。所以据我所知,我应该只在应用程序处于前台时启用触发此通知(但这里需要做哪些更改?
allowsBackgroundLocationUpdates=false
?)

并且仅当用户已授予“始终授权”状态时才使用后台用户位置更新

但奇怪的是,当应用程序在前台时,这个蓝色的巴德指示器也会出现

更新

好的,我设置了“询问下一次”,然后始终显示蓝色指示灯

如果在使用过程中有蓝色指示灯,则蓝色指示灯仅在背景中

这里总是没有蓝色的指示灯

我考虑添加这样的委托方法< /P>

func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {

        print("Core Location authorization status: \(status)")

        switch status {
        case .authorizedAlways:
            locationManager.allowsBackgroundLocationUpdates = true
        default:
            locationManager.allowsBackgroundLocationUpdates = false
        }
    }
我认为应该解决状态栏中蓝色指示器的突出显示问题。 唯一的问题是,如果iOS 13中的用户在使用时进行选择,则不会尝试在后台更新位置,因此最终不会显示关于“始终授权”问题的警报,唯一的可能是用户手动启用“始终处于”设置

因此,我这里的问题是如何在iOS 13中以这种方式使带有“始终授权”问题的警报出现,然后才更改

.allowsBackgroundLocationUpdates = true


问题是你所做的一切都是错的

您没有进行后台位置监视,因此不应总是请求授权。你不需要它,最终你也得不到它。你应该询问何时使用授权,仅此而已。(这对于通知地理围栏触发器来说已经足够了。)

至于蓝色条,规则和以往一样:您应该设置

locationManager.allowsBackgroundLocationUpdates = true
仅当您即将进入后台,而您已经在更新位置时,您应该设置

locationManager.allowsBackgroundLocationUpdates = false
当你回到前台。如果您在更新位置时没有实际在后台运行(具有后台功能),则根本不要使用
allowsBackgroundLocationUpdates


只要做你们应该做的,一切都会好起来。

但我也想在应用程序处于后台时触发通知。不仅当应用程序处于前台时。我理解用户应该有可能在使用时不允许我选择,而不是始终授权。但现在,当我将allowsBackgroundLocationUpdates设置为false时,就没有机会询问这个始终授权。或者我应该在应用程序进入后台应用程序WillEnterBackground时,以某种方式更改此allowsBackgroundLocationUpdates。但locationManager应该是singleton或在AppDelegate上定义的?“但我想在应用程序处于后台时触发通知”我不知道这意味着什么。当您配置通知并将其交给系统时,您将无法控制它。即使你的应用程序没有运行,系统也会触发它。您不需要始终对此进行授权,而且您从来都不需要它。好吧,它似乎在locationManager.allowsBackgroundLocationUpdates=false的后台工作,所以我认为我被教程误导了,在教程中,为了发送此地理围栏触发的通知,将此属性设置为true。
locationManager.allowsBackgroundLocationUpdates = false