0

I'm working on an alternative to the wireless network organization and diagnostics utilities provided by Apple. So far it's going fairly well. I'm able to pull and store the current network, some network details, password, and location data and store it in an encrypted realm.

The area I'm running into issues with is being able to access a list of the networks that are currently in range of the computer. I know this can be accessed by using the "airport -s" command from the terminal. My initial plan was to call this command from an NSTask and parse and store the results. However, after hours of tinkering and googling and coming up empty, I finally came upon Objective-C: NSCommand "airport -s" returning empty which seems to reflect what I'm seeing.

I've to get around this by writing a quick bash script to store in my bundle resources and call from an NSTask and return the output, or even to call a script that will write the output from "airport -s" to a .txt file in the bundle resources... but these all seem to fail when initiated from an NSTask even though they all run fine from the terminal.

    func runCommand(cmd : String, args : String...) -> (output: [String], error: [String], exitCode: Int32) {

    var output : [String] = []
    var error : [String] = []

    let task = NSTask()
    task.launchPath = cmd
    task.arguments = args

    let outpipe = NSPipe()
    task.standardOutput = outpipe
    let errpipe = NSPipe()
    task.standardError = errpipe

    task.launch()

    let outdata = outpipe.fileHandleForReading.readDataToEndOfFile()
    if var string = String.fromCString(UnsafePointer(outdata.bytes)) {
        string = string.stringByTrimmingCharactersInSet(NSCharacterSet.newlineCharacterSet())
        output = string.componentsSeparatedByString("\n")
    }

    let errdata = errpipe.fileHandleForReading.readDataToEndOfFile()
    if var string = String.fromCString(UnsafePointer(errdata.bytes)) {
        string = string.stringByTrimmingCharactersInSet(NSCharacterSet.newlineCharacterSet())
        error = string.componentsSeparatedByString("\n")
    }

   task.waitUntilExit()
    let status = task.terminationStatus

    return (output, error, status)
}

Is the method I'm using elsewhere to utilize NSTask. I'm attempting get a list of nearby networks with the following code:

    func findNearby() -> String {

    var checkNearby = sb.runCommand("/bin/sh", args: "-c", "sh '/Users/<username>/Library/Containers/com.user.app/Data/Library/Application Support/getnearby.sh'")

    if checkNearby.output != [] {
        return checkNearby.output[0]
    }
    return "could not access airport"
}

Is there an issue with my implementation of NSTask that is causing these scripts to time out, or is Apple really going through all the trouble to stop me from calling "airport -s" from NSTask?

Are there any alternatives to "airport -s" I could try?

Note: I know I'm not really storing the .sh file in my Resources, this location was just temporary while I tried to figure out a solution.

Community
  • 1
  • 1
beatsbears
  • 201
  • 2
  • 7
  • 1
    Did that answer help you? Is your app sandboxed? – jtbandes Aug 29 '15 at 03:02
  • That answer only helped in that it explained why I was getting empty results. It doesn't seem to matter whether my app is sandboxed or not, it always returns empty for "airport -s", however I call "airport -I" multiple times in the app, and those calls all work as expected. – beatsbears Aug 29 '15 at 03:47

0 Answers0