Imagine that on your hard drive you had a local collection of public wi-fi names and passwords such as those that are found in coffeeshops. And you enter a coffeeshop where you have never been before, and an OSX app matches an available wifi network as one on the list and signs you in. Is this possible with objective c?
Asked
Active
Viewed 2,395 times
1
-
This is automatically done by OS X. – Amin Negm-Awad Apr 13 '15 at 04:59
1 Answers
8
You can use CoreWLAN to scan, connect, disconnect and etc. to WiFi networks. Here is a short and simplified example how to scan for networks and connect to a network:
#import <CoreWLAN/CoreWLAN.h>
CWInterface *cwInterface = [[CWInterface alloc] initWithInterfaceName:@"en1"]; // specify here the WiFi interface
NSError *err = nil;
// get all networks
NSSet *networksSet = [cwInterface scanForNetworksWithName:nil error:&err];
NSArray *allNetworks = [networksSet allObjects];
CWNetwork *selectedNetwork;
// check if one of the scanned networks SSIDs matches network with SSID "network_name"
for (CWNetwork *network in allNetworks) {
// perhaps you will have another for here, looping over NSDictionary with network name as key and password as value
if ([network.ssid isEqualToString:@"network_name"]) {
selectedNetwork = network;
}
}
// finally connect to the selected network
[cwInterface associateToNetwork:selectedNetwork password:@"network_password" error:&err];
// you can also disconnect as well
[cwInterface disassociate];
You can check CoreWLANWirelessManager as a more extensive example.

VolenD
- 3,592
- 18
- 23
-
scanForNetworksWithName is always a giving empty list whatever I do, even with several available WiFi access points... =( – Marcelo Jun 15 '15 at 20:34
-
@mthama Probably your WiFi interface is not `en1`, but something else (like `en0`). So, you have to change it. In order to have a solution that works for every Mac, you have to detect what is the name of the WiFi interface. – VolenD Jun 15 '15 at 20:52
-
user3584460, thanks for your advise. However, I'm passing "nil"value to the method, so I'm expecting it to list all available networks... – Marcelo Jun 15 '15 at 23:15
-
@mthama You have to specify interface name in `initWithInterfaceName`. Check the documentation. If you specify `nil`, then the selected interface will be `en0` which could be some other non-wireless interface. – VolenD Jun 16 '15 at 07:00
-
user3584460, thanks for your attention. I've attempted to do what you described, however with no success. I've run ifconfig and tested every interface that appeared in the console, but it still gives me an empty list. Now, I changed my approach, and I'm trying to do it through NSTask running "airport -s" command line. – Marcelo Jun 16 '15 at 17:34
-
@mthama I see that you have opened a question about the problem. Add some more code in it, because what is in it now is not enough. – VolenD Jun 16 '15 at 18:01
-
Again, thanks for the attention. Well, I admit that I'm somewhat lost here. I haven't put any additional code because other snippets are not related (at least in my point of view). There's a question about a bug in NSPipe in StackOverflow that I'm reading and investigating right now: http://stackoverflow.com/questions/3444178/nstask-nspipe-objective-c-command-line-help – Marcelo Jun 16 '15 at 18:24
-
1Ok. I've discovered the issue: sandbox. Man, this is really a must-have annoying thing to implement... – Marcelo Jun 16 '15 at 21:41
-
It would be great if someone could take a look at https://stackoverflow.com/questions/58188313/programatically-connecting-to-a-wifi-network-causes-apple-error-3903-when-using. Its question based on this excellent answer here. – AdeleGoldberg Oct 01 '19 at 17:02