-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Fix location permission not resolving on iOS (#2672)
* fix: Fix location permission not resolving on iOS * fix: Remove unused code * fix: Ignore first call * Use `GlobalReferenceHolder`
- Loading branch information
Showing
6 changed files
with
88 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
package/ios/Extensions/CLLocationManager+requestAccess.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
// | ||
// CLLocationManager+requestAccess.swift | ||
// VisionCamera | ||
// | ||
// Created by Marc Rousavy on 21.03.24. | ||
// | ||
|
||
import CoreLocation | ||
import Foundation | ||
|
||
extension CLLocationManager { | ||
enum AccessType { | ||
case whenInUse | ||
case always | ||
} | ||
|
||
static func requestAccess(for accessType: AccessType, _ callback: @escaping (_ status: CLAuthorizationStatus) -> Void) { | ||
let manager = CLLocationManager() | ||
let delegate = CLLocationManagerCallbackDelegate(locationManager: manager, callback: callback) | ||
manager.delegate = delegate | ||
switch accessType { | ||
case .whenInUse: | ||
manager.requestWhenInUseAuthorization() | ||
case .always: | ||
manager.requestAlwaysAuthorization() | ||
} | ||
} | ||
|
||
private class CLLocationManagerCallbackDelegate: GlobalReferenceHolder, CLLocationManagerDelegate { | ||
private let locationManager: CLLocationManager | ||
private let callback: (_ status: CLAuthorizationStatus) -> Void | ||
|
||
init(locationManager: CLLocationManager, callback: @escaping (_ status: CLAuthorizationStatus) -> Void) { | ||
self.locationManager = locationManager | ||
self.callback = callback | ||
super.init() | ||
makeGlobal() | ||
} | ||
|
||
private var authorizationStatus: CLAuthorizationStatus { | ||
if #available(iOS 14.0, *) { | ||
return locationManager.authorizationStatus | ||
} else { | ||
return CLLocationManager.authorizationStatus() | ||
} | ||
} | ||
|
||
func locationManagerDidChangeAuthorization(_: CLLocationManager) { | ||
if authorizationStatus == .notDetermined { | ||
// This method is called once on init with status "notDetermined", ignore the first call. | ||
return | ||
} | ||
|
||
removeGlobal() | ||
callback(authorizationStatus) | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// | ||
// GlobalReferenceHolder.swift | ||
// VisionCamera | ||
// | ||
// Created by Marc Rousavy on 21.03.24. | ||
// | ||
|
||
import Foundation | ||
|
||
class GlobalReferenceHolder: NSObject { | ||
private static var references: [GlobalReferenceHolder] = [] | ||
|
||
func makeGlobal() { | ||
GlobalReferenceHolder.references.append(self) | ||
} | ||
|
||
func removeGlobal() { | ||
GlobalReferenceHolder.references.removeAll(where: { $0 == self }) | ||
} | ||
} |