What is required to register for CoreWLAN events?

I have a simple piece of demo code I am attempting to write for MacOS Yosemite that uses CoreWLAN to report on WiFi events. I've tried every combination I can think of Sandboxed, Signed, etc., all to no avail. Currently, I have the app set for Sandbox mode, and allowing outbound network connections in the entitlements. Any ideas as to how to get this to work? Sample code and scenario below:


import CoreWLAN
class WirelessMonitor: CWEventDelegate {
    func registerForEvents() {
        let client = CWWiFiClient.sharedWiFiClient()
        client.delegate = self
        var error:NSError?
        puts("Registering for SSID change...")
        client.startMonitoringEventWithType(.SSIDDidChange, error: &error)
        if (error != nil) {
            puts("error registering for SSID Change: \(error)")
        }
    }
    func ssidDidChangeForWiFiInterfaceWithName(interfaceName: String!) {
        print("ssid changed.")
    }
}



When I run this, I receive an error upon calling startMonitoringEventWithType(...):


"Couldn’t communicate with a helper application." (The connection to service named com.apple.airportd was invalidated.)


In the MaOS console, I see this message:


7/18/15 12:19:11.753 AM sandboxd[504]: ([4105]) WiFi Debugger(4105) deny mach-lookup com.apple.airportd

Accepted Reply

At this point I’m going to have to revisit this issue in depth and that’s not something I can do in the context of DevForums.

It turns out that a different developer open a DTS tech support incident about this issue and that gave me an excuse to re-test.

CWWiFiClient
is definitely working for me an 10.13.4. The only oddity, and I suspect that this is what’s tripped you up, is that this API is now gated by the
com.apple.security.network.client
entitlement. Please make sure to enable that entitlement (Xcode > project editor > target > Capabilities > App Sandbox > Outgoing Connections (Client)) and retest.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Replies

These are my entitlements

<key>com.apple.security.app-sandbox</key>
  <true/>
<key>com.apple.security.network.client</key>
  <true/>

Did you get that from the

.entitlements
file? Or from the built binary?

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Isn't working for me.

Line that fails:

scan = try (interface.scanForNetworks(withName: ssidName, includeHidden:true))


Error:

An error has occured  while scanning networks: Error Domain=NSCocoaErrorDomain Code=4099 "The connection to service on pid 0 named com.apple.airportd was invalidated from this process." UserInfo={NSDebugDescription=The connection to service on pid 0 named com.apple.airportd was invalidated from this process.}


codesign -d --entitlements  - DerivedData/airport-bssid/Build/Products/Debug/airport-bssid


Executable=/Users/maxcoplan/Documents/workspace/airport-bssid-swift/DerivedData/airport-bssid/Build/Products/Debug/airport-bssid



  com.apple.security.network.client
  
  com.apple.security.network.server
 

For some reason Apple's code formatter isn't working. Here's the raw output:

Executable=/Users/maxcoplan/Documents/workspace/airport-bssid-swift/DerivedData/airport-bssid/Build/Products/Debug/airport-bssid

��qq3<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">

<plist version="1.0">

<dict>

<key>com.apple.security.network.client</key>

<true/>

<key>com.apple.security.network.server</key>

<true/>

</dict>

</plist>

So I've been struggling with this lately on catalina with Xcode 11.3.1 but finally have a solution that works. I can listen for every event without an error except for virtualInterfaceStateChanged. This was throwing the vague error Domain=com.apple.wifi.request.error Code=4 "(null)". Below is my working implementation with a macOS project.

class WiFiService: CWEventDelegate {

  private let wifiClient = CWWiFiClient.shared()

  init() {
  self.wifiClient.delegate = self

  do {
  try self.wifiClient.startMonitoringEvent(with: .virtualInterfaceStateChanged)
  } catch {
  print("Failed to register for wifi events: \(error)")
  }
  }

  func ssidDidChangeForWiFiInterface(withName interfaceName: String) {
  guard let interface = self.wifiClient.interface(withName: interfaceName) else {
  return
  }
  print(interface.ssid())
  }
}

I hope this solves the problem for those that had this issue as it was the first result on google for me. Below are my entitlements. I figure because its fine with the Sandbox entitlement that I can push this to the appstore without issue?

<dict>
  <key>com.apple.security.app-sandbox</key>
  <true/>
  <key>com.apple.security.network.client</key>
  <true/>
</dict>
</plist>