gpt4 book ai didi

ios - 框架背景中的信标检测

转载 作者:行者123 更新时间:2023-11-29 00:39:11 26 4
gpt4 key购买 nike

我有一个包含所有信标检测逻辑的框架和一个设置和拆除框架的示例应用程序。我想在应用程序被杀死后获得区域进入和退出通知。当逻辑在应用程序中时,我能够从应用程序获取通知。但是当逻辑在框架中时,我不会收到通知。我做错了什么?

import UIKit
import CoreLocation

extension AppDelegate: CLLocationManagerDelegate {

func registerForBeaconNotifications() {
let locationManager = CLLocationManager()
let region = CLBeaconRegion(proximityUUID: UUID(uuidString: "83f9daec-4cae-54f1-b64e-846f12345d05")!, major: 10, minor: 10, identifier: "iPhone 6 Beacon")

locationManager.delegate = self
region.notifyOnEntry = true
region.notifyOnExit = true
region.notifyEntryStateOnDisplay = true

locationManager.startMonitoring(for: region)
locationManager.startRangingBeacons(in: region)

// Register for showing notification alerts
UIApplication.shared.registerUserNotificationSettings(UIUserNotificationSettings(types: .alert, categories: nil))
}

func locationManager(_ manager: CLLocationManager, didDetermineState state: CLRegionState, for region: CLRegion) {
let notification = UILocalNotification()

switch state {
case .inside:
notification.alertBody = "Entered region"
UIApplication.shared.presentLocalNotificationNow(notification)

case .outside:
notification.alertBody = "Exited region"
UIApplication.shared.presentLocalNotificationNow(notification)

default:
notification.alertBody = "Region unknown"
UIApplication.shared.presentLocalNotificationNow(notification)
}
}
}

最佳答案

为了防止停止监控的垃圾收集,locationManager 需要是一个类变量,并且初始化必须发生在方法内部。像这样:

let locationManager: CLLocationManager!

func registerForBeaconNotifications() {
self.locationManager = CLLocationManager()
...

关于ios - 框架背景中的信标检测,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39913085/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com