获取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";

}

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

  1. IOS 获取wifi的SSID

    #import <SystemConfiguration/CaptiveNetwork.h> - (NSString *)currentWifiSSID { // Does not wor ...

  2. iOS 获取WIFI SSID及MAC地址

    NSString *ssid = @"Not Found"; NSString *macIp = @"Not Found"; CFArrayRef myArra ...

  3. iOS开发中获取WiFi相关信息

    iOS 开发中难免会遇到很多与网络方面的判断,这里做个汇总,大多可能是与WiFi相关的. 1.Ping域名.Ping某IP 有 时候可能会遇到ping 某个域名或者ip通不通,再做下一步操作.这里的p ...

  4. 获取WIFI的SSID和本机IP

    1.获取WIFI的SSID 引入库 #import <SystemConfiguration/CaptiveNetwork.h> ..... ..... // WIFI的名字 + (NSS ...

  5. iOS 获取IP

    #import <ifaddrs.h> //获取IP #import <arpa/inet.h> //只能获取WIFI下的IP地址 + (NSString *)getIPAdd ...

  6. iOS 12中获取WiFi的SSID

    开始搞智能家居,wifi获取不到了?? 小插曲 旧方法失效,19-12-15更新,ios13开始需要请求定位信息 SSID全称Service Set IDentifier, 即Wifi网络的公开名称. ...

  7. ios 获取当前wifi名称

    ios5之前可以通过读取配置文件获取,ios5以后苹果修改wifi列表文件位置,只有root权限才可以读取. ios4:/System/Library/SystemConfiguration/WiFi ...

  8. iOS 12中无法获取WiFi的SSID了?

    1.现象描述 2018年苹果升级iOS12之后,没有办法获取wifi名称等信息. 2.获取wifi信息 2.1 获取代码 /************ 控制器的view 加载完毕 的时候调用 ***** ...

  9. iOS 根据域名查询 IP 地址

    在 iOS 开发中,如果需要知道网站的 IP 地址: #include <netdb.h> #include <arpa/inet.h> NSString *webSiteSt ...

随机推荐

  1. document.all 用法

    一. document.all是页面内所有元素的一个集合.例如:        document.all(0)表示页面内第一个元素 二. document.all可以判断浏览器是否是IE     if ...

  2. o(n)线性排序算法

    O(n) 排序算法 前言 前面有总结过各类常用的排序算法,但是那些排序算法最优的时间复杂度是O(nlogn),所以我要介绍三种时间复杂度为O(n)的线性时间复杂度的排序算法. 计数排序 计数排序利用了 ...

  3. React-Router 4 的新玩意儿

    上一个项目用的还是 2.6.1,转眼的功夫 4.0 都发布了,API 变化实在有点大,2.X那套东西不顶用了,老老实实重新看一遍文档,其中有几点需要注意的,拿出来说一说. 本文只讨论针对浏览器的应用, ...

  4. C#中设置TextBox控件中仅可以输入数字且设置上限

    首先设置只可以输入数字: 首先设置TextBox控件的KeyPress事件:当用户按下的键盘的键不在数字位的话,就禁止输入 private void textBox1_KeyPress(object ...

  5. 使用SpringBoot快速构建应用程序

    1.Spring MVC和Spring Boot自带的web构建方式有所区别.Spring提供了spring-boot-starter-web自动配置模块. 2. 添加如下依赖 <depende ...

  6. .net很简介的操作json数组

    using Newtonsoft.Json.Linq;//添加的引用,Newtonsoft.dll可以到guget里面下载 string json="json字符串" JObjec ...

  7. Linux中重定向

    转:http://blog.csdn.net/songyang516/article/details/6758256 1重定向 1.1      重定向符号 >               输出 ...

  8. 教育行业app开发新契机,在线教育要从B端出发

    近几年移动互联网教育风生水起,在运营模式上的开拓也各不相同,随着移动互联网进入下半场,好未来.新东方.猿题库.学霸君等,都在加速三四线地区布局,以及教育行业app开发和升级. 在移动互联网下半场,用户 ...

  9. 用css3过滤做遮罩效果

    <!DOCTYPE html><html ng-app="myApp" ng-controller="myController">< ...

  10. [oracle]Oracle数据库安全管理

    目录 +  1.数据库安全控制策略概述 +  2.用户管理 +  3.资源限制与口令管理 +  4.权限管理 +  5.角色管理 +  6.审计 1.数据库安全控制策略概述 安全性是评估一个数据库的重 ...