iOS获取WIFI的IP、子网掩码,以及域名转IP

获取WIFI需要的头文件:

#import "GetCurrentIP.h"

#import <ifaddrs.h>

#import <arpa/inet.h>

#import <SystemConfiguration/CaptiveNetwork.h>

#include <netdb.h>

#include <net/if.h>

#import <dlfcn.h>

#include <sys/socket.h>

#include <sys/sysctl.h>

获取所连wifi的IP的方法:

#pragma mark - 获取用户当前的IP地址

+ (nullable NSString*)getCurrentLocalIP

{

NSString *address = nil;

struct ifaddrs *interfaces = NULL;

struct ifaddrs *temp_addr = NULL;

int success = 0;

// retrieve the current interfaces - returns 0 on success

success = getifaddrs(&interfaces);

if (success == 0) {

// Loop through linked list of interfaces

temp_addr = interfaces;

while(temp_addr != NULL) {

if(temp_addr->ifa_addr->sa_family == AF_INET) {

// Check if interface is en0 which is the wifi connection on the iPhone

if([[NSString stringWithUTF8String:temp_addr->ifa_name] isEqualToString:@"en0"]) {

// Get NSString from C String

address = [NSString stringWithUTF8String:inet_ntoa(((struct sockaddr_in *)temp_addr->ifa_addr)->sin_addr)];

}

}

temp_addr = temp_addr->ifa_next;

}

}

// Free memory

freeifaddrs(interfaces);

return address;

}

获取所连WIFI的详细信息:

+ (nullable NSString*)getCurrentWifiMessage {

NSString *address = nil;

struct ifaddrs *interfaces = NULL;

struct ifaddrs *temp_addr = NULL;

int success = 0;

// retrieve the current interfaces - returns 0 on success

success = getifaddrs(&interfaces);

if (success == 0)

{

// Loop through linked list of interfaces

temp_addr = interfaces;

while(temp_addr != NULL)

{

if(temp_addr->ifa_addr->sa_family == AF_INET)

{

// Check if interface is en0 which is the wifi connection on the iPhone

if([[NSString stringWithUTF8String:temp_addr->ifa_name] isEqualToString:@"en0"])

address = [NSString stringWithUTF8String:inet_ntoa(((struct sockaddr_in *)temp_addr->ifa_netmask)->sin_addr)];

//                    NSLog(@"子网掩码:%@",[NSString stringWithUTF8String:inet_ntoa(((struct sockaddr_in *)temp_addr->ifa_netmask)->sin_addr)]);

//                NSLog(@"本地IP:%@",[NSString stringWithUTF8String:inet_ntoa(((struct sockaddr_in *)temp_addr->ifa_addr)->sin_addr)]);

//                NSLog(@"广播地址:%@",[NSString stringWithUTF8String:inet_ntoa(((struct sockaddr_in *)temp_addr->ifa_dstaddr)->sin_addr)]);

}

temp_addr = temp_addr->ifa_next;

}

}

// Free memory

freeifaddrs(interfaces);

return address;

}

域名转换成IP:

#pragma mark - 域名转成IP的方法

+ (NSString *)queryIpWithDomain:(NSString *)domain

{

struct hostent *hs;

struct sockaddr_in server;

if ((hs = gethostbyname([domain UTF8String])) != NULL)

{

server.sin_addr = *((struct in_addr*)hs->h_addr_list[0]);

return [NSString stringWithUTF8String:inet_ntoa(server.sin_addr)];

}

return @"1";

}

时间: 2024-12-31 20:39:59

iOS获取WIFI的IP、子网掩码,以及域名转IP的相关文章

iOS 获取 WiFi 列表

IOS 获取 WIFi列表有2016 /11 / 10 之后就不用申请权限了  Apple 官方邮件回复: Thank you for requesting information about the Network Extension framework. Please note that as of November 10, 2016 this process is not required for developers who wish to use App Proxy, Content

iOS 获取手机当前所连接的网络的IP地址

1首先要在当前所在的类导入这几个头文件: #include <arpa/inet.h>#include <netdb.h>#include <net/if.h>#include <ifaddrs.h>#import <dlfcn.h> 2具体代码的实现: //获取手机的网络的ip地址- (NSString *)getIPAddress{ BOOL success; struct ifaddrs * addrs; const struct ifad

IOS 获取wifi的SSID

#import <SystemConfiguration/CaptiveNetwork.h> - (NSString *)currentWifiSSID { // Does not work on the simulator. NSString *ssid = nil; NSArray *ifs = (__bridge_transfer id)CNCopySupportedInterfaces(); for (NSString *ifnam in ifs) { NSDictionary *in

【IOS】获取wifi名称 (即SSID)

iOS 获取wifi ssid 名称 SSID全称Service Set IDentifier, 即Wifi网络的公开名称.在IOS 4.1以上版本提供了公开的方法来获取该信息. 首先添加框架:SystemConfiguration.framework 1 #import <SystemConfiguration/CaptiveNetwork.h> 2 - (id)fetchSSIDInfo 3 { 4 NSArray *ifs = (id)CNCopySupportedInterfaces(

网址(url),域名,ip地址,dns,hosts之间的关系

什么是ip? 我们知道,在Internet上有千百万台主机,为了区分这些主机,人们给每台主机都分配了一个专门的地址,称为IP地址.通过IP地址就可以访问到每一台主机. IP地址由4部分数字组成,每部分都不大于256,各部分之间用小数点分开.例如"百度搜索"主机的IP地址就是:"119.75.217.109,"在浏览器上输入这个IP地址,就可以访问到百度的主页. 我们的每个虚拟主机用户,都分配一个永久的IP地址. 什么是域名? 虽然可以通过IP地址来访问每一台主机,但

IOS 获取手机ip地址

#include <ifaddrs.h> #include <arpa/inet.h> - (NSString *)getIPAddress {          NSString *address = @"error";     struct ifaddrs *interfaces = NULL;     struct ifaddrs *temp_addr = NULL;     int success = 0;          // retrieve th

ios获取局域网IP地址

#include <arpa/inet.h> #include <net/if.h> #include <ifaddrs.h> - (NSString *)localIPAddress { NSString *localIP = nil; struct ifaddrs *addrs; if (getifaddrs(&addrs)==0) { const struct ifaddrs *cursor = addrs; while (cursor != NULL)

转载-iOS获取设备IP地址

iOS获取设备IP地址 代码如下: 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596 #import <ifaddrs.h>#import <arpa/inet.h>#

ios获取当前wifi热点信息

#pragma mark - 获取当前连接wifi信息 - (NSString *)returnWifiName { NSString *wifiName = @"Not Found"; CFArrayRef myArray = CNCopySupportedInterfaces(); if (myArray != nil) { CFDictionaryRef myDict = CNCopyCurrentNetworkInfo(CFArrayGetValueAtIndex(myArra