Skip to content

Instantly share code, notes, and snippets.

@topnotcher
Created February 24, 2015 20:09
Show Gist options
  • Save topnotcher/4817ae6038247bf330ea to your computer and use it in GitHub Desktop.
Save topnotcher/4817ae6038247bf330ea to your computer and use it in GitHub Desktop.
CWInterface *interface = [CWInterface interfaceWithName:[[CWInterface interfaceNames] anyObject]];
CWMutableConfiguration *config = [CWMutableConfiguration configurationWithConfiguration:interface.configuration];
NSMutableArray *networks = [NSMutableArray arrayWithArray:[config.networkProfiles array]];
for (CWNetworkProfile *profile in [networks copy]) {
if ([[profile ssid] isEqualToString:@"URI_Open"]) {
[networks removeObject:profile];
} else if ([[profile ssid] isEqualToString:@"URI_Secure"]) {
CWNetworkProfile *tmp = profile;
[networks removeObject:tmp];
[networks insertObject:tmp atIndex:0];
}
}
config.networkProfiles = [NSOrderedSet orderedSetWithArray:networks];
SFAuthorization *auth = [SFAuthorization authorization];
BOOL authResult = [auth obtainWithRight:"system.preferences"
flags:(
kAuthorizationFlagExtendRights |
kAuthorizationFlagInteractionAllowed |
kAuthorizationFlagPreAuthorize
) error:nil
];
if (!authResult) {
NSRunCriticalAlertPanel(@"Error", @"Authorization Failed!", @"OK", nil, nil);
return EXIT_FAILURE;
}
NSError *error = nil;
[interface commitConfiguration:config authorization:auth error:&error];
if (error) {
[NSApp presentError:error];
return EXIT_FAILURE;
}
NSAlert *alert = [[NSAlert alloc] init];
[alert addButtonWithTitle:@"OK"];
[alert setMessageText:@"Use the Wi-Fi icon in the top menu bar to connect to URI_Secure."];
[alert setAlertStyle:NSInformationalAlertStyle];
[alert runModal];
return EXIT_SUCCESS;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment