Raviprakash
Raviprakash

Reputation: 2440

How do I get current network location name?

In system network preference there are some location names.How to get the current or active network location name and list of all network locations?
I guess SystemConfiguration.framework supports this but i didn't get exactly which API to use.

Thanks in advance for your answer.

Regards
Devara Gudda

Upvotes: 3

Views: 2615

Answers (2)

Francesco Germinara
Francesco Germinara

Reputation: 508

This is a swift 5.1 implementation to get list of all available network location

    func getAvailabeNetworkLocation() -> [String]
{
    var results = [String]()

    
    var prefs : SCPreferences
    prefs = SCPreferencesCreate (nil, "Softech" as CFString, nil)!
    var sets : CFArray
    sets = SCNetworkSetCopyAll (prefs)!
    
    let count = CFArrayGetCount(sets) as Int
    var newSet : SCNetworkSet? = nil
    var bFound : Bool = false
    
   

    
    for nIndex  in 0..<count {
     let mySet = CFArrayGetValueAtIndex (sets, nIndex)
     let key = Unmanaged<SCNetworkSet>.fromOpaque(mySet!)
        newSet = key.takeUnretainedValue()
        let name : String = SCNetworkSetGetName(newSet!)! as String
        print("Network location name: \(name)")
     results.append(name)
    }
    return results
}

Upvotes: 1

outis
outis

Reputation: 77450

You can use SCPreferencesCreate to get the preferences, then SCNetworkSetCopyAll to get just the network locations. SCNetworkSetGetName will get the name of a location.

SCPreferencesRef prefs = SCPreferencesCreate(NULL, @"SystemConfiguration", NULL);
NSArray *locations = (NSArray *)SCNetworkSetCopyAll(prefs);
for (id item in locations) {
    NSString *name = (NSString *)SCNetworkSetGetName((SCNetworkSetRef)item);
    ...
}
CFRelease(locations);
CFRelease(prefs);

Read "System Configuration Programming Guidelines" for more.

Upvotes: 7

Related Questions