iOS 개발 로 네트워크 IP 와 랜 IP 주소 가 져 오기
// IP IP
NSString *url = @"http://ip.taobao.com/service/getIpInfo.php?ip=myip";
JSON
{"code":0,
"data":{
"country":" ",
"country_id":"CN",
"area":" ",
"area_id":"300000",
"region":" ",
"region_id":"310000",
"city":" ",
"city_id":"310100",
"county":" ",
"county_id":"334115",
"isp":" ",
"isp_id":"100227",
"ip":"126.231.115.34"
}
}
- (void)urlRequestOperation{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
NSString *URLTmp1 = @"http://1212.ip138.com/ic.asp";
NSString *URLTmp = [URLTmp1 stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSData * resData = [NSData dataWithContentsOfURL:[NSURL URLWithString:URLTmp]];
dispatch_async(dispatch_get_main_queue(), ^{
if (resData) {
//系统自带JSON解析
NSStringEncoding gbkEncoding = CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000);
//您的IP是:[122.222.122.22] 来自:上海市某某区 某某运营商
NSString *str3 = [[NSString alloc] initWithData:resData encoding:gbkEncoding];
}
});
});
}
// IP
- (NSString *)getIpAddresses{
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;
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
KVO 구현 메커니즘 이해관찰자 모델의 정의: 한 목표 대상은 모든 관찰자 대상에 의존하고 그 자체의 상태가 바뀔 때 관찰자 대상에게 자발적으로 통지한다.이 주동적인 통지는 일반적으로 각 관찰자 대상이 제공한 인터페이스 방법을 호출함으로써 ...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.