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 ...
随机推荐
- [刷题]Codeforces 786A - Berzerk
http://codeforces.com/problemset/problem/786/A Description Rick and Morty are playing their own vers ...
- Linux下Oracle的启动和关闭
默认情况下,Linux下Oracle是不会随系统自动启动的. 1.启动Oracle 1.以oracle账户登录到CentOS,或者切换到oracle用户权限 # su – oracle 2.然后输入 ...
- 增强for循环 -- foreach循环
1 作用 简化迭代器的书写格式.(注意:foreach循环的底层还是使用了迭代器遍历.) 2 适用范围 如果是实现了Iterable接口的对象或者是数组对象都可以使用foreach循环. 3 格 ...
- [转]亿级Web系统搭建:单机到分布式集群
当一个Web系统从日访问量10万逐步增长到1000万,甚至超过1亿的过程中,Web系统承受的压力会越来越大,在这个过程中,我们会遇到很多的问题.为了解决这些性能压力带来问题,我们需要在Web系统架构层 ...
- 分布式缓存技术redis学习—— 深入理解Spring Redis的使用
关于spring redis框架的使用,网上的例子很多很多.但是在自己最近一段时间的使用中,发现这些教程都是入门教程,包括很多的使用方法,与spring redis丰富的api大相径庭,真是浪费了这么 ...
- 《安卓网络编程》之第四篇 处理URL地址
在Android手机系统中,可以通过URL地址获取网络资源.在URL类的众多方法中,可以使用openStream()方法来读取该URL资源的输入流InputStream.在此方法的基础上可以引申出很多 ...
- UICollection无法下拉刷新的问题
当UICollectonView加载的内容不够多的时候会出现无法上下拉刷新的问题,折腾了半天,原来是有一个属性没有打开 设置 : self.collectionView.alwaysBounceVer ...
- Kotlin初探
前几天看到新闻,Google将Kotlin语言作为Android应用开发的一级语言, 与Java并驾齐驱, 这则消息在开发界一下就炸开了锅( 好像平息的很快...)! 连Google的亲儿子go语言也 ...
- What does a Bayes factor feel like?(转)
A Bayes factor (BF) is a statistical index that quantifies the evidence for a hypothesis, compared t ...
- 写给Android App开发人员看的Android底层知识(6)
(十一)BroadcastReceiver BroadcastReceiver,也就是广播,简称Receiver. 很多App开发人员表示,从来没用过Receiver.其实吧,对于音乐播放类App,用 ...