Wil*_*bur 122 macos ip-address ipv6 ios
我想以编程方式获取iPad的IP地址.如何查询网络子系统以找出我的IPv4(和IPv6)地址是什么?
谢谢.PS:我可以以某种方式禁用IPv6吗?
Dav*_*d H 131
以下代码查找iOS或OSX设备上的所有IPv4和IPv6地址.第getIPAddress
一种方法或多或少地作为这个答案中的旧代码:你可以选择一个或另一个类型的地址,它总是喜欢WIFI而不是蜂窝(显然你可以改变它).
更有趣的是,它可以返回找到的所有地址的字典,跳过not up
接口的地址或与之关联的地址loopback
.以前的代码以及关于该主题的其他解决方案将无法正确解码IPv6(inet_ntoa无法处理它们).这是由Jens Alfke在Apple论坛上向我指出的- 使用的正确函数是inet_ntop(查看手册页,或者参考Jens提供的这篇inet_ntop文章.
字典键的形式为"interface""/""ipv4或ipv6".
#include <ifaddrs.h>
#include <arpa/inet.h>
#include <net/if.h>
#define IOS_CELLULAR @"pdp_ip0"
#define IOS_WIFI @"en0"
//#define IOS_VPN @"utun0"
#define IP_ADDR_IPv4 @"ipv4"
#define IP_ADDR_IPv6 @"ipv6"
- (NSString *)getIPAddress:(BOOL)preferIPv4
{
NSArray *searchArray = preferIPv4 ?
@[ /*IOS_VPN @"/" IP_ADDR_IPv4, IOS_VPN @"/" IP_ADDR_IPv6,*/ IOS_WIFI @"/" IP_ADDR_IPv4, IOS_WIFI @"/" IP_ADDR_IPv6, IOS_CELLULAR @"/" IP_ADDR_IPv4, IOS_CELLULAR @"/" IP_ADDR_IPv6 ] :
@[ /*IOS_VPN @"/" IP_ADDR_IPv6, IOS_VPN @"/" IP_ADDR_IPv4,*/ IOS_WIFI @"/" IP_ADDR_IPv6, IOS_WIFI @"/" IP_ADDR_IPv4, IOS_CELLULAR @"/" IP_ADDR_IPv6, IOS_CELLULAR @"/" IP_ADDR_IPv4 ] ;
NSDictionary *addresses = [self getIPAddresses];
NSLog(@"addresses: %@", addresses);
__block NSString *address;
[searchArray enumerateObjectsUsingBlock:^(NSString *key, NSUInteger idx, BOOL *stop)
{
address = addresses[key];
if(address) *stop = YES;
} ];
return address ? address : @"0.0.0.0";
}
- (NSDictionary *)getIPAddresses
{
NSMutableDictionary *addresses = [NSMutableDictionary dictionaryWithCapacity:8];
// retrieve the current interfaces - returns 0 on success
struct ifaddrs *interfaces;
if(!getifaddrs(&interfaces)) {
// Loop through linked list of interfaces
struct ifaddrs *interface;
for(interface=interfaces; interface; interface=interface->ifa_next) {
if(!(interface->ifa_flags & IFF_UP) /* || (interface->ifa_flags & IFF_LOOPBACK) */ ) {
continue; // deeply nested code harder to read
}
const struct sockaddr_in *addr = (const struct sockaddr_in*)interface->ifa_addr;
char addrBuf[ MAX(INET_ADDRSTRLEN, INET6_ADDRSTRLEN) ];
if(addr && (addr->sin_family==AF_INET || addr->sin_family==AF_INET6)) {
NSString *name = [NSString stringWithUTF8String:interface->ifa_name];
NSString *type;
if(addr->sin_family == AF_INET) {
if(inet_ntop(AF_INET, &addr->sin_addr, addrBuf, INET_ADDRSTRLEN)) {
type = IP_ADDR_IPv4;
}
} else {
const struct sockaddr_in6 *addr6 = (const struct sockaddr_in6*)interface->ifa_addr;
if(inet_ntop(AF_INET6, &addr6->sin6_addr, addrBuf, INET6_ADDRSTRLEN)) {
type = IP_ADDR_IPv6;
}
}
if(type) {
NSString *key = [NSString stringWithFormat:@"%@/%@", name, type];
addresses[key] = [NSString stringWithUTF8String:addrBuf];
}
}
}
// Free memory
freeifaddrs(interfaces);
}
return [addresses count] ? addresses : nil;
}
Run Code Online (Sandbox Code Playgroud)
EDIT1:代码于2014年5月16日更新(lhunath指出的bug,请参阅注释).现在返回了环回地址,但您可以轻松取消注释测试以自行排除它们.
EDIT2 :(由一些不知名的人):进一步改进2015年3月13日:如果用户使用VPN(无论是WiFi还是Cellular),之前的代码都会失败.现在,它甚至可以与VPN连接一起使用.VPN连接优先于WiFi和Cell,因为这是设备处理它的方式.这应该适用于Mac,因为Mac上的VPN连接也使用IF utun0但未经过测试.
EDIT3:(2016年9月8日)鉴于@Qiulang遇到的问题(见评论)和VPN代码(其他人添加了),我已经评论过了.如果有人明确知道如何指定用户VPN,请发表评论.
Rap*_*tor 88
在您的实现文件.m中,
#import <ifaddrs.h>
#import <arpa/inet.h>
// Get IP Address
- (NSString *)getIPAddress {
NSString *address = @"error";
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;
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
81185 次 |
最近记录: |