一、获取外网ip
- 首推方法1:此方法采用的淘宝网址,获取的到IP与百度IP是一样的
- (NSString *)deviceWANIPAddress {
NSURL *ipURL = [NSURL URLWithString:@"http://ip.taobao.com/service/getIpInfo.php?ip=myip"];
NSData *data = [NSData dataWithContentsOfURL:ipURL];
NSDictionary *ipDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
NSString *ipStr = nil;
if (ipDic && [ipDic[@"code"] integerValue] == 0) { //获取成功
ipStr = ipDic[@"data"][@"ip"];
}
return (ipStr ? ipStr : @"");
}
返回数据
{
code = 0;
data = {
area = "";
"area_id" = "";
city = "\U4e0a\U6d77";
"city_id" = 310100;
country = "\U4e2d\U56fd";
"country_id" = CN;
county = XX;
"county_id" = xx;
ip = "61.152.71.206";
isp = "\U7535\U4fe1";
"isp_id" = 100017;
region = "\U4e0a\U6d77";
"region_id" = 310000;
};
}
- 方法2:此方法访问的搜狐的获取ip接口
+ (NSString *)deviceWANIPAddress {
NSError *error;
NSURL *ipURL = [NSURL URLWithString:@"http://pv.sohu.com/cityjson?ie=utf-8"];
NSMutableString *ip = [NSMutableString stringWithContentsOfURL:ipURL encoding:NSUTF8StringEncoding error:&error];
//判断返回字符串是否为所需数据
if ([ip hasPrefix:@"var returnCitySN = "]) {
//对字符串进行处理,然后进行json解析
//删除字符串多余字符串
NSRange range = NSMakeRange(0, 19);
[ip deleteCharactersInRange:range];
NSString * nowIp =[ip substringToIndex:ip.length-1];
//将字符串转换成二进制进行Json解析
NSData * data = [nowIp dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary * dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
NSLog(@"%@",dict);
return dict[@"cip"] ? dict[@"cip"] : @"";
}
return @"";
}
接口返回
{
cid = 310000;
cip = "61.152.71.206";
cname = "\U4e0a\U6d77\U5e02";
}
二、获取手机本地静态IP地址:(局域网)
#import <Foundation/Foundation.h>
@interface RequestIPAddress : NSObject
/*
* 获取设备IP地址
*/
+ (NSString *)getIP;
@end
#import "RequestIPAddress.h"
#import "JCIPAddressConfig.h"
#define IOS_CELLULAR @"pdp_ip0"
#define IOS_WIFI @"en0"
#define IOS_VPN @"utun0"
#define IP_ADDR_IPv4 @"ipv4"
#define IP_ADDR_IPv6 @"ipv6"
#pragma mark IPv4是32位地址长度
#pragma mark IPv6是128位地址长度
@implementation RequestIPAddress
/*
* 获取设备IP地址
*/
+ (NSString *)getIP
{
bool ret = [self isIpv6Net];
NSArray *searchArray = ret ?
@[ 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 ]
:
@[ 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 ];
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;
}
+ (BOOL)isIpv6Net{
NSArray *searchArray =
@[ 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];
__block BOOL isIpv6 = NO;
[searchArray enumerateObjectsUsingBlock:^(NSString *key, NSUInteger idx, BOOL *stop)
{
if ([key rangeOfString:@"ipv6"].length > 0 && ![[NSString stringWithFormat:@"%@",addresses[key]] hasPrefix:@"(null)"] ) {
if ( ![addresses[key] hasPrefix:@"fe80"]) {
isIpv6 = YES;
}
}
} ];
return isIpv6;
}
@end