Connect to specified wifi router in Macintosh, by Objective C
2015-04-02 13:14
218 查看
Similar to
the other article of mine: In here, I post the code which could connect to target wifi router. The Xcode project be set as :
Base SDK is 10.7, and OSX deployment SDK as 10.5. The code depends on the frame work CoreWLAN.framework of 10.7 (it exists in /Developer/SDKs/MacOSX10.7.sdk/System/Library/Frameworks by default , if you have installed 10.7 SDK), you should
add it to your Xcode project. The could running in OS X 10.8 and 10.9 without any wrong(both 32 and 64 bit).
If you do not install 10.7 SDK, to set the environment, it is necessary to download OS X 10.7 SDK. you could download it from
here ( need apple id). Refer to
the article in stack overflow, SDKs of OS X are bundled with each version of Xcode (10.5 -> 3.1, 10.6 ->3.2, 10.7 ->4.3, 10.8 -> 5.1, 10.9 -> 6.0, respectively). In here, you should download Xcode 4.3, extract the 10.7 SDK to your computer.
The code is in objective-C, but the function call is C-style, for C/C++ calling compatible.
the other article of mine: In here, I post the code which could connect to target wifi router. The Xcode project be set as :
Base SDK is 10.7, and OSX deployment SDK as 10.5. The code depends on the frame work CoreWLAN.framework of 10.7 (it exists in /Developer/SDKs/MacOSX10.7.sdk/System/Library/Frameworks by default , if you have installed 10.7 SDK), you should
add it to your Xcode project. The could running in OS X 10.8 and 10.9 without any wrong(both 32 and 64 bit).
If you do not install 10.7 SDK, to set the environment, it is necessary to download OS X 10.7 SDK. you could download it from
here ( need apple id). Refer to
the article in stack overflow, SDKs of OS X are bundled with each version of Xcode (10.5 -> 3.1, 10.6 ->3.2, 10.7 ->4.3, 10.8 -> 5.1, 10.9 -> 6.0, respectively). In here, you should download Xcode 4.3, extract the 10.7 SDK to your computer.
The code is in objective-C, but the function call is C-style, for C/C++ calling compatible.
#import <CoreWLAN/CoreWLAN.h> int ConnectToTargetWifiSSID(char *pSSIDName, char *pPassword) { @autoreleasepool { if(NULL == pSSIDName) return -1; NSString *ssidName = [ [NSString alloc] initWithUTF8String : pSSIDName ]; //NSLog(@"%@", ssidName); CWInterface *currentInterface = [CWInterface interface]; if( [ssidName isEqualToString: [currentInterface ssid]]) { printf("current is %s\n", pSSIDName); return 1; }/*if*/ if( NO== currentInterface.powerOn) { printf("wireless card has been turn off\n"); return -1; }/*if */ NSArray *networks = [[currentInterface scanForNetworksWithName:nil error:nil] allObjects]; if(0 == [networks count]) { printf("no available wifi ap\n"); return -2; }/*if */ printf("available wifi ssid name:\n"); for(int i = 0; i< [networks count]; i++){ printf("%s \n", [[[networks objectAtIndex:i] ssid] cStringUsingEncoding:NSUTF8StringEncoding]); }/*for*/ int iii; iii = -1; for(int i = 0; i< [networks count]; i++){ if([ ssidName isEqualToString: [[networks objectAtIndex:i] ssid] ]) { iii = i; break; }/*if*/ }/*for*/ if(-1 == iii) { printf("no available wifi ssid name marches %s\n", pSSIDName); return -3; }/*if*/ NSString *password; if(NULL == pPassword) password = [ [NSString alloc] initWithFormat:@"%@",@" "]; else password = [ [NSString alloc] initWithUTF8String : pPassword]; //NSLog(@"%@", password); CWNetwork * network = [networks objectAtIndex:iii]; BOOL isSuccess; isSuccess = [currentInterface associateToNetwork:network password:password error:nil]; if(NO == isSuccess) return -6; }/*@autoreleasepool*/ return 0; }/*ConnectToTargetWifiSSID*/
相关文章推荐
- Connect to specified wifi router in windows, by C
- connect to mysql by odbc in linux
- Unable to locate JAR/zip in file system as specified by the driver definition: mysql-connector-java-
- ArcMap 连接SDE 出错“Failed to connect to the specified server. Entry for SDE instance no found in services file.”
- SQL: How to get a series of dates in sql using connect by
- How to avoid Excel from prompting "The file you are trying to open, , is in a different format than specified by the file extension"? (转)
- connect to a specific wifi network in Android programmatically
- How to: Protect Against Script Exploits in a Web Application by Applying HTML Encoding to Strings
- MS bug "The connection pool" in Oracle 10g and the data sort according to specified filed on DataGrid control.
- SAX used in java to connect to database
- Drive More Search Traffic to Your Maps API Site by Including KML Files in a Sitemap——Google 地图 API 文档之五
- How to read Mac .DMG file in Linux or PC
- Programming Basic--why we need to add ' extern "C" ' in C++ program when calling the function which is complied by C complier?
- LINQ体验(7)——LINQ to SQL语句之Group By/Having和Exists/In/Any/All/Contains
- by the end of, at the end of , in the end , to the end 的区别与用法
- LINQ体验(7)——LINQ to SQL语句之Group By/Having和Exists/In/Any/All/Contains
- Using IPv6 to connect computer in dorm from our laboratory
- LINQ体验(7)——LINQ to SQL语句之Group By/Having和Exists/In/Any/All/Contains
- [ASP.NET2.0][数据库更新]How to troubleshoot 'Procedure or function has too many arguments specified' in asp.net 2.0