I'm trying to do the same from a command line utility written in Swift. I enabled "Location" resource access under Signing & Capabilities. I also tried adding an Info.plist file with the "Create Info.plist Section in Binary", so I could include a location authorization description. I see the prompt for location access and am able to get location updates, but the bssid is still nil. Here's my code:import CoreWLAN
import CoreLocation
class Delegate: NSObject, CLLocationManagerDelegate {
func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
print("changed auth status: \(status.rawValue)")
if status == .authorizedAlways {
print("Scanning for WiFi networks")
let interface = CWWiFiClient.shared().interface()!
let networks = try! interface.scanForNetworks(withName: nil)
for n in networks {
print("\(n.ssid) - \(n.bssid)")
}
}
}
func locationManager(_ manager: CLLocationManager, didUpdateTo newLocation: CLLocation, from oldLocation: CLLocation) {
print("Got location: \(newLocation)")
}
}
let delegate = Delegate()
let lm = CLLocationManager()
lm.requestAlwaysAuthorization()
lm.startUpdatingLocation()
lm.delegate = delegate
RunLoop.main.run()
print("exiting")
Post
Replies
Boosts
Views
Activity
Filed FB8831937.
A slightly different topic, but I hope this may be helpful to others in the future.
I had a similar issue, but I was looking for IODisplayConnect for the purposes of reading the display brightness.
On a M1 Mac with Big Sur, I found the easiest way to get the brightness info is by running corebrightnessdiag (this can also read keyboard backlight brightness):
$ /usr/libexec/corebrightnessdiag status-info | grep 'DisplayServicesBrightness '
DisplayServicesBrightness = "0.9302966";
To get and set brightness from code, you can use private APIs from the DisplayServices framework (/System/Library/PrivateFrameworks/DisplayServices.framework):
extern int DisplayServicesGetBrightness(int display, float *brightness);
extern int DisplayServicesSetBrightness(int display, float brightness);
/* Change brightness */
float brightness = 0.8;
int err = DisplayServicesSetBrightness(1, brightness);
/* Get current brightness */
err = DisplayServicesGetBrightness(1, &brightness);
This thread addresses the same question: https://developer.apple.com/forums/thread/666383
In case it helps anyone, with some effort I was able to write a MDImporter plugin in Swift: https://github.com/foxglove/MCAPSpotlightImporter
It would’ve been much nicer to write it as a CSImportExtension, and it does seem that mdimport on macOS has some references to “modern importers” (see mdimport -help), but as mentioned above, I wasn’t able to get CSImportExtensions to work at all on macOS. I saw some logs related to them so it seems like it is partially implemented, but somehow broken.
The Swift MDImporter will work as a stand-in for now, but I look forward to replacing it someday!
I've just tried installing the 17.2 Public Beta (21C5029g) on my device. I have the profile installed. The error is different than it was on the 17.1 full release build, but still things are not working. My code looks like this:
Task {
print("creating THClient")
let cli = THClient() // on 17.1, this would log an XPC error and hang indefinitely, so...progress?
print("created THClient")
print("is preferred available: \(await cli.isPreferredAvailable())")
do {
let creds = try await cli.allCredentials()
print("All creds: \(creds)")
} catch {
print("Error: \(error)")
}
do {
try await cli.storeCredentials(forBorderAgent: borderAgentID, activeOperationalDataSet: dataset)
print("Added creds")
} catch {
print("Error adding creds: \(error)")
}
do {
let creds = try await cli.allCredentials()
print("All creds: \(creds)")
} catch {
print("Error: \(error)")
}
}
The logs I get are:
creating THClient
created THClient
is preferred available: false
Client: -[THClient getConnectionEntitlementValidity]_block_invoke - Error:
Error: Error Domain=ThreadCredentialsStore Code=3 "Failed to retrieve all active border router records" UserInfo={NSLocalizedDescription=Failed to retrieve all active border router records, NSUnderlyingError=0x28198aeb0 {Error Domain=NSOSStatusErrorDomain Code=0 "(null)"}}
Error adding creds: Error Domain=ThreadCredentialsStore Code=4 "Invalid parameter sent to server..." UserInfo={NSLocalizedDescription=Invalid parameter sent to server...}
Error: Error Domain=ThreadCredentialsStore Code=3 "Failed to retrieve all active border router records" UserInfo={NSLocalizedDescription=Failed to retrieve all active border router records, NSUnderlyingError=0x281980ff0 {Error Domain=NSOSStatusErrorDomain Code=0 "(null)"}}
-[THClient storeCredentialsForBorderAgent:activeOperationalDataSet:completion:]_block_invoke:660: - Response: Error Domain=ThreadCredentialsStore Code=4 "Invalid parameter sent to server..." UserInfo={NSLocalizedDescription=Invalid parameter sent to server...}
I also just tried try await cli.credentials(forBorderAgentID: borderAgentID), which immediately crashes with
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '+[NSError storeError:description:]: unrecognized selector sent to class 0x1f9f21378'
...as well as try await cli.credentials(forExtendedPANID: extendedPANID), which crashes with
:0: Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value
:(
(Also worth mentioning: when I call these same APIs on macOS, they seem to silently do nothing without reporting any errors, which is not much better than hanging or crashing!)