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";
}
iOS获取WIFI的IP、子网掩码,以及域名转IP的更多相关文章
- IOS 获取wifi的SSID
#import <SystemConfiguration/CaptiveNetwork.h> - (NSString *)currentWifiSSID { // Does not wor ...
- iOS 获取WIFI SSID及MAC地址
NSString *ssid = @"Not Found"; NSString *macIp = @"Not Found"; CFArrayRef myArra ...
- iOS开发中获取WiFi相关信息
iOS 开发中难免会遇到很多与网络方面的判断,这里做个汇总,大多可能是与WiFi相关的. 1.Ping域名.Ping某IP 有 时候可能会遇到ping 某个域名或者ip通不通,再做下一步操作.这里的p ...
- 获取WIFI的SSID和本机IP
1.获取WIFI的SSID 引入库 #import <SystemConfiguration/CaptiveNetwork.h> ..... ..... // WIFI的名字 + (NSS ...
- iOS 获取IP
#import <ifaddrs.h> //获取IP #import <arpa/inet.h> //只能获取WIFI下的IP地址 + (NSString *)getIPAdd ...
- iOS 12中获取WiFi的SSID
开始搞智能家居,wifi获取不到了?? 小插曲 旧方法失效,19-12-15更新,ios13开始需要请求定位信息 SSID全称Service Set IDentifier, 即Wifi网络的公开名称. ...
- ios 获取当前wifi名称
ios5之前可以通过读取配置文件获取,ios5以后苹果修改wifi列表文件位置,只有root权限才可以读取. ios4:/System/Library/SystemConfiguration/WiFi ...
- iOS 12中无法获取WiFi的SSID了?
1.现象描述 2018年苹果升级iOS12之后,没有办法获取wifi名称等信息. 2.获取wifi信息 2.1 获取代码 /************ 控制器的view 加载完毕 的时候调用 ***** ...
- iOS 根据域名查询 IP 地址
在 iOS 开发中,如果需要知道网站的 IP 地址: #include <netdb.h> #include <arpa/inet.h> NSString *webSiteSt ...
随机推荐
- Centos7.3 安装Mysql5.7并修改初始密码
1.官方安装文档 http://dev.mysql.com/doc/mysql-yum-repo-quick-guide/en/ 2.下载 Mysql yum包 http://dev.mysql.co ...
- 为Distinct准备的通用对比器
使用Linq过滤重复对象的时候,我们使用Distinct. 但是Distinct对int long等值类型才有效果,对于对象我们需要自己写个对象. 以下利用泛型封装了两个类: CommonCompar ...
- Linux-vim学习入门
1.前言 vi/vim是linux中很重要的文本编辑器.我第一次使用这个编辑器时,很不习惯,甚至都不知道如何移动光标和插入字符.慢慢地经过学习,才知道如何使用vi/vim. vi/vi ...
- Windows安装Mysql5.7.10绿色版
今天在Windows上安装Mysql的时候,去官网上下了一个最新版本的Mysql5.7.10绿色版,但是之前网上安装方式都过时了,比如会报一些常见的错误“[ERROR] Fatal error: Ca ...
- C语言之强化,弱化符号weak
一.概述 在C语言中,函数和初始化的全局变量(包括显示初始化为0)是强符号,未初始化的全局变量是弱符号. 对于它们,下列三条规则使用: ① 同名的强符号只能有一个,否则编译器报"重复定义&q ...
- 关于QT按键信号槽的总结(原创)
QT界面按钮一般是必填的: 每个按钮都要 Go to slot 下面有几个都是常用的,先说一下 clicked:pressed:releaed的区别 字面意思看:click是点击一下,pressed是 ...
- SpringMVC 3.2集成Spring Security 3.2
参考:http://www.cnblogs.com/Beyond-bit/p/springmvc_and_springsecurity.html SpringMVC 3.2集成Spring Secur ...
- 第十五章(附)分布式缓存-Memcached
一.概念 Memcached是danga.com(运营LiveJournal的技术团队)开发的一套分布式内存对象缓存系统,用于在动态系统中减少数据库负载,提升性能. 二.适用场合 1.分布式应用.由于 ...
- JVM、GC与HashMap
阿里巴巴突然来了个面试邀请电话,问了些java底层的东西,不知所措,所以专门花了些时间做了下学习,顺便记录下,好记性不如烂笔头. 一.对JAVA的垃圾回收机制(GC)的理解 不同于C/C++需要手工释 ...
- Include promo/activity effect into the prediction (extended ARIMA model with R)
I want to consider an approach of forecasting I really like and frequently use. It allows to include ...