#import <CoreWLAN/CoreWLAN.h>
#import <SecurityFoundation/SFAuthorization.h>
int main( int argc, const char *argv[] ) {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSString *interface = @"en1";
CWInterface *airport = [CWInterface interfaceWithName:interface];
CWWirelessProfile *newProfile = [CWWirelessProfile profile];
[newProfile setSsid:@"wep-poc"];
[newProfile setSecurityMode:[NSNumber numberWithInt:kCWSecurityModeWEP]];
[newProfile setPassphrase:@"wepexample201"];
CWConfiguration *conf = [airport configuration];
BOOL configChanged =NO;
NSSet *rememberedNetworks = [conf rememberedNetworks];
if (![ rememberedNetworks containsObject:newProfile]){
[conf setRememberedNetworks:[rememberedNetworks setByAddingObject:newProfile]];
configChanged =YES;
}
NSArray *preferredNetworks = [conf preferredNetworks];
if (![preferredNetworks containsObject:newProfile]){
[conf setPreferredNetworks:[preferredNetworks arrayByAddingObject:newProfile]];
configChanged =YES;
}
if (!configChanged ){
NSLog(@"La red no fue cambiada.");
return 1;
}
SFAuthorization *authorization = [SFAuthorization authorization];
BOOL authResult =[authorization obtainWithRight:"system.preferences"
flags:(kAuthorizationFlagExtendRights |
kAuthorizationFlagInteractionAllowed |
kAuthorizationFlagPreAuthorize)
error:NULL];
if (!authResult){
NSLog(@"Error de autenticacion.");
return 1;
}
[airport setAuthorization:authorization];
NSError *err =nil;;
BOOL saved = [airport commitConfiguration:conf error:&err];
if (!saved && err){
NSLog(@"La red no fue agregada.");
return 1;
}
if (!saved){
NSLog(@"Error.");
return 1;
}
[pool release];
return 0;
}
----------8<----------[ERROR]----------8<----------
2011-07-27 04:24:16.185 consolaWifi[2290:a0f] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSCFArray insertObject:atIndex:]: attempt to insert nil'
*** Call stack at first throw:
(
0 CoreFoundation 0x00007fff83f29444 __exceptionPreprocess + 180
1 libobjc.A.dylib 0x00007fff85f4c0f3 objc_exception_throw + 45
2 CoreFoundation 0x00007fff83f29267 +[NSException raise:format:arguments:] + 103
3 CoreFoundation 0x00007fff83f291f4 +[NSException raise:format:] + 148
4 Foundation 0x00007fff849a4c52 -[NSCFArray insertObject:atIndex:] + 119
5 CoreWLAN 0x000000010001b769 -[CWConfiguration initWithInterface:] + 577
6 CoreWLAN 0x00000001000141d1 -[CWInterface configuration] + 53
7 consolaWifi 0x0000000100000fd0 main + 248
8 consolaWifi 0x0000000100000ed0 start + 52
9 ??? 0x0000000000000001 0x0 + 1
)
terminate called after throwing an instance of 'NSException'