获取Ip
要先导入:
#import <ifaddrs.h>
#import <arpa/inet.h>
代码如下:
-(NSString *)getUserIP {
NSString *address = @"error";
struct ifaddrs *interfaces = NULL;
struct ifaddrs *temp_addr = NULL;
int success = 0;
success =getifaddrs(&interfaces);
if (success == 0) {
temp_addr = interfaces;
while (temp_addr != NULL) {
if ([[NSString stringWithUTF8String:temp_addr->ifa_name] isEqualToString:@"en0"]) {
address = [NSString stringWithUTF8String:inet_ntoa(((struct sockaddr_in*) temp_addr->ifa_addr)->sin_addr)];
}
}
temp_addr = temp_addr->ifa_next;
}
freeifaddrs(interfaces);
return address;
}
2.获取当前连接的wifi信息
要先导入:
#import <SystemConfiguration/CaptiveNetwork.h>
代码如下:
- (IBAction)getWifiList:(id)sender {
NSArray *ifs = (__bridge_transfer id) CNCopySupportedInterfaces();
id info = nil;
for (NSString *ifnam in ifs) {
info = (__bridge_transfer id) CNCopyCurrentNetworkInfo((__bridge CFStringRef) ifnam);
NSLog(@"%@ => %@", ifnam, info);
NSString *string = [[NSString alloc] initWithData:info[@"SSIDDATA"] encoding:NSUTF8StringEncoding];
NSLog(@"%@", string);
if (info && [info count]) {
break;
}
}
}