本文介绍了使用NSHost访问IP地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想使用 NSHost
获取IP地址。使用 NSHost
对象,我可以使用地址方法访问对象数组,其中之一是IP地址。我担心,虽然IP地址可能改变阵列中的位置从一台机器到另一台。有没有办法以普遍的方式访问这些信息?
I am trying to get the IP Address using NSHost
. With the NSHost
object I can use the addresses method to access an array of objects one of which is the IP Address. I fear though that the IP Address may change position in the array from one machine to the other. Is there a way to access this information in a universal way?
有人尝试在上一篇文章中回答这个问题,但是你可以看到它的缺点。
There was an attempt to answer this question in a previous post, but as you can see it falls short.
这是我的代码:
+(NSString *) ipAddress {
NSHost * h = [[[NSHost currentHost] addresses] objectAtIndex:1];
return h ;
}
推荐答案
机器没有问题。
-(void) getIPWithNSHost{
NSArray *addresses = [[NSHost currentHost] addresses];
for (NSString *anAddress in addresses) {
if (![anAddress hasPrefix:@"127"] && [[anAddress componentsSeparatedByString:@"."] count] == 4) {
stringAddress = anAddress;
break;
} else {
stringAddress = @"IPv4 address not available" ;
}
}
//NSLog (@"getIPWithNSHost: stringAddress = %@ ",stringAddress);
}
NSString * stringAddress;被声明为else
NSString *stringAddress; is declared else where
这篇关于使用NSHost访问IP地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!