Maps & Location

RSS for tag

Learn how to integrate MapKit and Core Location to unlock the power of location-based features in your app.

Maps & Location Documentation

Post

Replies

Boosts

Views

Activity

The RSSI obtained from CLBeacon is always 0
The beacon device is very close to the phone(ios17). But the RSSI obtained from the beacon information returned by the locationManager's didRange function is always 0. Why? log: didRange [CLBeacon (uuid:D4C3CCC0-29FB-11E5-884F-0002A5D5C51B, major:0, minor:9, proximity:0 +/- -1.00m, rssi:0, timestamp:2024-02-15 06:48:57 +0000)] didRange [CLBeacon (uuid:D4C3CCC0-29FB-11E5-884F-0002A5D5C51B, major:0, minor:9, proximity:0 +/- -1.00m, rssi:0, timestamp:2024-02-15 06:48:58 +0000)] didRange [CLBeacon (uuid:D4C3CCC0-29FB-11E5-884F-0002A5D5C51B, major:0, minor:9, proximity:0 +/- -1.00m, rssi:0, timestamp:2024-02-15 06:48:59 +0000)] didRange [CLBeacon (uuid:D4C3CCC0-29FB-11E5-884F-0002A5D5C51B, major:0, minor:9, proximity:0 +/- -1.00m, rssi:0, timestamp:2024-02-15 06:49:00 +0000)]
0
0
236
Feb ’24
UIKit mapView color annotations
I have tried to make colored annotations in mapView (shown in the commented sections) but they always appear in black. Any help would be appreciated. func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? { let annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: "TempAnnotationView") annotationView.canShowCallout = true annotationView.rightCalloutAccessoryView = UIButton(type: .detailDisclosure) let configuration = UIImage.SymbolConfiguration(pointSize: 10, weight: .thin, scale: .default) if annotation.title == "Start" { // let config = UIImage.SymbolConfiguration.preferringMulticolor() // let image = UIImage(systemName: "flag.fill", withConfiguration: config) // // palette // let config2 = UIImage.SymbolConfiguration(paletteColors: [.systemRed, .systemGreen, .systemBlue]) // let image2 = UIImage(systemName: "person.3.sequence.fill", withConfiguration: config2) // // hierarchical symbols // let config3 = UIImage.SymbolConfiguration(hierarchicalColor: .systemIndigo) // let image3 = UIImage(systemName: "square.stack.3d.down.right.fill", withConfiguration: config3) // // color // let image4 = UIImage(systemName: "cone.fill")?.withTintColor(.systemRed, renderingMode: .alwaysTemplate) // annotationView.image = image4 annotationView.image = UIImage(systemName: "poweron", withConfiguration: configuration) } return annotationView }
0
0
393
Feb ’24
Any control of MapKit font sizes?
I understand that MapKit automatically sizes the font based on the system Dynamic Type size. The thing is, the default font size is plenty legible for me system wide except for some type in MapKit, while other type in MapKit is already plenty big. For instance, the stream names are much harder to read than they should be by default. And if I increase the system Dynamic Type size, then it makes some MapKit text much larger than needed, while the stream text can still be hard to read. So is there anyway to override or adjust font sizes in MapKit? I'd like to be able to apply percentages to Dynamic Type suggestions. Like for streams, I'd like to scale it somewhere between 133% and 150%. The smallest Dynamic Type is size is .caption2 at 11 points with default settings. With default Dynamic Type settings, it looks like MapKit is drawing stream text around 7 points.
0
0
318
Feb ’24
visionOS: Moving window with a Map causes app to freeze
I'm developing a map-based app for visionOS. The loads map data from a server, using JSON. It works just fine, but I noticed the following effect: If I move the app's window around, it freezes; either on the first movement, or on one of the subsequent ones. The map cannot be panned anymore, and all other UI elements lose their interactivity as well. I noticed this issue before, when I was opening the map on app startup (and here it even happened without moving the window). Since I added a short delay, this was resolved. There was no log message in this case. However, when I noticed that it also happens if I move the window around, I saw that Xcode logs an error: +[UIView setAnimationsEnabled:] being called from a background thread. Performing any operation from a background thread on UIView or a subclass is not supported and may result in unexpected and insidious behavior. trace=( 0 UIKitCore 0x0000000185824a24 __42+[UIView(Animation) setAnimationsEnabled:]_block_invoke + 112 1 libdispatch.dylib 0x0000000102a327e4 _dispatch_client_callout + 16 2 libdispatch.dylib 0x0000000102a34284 _dispatch_once_callout + 84 3 UIKitCore 0x0000000185824ad8 +[UIView(Animation) performWithoutAnimation:] + 56 4 SwiftUI 0x00000001c68cf1e0 OUTLINED_FUNCTION_136 + 10376 5 SwiftUI 0x00000001c782bebc OUTLINED_FUNCTION_12 + 22864 6 SwiftUI 0x00000001c78285e8 OUTLINED_FUNCTION_12 + 8316 7 SwiftUI 0x00000001c787c288 OUTLINED_FUNCTION_20 + 39264 8 SwiftUI 0x00000001c787c2cc OUTLINED_FUNCTION_20 + 39332 9 UIKitCore 0x000000018582fc24 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 1496 10 QuartzCore 0x000000018a05cf00 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 440 11 QuartzCore 0x000000018a068ad0 _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 124 12 QuartzCore 0x0000000189f80498 _ZN2CA7Context18commit_transactionEPNS_11TransactionEdPd + 460 13 QuartzCore 0x0000000189fb00b0 _ZN2CA11Transaction6commitEv + 652 14 VectorKit 0x00000001938ee620 _ZN2md12HoverSupport18updateHoverProxiesERKNSt3__16vectorINS1_10shared_ptrINS_5LabelEEEN3geo12StdAllocatorIS5_N3mdm9AllocatorEEEEE + 2468 15 VectorKit 0x0000000193afd1cc _ZN2md15StandardLabeler16layoutForDisplayERKNS_13LayoutContextE + 156 16 VectorKit 0x0000000193cf133c _ZN2md16CompositeLabeler16layoutForDisplayERKNS_13LayoutContextE + 52 17 VectorKit 0x0000000193abf318 _ZN2md12LabelManager6layoutERKNS_13LayoutContextEPKNS_20CartographicRendererERKNSt3__113unordered_setINS7_10shared_ptrINS_12LabelMapTileEEENS7_4hashISB_EENS7_8equal_toISB_EEN3geo12StdAllocatorISB_N3mdm9AllocatorEEEEERNS_8PassListE + 2904 18 VectorKit 0x0000000193cad464 _ZN2md9realistic16LabelRenderLayer6layoutERKNS_13LayoutContextE + 464 19 VectorKit 0x0000000193658b54 _ZNSt3__110__function6__funcIZN2md9realistic20RealisticRenderLayer5frameERNS2_13LayoutContextEE3$_0NS_9allocatorIS7_EEFvvEEclEv + 180 20 VectorKit 0x00000001936584cc ___ZN3geo9TaskQueue14queueAsyncTaskENSt3__110shared_ptrINS_4TaskEEEPU28objcproto17OS_dispatch_group8NSObject_block_invoke + 80 21 libdispatch.dylib 0x0000000102a30f98 _dispatch_call_block_and_release + 24 22 libdispatch.dylib 0x0000000102a327e4 _dispatch_client_callout + 16 23 libdispatch.dylib 0x0000000102a3aa80 _dispatch_lane_serial_drain + 916 24 libdispatch.dylib 0x0000000102a3b7c4 _dispatch_lane_invoke + 420 25 libdispatch.dylib 0x0000000102a3c794 _dispatch_workloop_invoke + 864 26 libdispatch.dylib 0x0000000102a481a0 _dispatch_root_queue_drain_deferred_wlh + 324 27 libdispatch.dylib 0x0000000102a475fc _dispatch_workloop_worker_thread + 488 28 libsystem_pthread.dylib 0x0000000103b0f924 _pthread_wqthread + 284 29 libsystem_pthread.dylib 0x0000000103b0e6e4 start_wqthread + 8 I disabled all my withAnimation() statements, and the problem persists. I also thought it might be related to my own network fetches, but I think all apply their changes on the main thread. And when I turn on network logging for my own fetching logic, I do not see any data coming in. I also do not think there should be a reason for it. How can I debug such a situation, so I know, which call actually threw this message? I'd like to know if it is my code or a bug in the SwiftUI map itself.
3
0
636
Feb ’24
geolocation represented on 3d model
Hi Developers. I need to try to embed GeoData into a USDZ model and then have the capabilities to "explore" the model while walking around in the real world. e.g. load and display a 3D model of Time Square and show a location pin which displays a users' virtual position on the model... when the user walks 10 meters north, in the real world, the location pin must move 10m (to scale) on the displayed model. As a new developer, I'm starting from scratch so would benefit greatly from having my hand held - please don't be afraid of patronizing me. Step 1 - load and display the 3D model Step 2 - geolocate the user Step 3 - translate the user's current position into a position on the 3D model and represent their "position" on the model with a traditional GPS location marker "pin". Step 4 - move the user's GPS pin on the mode, relative to (and reflecting) their actual real world movement. Is this acheivable or have I explained things to ambiguously? I look forward to hearing from my savior(s) as soon as convenient. Thank you in advance
0
0
261
Feb ’24
CLMonitor is Not Working and No way to debug!
CLMonitor WWDC video, it says the same name can be reused await CLMonitor("greeting") but the production iOS 17 API actually crashes. Is this the correct behaviour? (See screenshot 1) for try await events in monitor.events should work in from the WWDC video, but the production iOS 17 API does not work and need to insert await after in keyword. Also, never received any events from the monitor (See screenshot 2) WWDC Video: https://developer.apple.com/wwdc23/10147
0
2
430
Feb ’24
Cannot select my created MKMapItem on the Map
Hi, I am learning about MapKit for SwiftUI, at first I created the MKMapItem @State private var mapSelection: MKMapItem? @State var starbucksReserve = MKMapItem(placemark: .init(coordinate: CLLocationCoordinate2D(latitude: 41.894178596474575, longitude: -87.62451598445733)))` I put that in the Map Map(position: $cameraPosition,selection: $mapSelection){ Marker("Starbucks Reserve", systemImage: "cup.and.saucer.fill" ,coordinate: starbucksReserve.placemark.coordinate) } Even though I have put the selection $mapSelection, my Marker is not selectable. I am not sure why. I tried to fix it by changing mapSelection to Int? and made a tag for Marker, but because I am learning how to use it so I want to make sure why marker cannot be selectable with the first approach? Thank you
0
0
294
Feb ’24
CLLocationManagerDelegate Bug on VIsionOS
I found something when I use CLLocationManagerDelegate on VisionOS. When I called func locationManagerDidChangeAuthorization func locationManagerDidChangeAuthorization(_ manager: CLLocationManager) { switch manager.authorizationStatus { case .notDetermined, .denied: locationManager.requestWhenInUseAuthorization() default: locationManager.startUpdatingLocation() guard let center = locationManager.location?.coordinate else { return } isFollowing = false mapCamera = .region(.init(center: center, span: .init(latitudeDelta: 0.005, longitudeDelta: 0.005))) addItems() } } Although I choose allow auth, locationManagerDidChangeAuthorization wasn't called.
0
0
267
Feb ’24
how to get user live location in Background and terminated mode?
Hi All, I need user continues location event if app in background and terminated (Not-running) mode below is the code I'm using to get the location but still I'm not get the location continuously import BackgroundTasks import UIKit class AppDelegate: UIResponder, UIApplicationDelegate { var significatLocationManager: CLLocationManager? func startSignificationLocation() { self.significatLocationManager = CLLocationManager() self.significatLocationManager?.delegate = self self.significatLocationManager?.activityType = .fitness self.significatLocationManager?.distanceFilter = 10 self.significatLocationManager?.desiredAccuracy = kCLLocationAccuracyNearestTenMeters self.significatLocationManager?.allowsBackgroundLocationUpdates = true self.significatLocationManager?.pausesLocationUpdatesAutomatically = false self.significatLocationManager?.startUpdatingLocation() self.significatLocationManager?.startMonitoringSignificantLocationChanges() self.significatLocationManager?.requestAlwaysAuthorization() } func stopAllRegionMonitoring(locationManager:CLLocationManager?){ for delRegion in (locationManager?.monitoredRegions ?? []){locationManager?.stopMonitoring(for: delRegion)} } } extension AppDelegate: CLLocationManagerDelegate { func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { self.myLocation = locations.last //call update My Location API if (self.myLocation?.horizontalAccuracy ?? 0.0) <= (self.liveLocationTrackingRegionRadius + 15.0 ){ self.createRegion(location: self.myLocation) } else { manager.stopUpdatingLocation() manager.startUpdatingLocation() } } func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {} func locationManagerDidPauseLocationUpdates(_ manager: CLLocationManager) { manager.startUpdatingLocation() } func locationManagerDidResumeLocationUpdates(_ manager: CLLocationManager) { manager.startUpdatingLocation() } } extension AppDelegate { func createRegion(location:CLLocation?, type:LocationRegionMonitoringTyep = .LiveLocationTracking) { if self.significatLocationManager == nil { self.startSignificationLocation() } guard let location = location else { return } guard CLLocationManager.isMonitoringAvailable(for: CLCircularRegion.self) else{ return } var identifier:String = "Live_Location_Tracking-" + "\(location.coordinate.latitude)" + "-" + "\(location.coordinate.longitude)" if (self.significatLocationManager?.monitoredRegions.count ?? 0) > 10 { self.stopAllRegionMonitoring(locationManager: self.significatLocationManager) } var region : CLCircularRegion? region = CLCircularRegion(center: CLLocationCoordinate2D(latitude: location.coordinate.latitude,longitude: location.coordinate.longitude),radius: 10.0 ,identifier: identifier) region?.notifyOnExit = true self.significatLocationManager?.startUpdatingLocation() if let reg = region { self.significatLocationManager?.startMonitoring(for: reg) } } func stopAllRegionMonitoring(locationManager:CLLocationManager?){ let WOMRequestId:String? = UserDefaultManager.shared.womEmergencyDetails?.data?.request?.id for delRegion in (locationManager?.monitoredRegions ?? []){ if let reqId = WOMRequestId { if delRegion.identifier.contains(reqId) { locationManager?.stopMonitoring(for: delRegion) } } else { locationManager?.stopMonitoring(for: delRegion) } } } func locationManager(_ manager: CLLocationManager, didEnterRegion region: CLRegion) { if region.identifier.contains(AppConstants.WatchOverMe) { AppDelegate.shared?.isWOMReachDestination(location: manager.location, region: region) } } func locationManager(_ manager: CLLocationManager, didExitRegion region: CLRegion) { self.myLocation = manager.location manager.stopMonitoring(for: region) self.significatLocationManager?.stopMonitoring(for: region) manager.startUpdatingLocation() self.significatLocationManager?.startUpdatingLocation() self.createRegion(location: self.myLocation) //update M yLocation API if region.identifier.contains( AppConstants.WatchOverMe ) { TabBarController.isWOMReachDestinationAlertShown = false } } func locationManager(_ manager: CLLocationManager, monitoringDidFailFor region: CLRegion?, withError error: Error) { self.myLocation = manager.location self.createRegion(location: self.myLocation) } } extension AppDelegate { func applicationDidEnterBackground(_ application: UIApplication) { self.scheduleBackgroundTask() } func application(_ application: UIApplication, performFetchWithCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) { completionHandler(.newData) } func registerBGTask(){ BGTaskScheduler.shared.register(forTaskWithIdentifier: self.getBGTaskIdentifier(), using: nil) { task in self.handleBackgroundTask(task: task as! BGAppRefreshTask) } } func handleBackgroundTask(task: BGAppRefreshTask) { task.setTaskCompleted(success: true) } func scheduleBackgroundTask() { let request = BGAppRefreshTaskRequest(identifier: self.getBGTaskIdentifier() ) request.earliestBeginDate = Date(timeIntervalSinceNow: 10) // 30 second do { try BGTaskScheduler.shared.submit(request) } catch { print("Unable to schedule background task: (error)") } } func getBGTaskIdentifier()->String { let bundleId:String = AppInfoManager.shared.bundleId + ".locationBackgroundTask" return bundleId } func startLocationTrackingTimer() { self.stopLocationTrackingTimer() self.locationTrackingTimer = Timer.scheduledTimer(timeInterval: 1.0,target: self, selector: #selector(self.updateUserLocation),userInfo: nil,repeats: true) RunLoop.current.add(self.locationTrackingTimer!, forMode: .common) } func stopLocationTrackingTimer() { self.locationTrackingTimer?.invalidate() self.locationTrackingTimer = nil } @objc func updateUserLocation() { if self.isSocketActive { self.updateMyLocationAPI(fromRoam: false) } else { self.updateUserCurrentLocation() } } }
0
0
351
Jan ’24
MapKit top MKTileOverlay blendMode overrides lower MKTileOverlay blendMode
I need multiple MKTileOverlays with multiple blendModes. Apparently using an overlay with a different blend causes the layer under to use the same blend mode. For the example below, using a normal blend mode on top of a soft light blend mode causes a normal blend mode to be used instead of soft light. The soft light layer is rendered as expected until the normal layer is displayed starting at zoom level 15. First, I've subclassed MKTileOverlay to add an overlay type so that the correct renderer is provided per overlay. (I know there is a title, but I prefer this) enum OverlayType { case softLight, normal } class TileOverlay: MKTileOverlay { var type: OverlayType = .normal } Then setup layers and renderers in the typical fashion: var softLightRenderer: MKTileOverlayRenderer! var normalRenderer: MKTileOverlayRenderer! private func setupSoftlightRenderer() { let overlay = TileOverlay(urlTemplate: "http://localhost/softlight/{z}/{x}/{y}") overlay.type = .softLight overlay.canReplaceMapContent = false overlay.minimumZ = 9 overlay.maximumZ = 20 mapView.addOverlay(overlay, level: .aboveLabels) softLightRenderer = MKTileOverlayRenderer(tileOverlay: overlay) tileRenderer.blendMode = .softLight } private func setupNormalRenderer() { let overlay = TileOverlay(urlTemplate: "http://localhost/normal/{z}/{x}/{y}") overlay.type = .normal overlay.canReplaceMapContent = false overlay.minimumZ = 15 overlay.maximumZ = 20 mapView.addOverlay(overlay, level: .aboveLabels) normalRenderer = MKTileOverlayRenderer(tileOverlay: overlay) normalRenderer.blendMode = .normal } func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer { if let overlay = overlay as? TileOverlay { switch overlay.type { case .softLight: return softLightRenderer case .normal: return normalRenderer } } print("Warning: using unhandled overlay renderer") return blankRenderer } override func viewDidLoad() { ... setupSoftlightRenderer() setupNormalRenderer() } Interestingly, if I put the overlays at two different levels, one .aboveLabels and another .aboveRoads it works as expected. The problem is that limits me to two different overlay blend modes. I really could use more than two. I tried every possible variation of inserting the layers at different indexes and methods, but the only two that seem to work are the .aboveLabels and .aboveRoads. How can I use more than two different blend modes?
1
0
375
Jan ’24
Join in Apple Developer and Find My Network as a Chinese College Student
Hi, I'm a college student in Hangzhou, China. I want to learn and develop with Find My Network and other useful Apple's tech. But when I access the application website, I can't find the university entrance but only with company. Can I develop as a college student? Where can I be? Or in the same page, what's the DUNS numbers? If only corps can, what's the fee to get the DUNS numbers with Apple's support? What's the fee to join in Apple Developments? Should I offer some documents such as Bank Corporate Account Statement? If I create a new corp to join in the development, what should I or corp need and do? I just wanna develop a school ID card with UWB tech and Apple's useful Find My Network. Hope to get your's help. Have a good day. Thanks!
0
0
342
Jan ’24
MapView mapType `.standard` iOS17 does not allow 3d
I am working on a virtual simulator for iOS 17 from my macbook. I want to have the initial view present the Map but zoomed out enough so that it's basically showing the whole world - in a 3d render. My understanding is that as of iOS17 if you have .standard for the MKMapView type, it should still render as 3D if the altitude is high enough. But on my simulator, I literally cannot zoom out any further. Is the right protocol to update the mapType to be .hybridFlyover? That seems different than the .standard (which I think is what I actually want). Does anyone have any idea / is there anything sticking out in this code? Here is my code: struct GlobeView: UIViewRepresentable { var coordinates: [IdentifiableCoordinate] func makeUIView(context: Context) -> MKMapView { let mapView = MKMapView() mapView.mapType = .hybridFlyover mapView.delegate = context.coordinator let camera = MKMapCamera(lookingAtCenter: CLLocationCoordinate2D(latitude: 0, longitude: 0), fromDistance: 1_000_000_000, pitch: 0, heading: 0) mapView.setCamera(camera, animated: true) return mapView } func updateUIView(_ uiView: MKMapView, context: Context) { let annotations = coordinates.map { coordinate -> MKPointAnnotation in let annotation = MKPointAnnotation() annotation.coordinate = coordinate.coordinate return annotation } uiView.addAnnotations(annotations) } class Coordinator: NSObject, MKMapViewDelegate { var parent: GlobeView init(_ parent: GlobeView) { self.parent = parent } } func makeCoordinator() -> Coordinator { Coordinator(self) } }
1
0
352
Jan ’24
Asking Location Permission is not working on the AppIntents
@MainActor perform() async throws -> some IntentResult { // ... switch locationManager.authorizationStatus { case .denied, .restricted: throw UserLocationError.permissionDenied case .notDetermined: await locationManager.requestWhenInUseAuthorization() // to ask permission default: break } // ... } Here is my code. When the authorizationStatus is .notDetermined, it invokes requestWhenInUseAuthorization() method on the main thread, but throws UserLocationError.permissionDenied immediately, eventhough I didn't deny the permission. It's really weird and unexpected that it throws UserLocationError.permissionDenied when the status is not .denied or .restricted Even it invokes requestWhenInUseAuthorization(), there's no alert for asking permission If there's any solution, please let me know
0
0
370
Jan ’24
Region monitoring not working after 1 hour the app is killed.
This is my setup: Granted always allow permission. I have location added in UIBackgroundModes, but I did NOT set allowsBackgroundLocationUpdates to true Note: I have this allowsBackgroundLocationUpdates = true set in my earlier version of app, which worked but we noticed it drained battery much faster, hence we removed all the settings that could affect battery. The location model is setup with 20 regions, when boundary crossing happen, app sends a local notification. This works fine when app is in foreground/background. If app is killed, the app receives notification for boundary crossing only once. Failed case for region monitoring: Setup region monitoring Kill the app cross the boundary, app sends a local notification. wait for 1 hour leave the device in same state (notification is not opened, app is still killed state) cross the boundary again expect a notification, but app did not register any event related to region monitoring. The console logs did not print anything in this second case. public class LocationViewModel: NSObject, ObservableObject { private let maxMonitoredRegions = 20 private var anyCancellable: AnyCancellable? private let locationManager: CLLocationManager @Published public var authorizationStatus: CLAuthorizationStatus @Published public var isMonitoringAvailable: Bool @Published public var monitoredRegions: [Region] @Published public var recentLocation: CLLocation? public var newlyEnteredRegionSignal = PassthroughSubject<CLRegion, Never>() public var recentLocationSignal = PassthroughSubject<CLLocation, Never>() public var authorizationStatusPublisher: Published<CLAuthorizationStatus>.Publisher { $authorizationStatus } public var isLocationEnabled: Bool { locationManager.authorizationStatus == .authorizedWhenInUse || locationManager.authorizationStatus == .authorizedAlways } public override init() { locationManager = CLLocationManager() authorizationStatus = locationManager.authorizationStatus isMonitoringAvailable = CLLocationManager.isMonitoringAvailable(for: CLCircularRegion.self) monitoredRegions = [] super.init() locationManager.delegate = self monitoredRegions.append(contentsOf: getMonitoredRegions()) requestLocation() } public func requestLocation() { locationManager.requestLocation() } public func startRegionMonitoring(regions: [CLRegion]) { guard isMonitoringAvailable else { return } stopRegionMonitoring() if regions.isEmpty { return } if regions.count <= 20 { for region in regions { locationManager.startMonitoring(for: region) } } else { for region in regions[0...maxMonitoredRegions-1] { locationManager.startMonitoring(for: region) } } } public func stopRegionMonitoring() { guard isMonitoringAvailable else { return } if monitoredRegions.isEmpty { return } for region in monitoredRegions { let monitoredRegion = LocationUtils.convertRegionToCLRegion(region) locationManager.stopMonitoring(for: monitoredRegion) } monitoredRegions.removeAll() } private func getMonitoredRegions() -> [Region] { let monitoredRegions = locationManager.monitoredRegions var regions = [Region]() for monitoredRegion in monitoredRegions { if let region = LocationUtils.convertCLRegionToRegion(monitoredRegion) { regions.append(region) } } return regions } public func stopMonitoring() { recentLocation = nil stopRegionMonitoring() } } extension LocationViewModel: CLLocationManagerDelegate { public func locationManagerDidChangeAuthorization(_ manager: CLLocationManager) { authorizationStatus = manager.authorizationStatus switch authorizationStatus { case .notDetermined: stopMonitoring() case .denied: stopMonitoring() case .authorizedAlways: break case .authorizedWhenInUse: // If user has requested whenInUse, request for always allow. locationManager.requestAlwaysAuthorization() @unknown default: break } if let location = manager.location { recentLocationSignal.send(location) recentLocation = location } } public func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { if let recentLocation = locations.last { self.recentLocation = recentLocation recentLocationSignal.send(recentLocation) } } public func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) { } public func locationManager(_ manager: CLLocationManager, didStartMonitoringFor region: CLRegion) { if let monitoredRegion = LocationUtils.convertCLRegionToRegion(region) { let oldRegion = monitoredRegions.first { $0.identifier == monitoredRegion.identifier } if oldRegion == nil { monitoredRegions.append(monitoredRegion) } } } public func locationManager(_ manager: CLLocationManager, monitoringDidFailFor region: CLRegion?, withError error: Error) { } public func locationManager(_ manager: CLLocationManager, didEnterRegion region: CLRegion) { newlyEnteredRegionSignal.send(region) } public func locationManager(_ manager: CLLocationManager, didExitRegion region: CLRegion) { newlyEnteredRegionSignal.send(region) } } When app is awaken due to location event on app delegate, we initialize this location model, and location manager, and remove old monitored regions, and call startMonitoringRegions again, to keep the regions updated. Please let me know if I'm missing any crucial information.
1
1
353
Jan ’24
Searching for an example of a proper iBeacon implementation for iOS 17
Hello, I'm looking for an end-to-end example project that shows a correct implementation for the new iBeacon CoreLocation APIs in iOS 17.0 (CLMonitor, etc.). Ideally the example would cover background waking as that is the area in which I am facing the largest challenges. Perhaps there's an Open Source project or an official Apple example? Thanks for pointing me in the right direction. Jeremy
3
0
719
Jan ’24