我想在应用程序中找到IP地址.我能找到它.但是,问题是,它在iphone os 2.0左右工作.但是,在iphone os 3.0中它给了我一个警告:
warning: no '+currentHost' method found
warning: (Messages without a matching method signature)
Run Code Online (Sandbox Code Playgroud)
我正在使用此代码,它与os 2.0版一起工作正常.
-(NSString*)getAddress {
char iphone_ip[255];
strcpy(iphone_ip,"127.0.0.1"); // if everything fails
NSHost* myhost = [NSHost currentHost];
if (myhost)
{
NSString *ad = [myhost address];
if (ad)
strcpy(iphone_ip,[ad cStringUsingEncoding: NSISOLatin1StringEncoding]);
}
return [NSString stringWithFormat:@"%s",iphone_ip];
Run Code Online (Sandbox Code Playgroud)
}
如何在iphone os 3.0或更高版本的os版本中找到IP地址?
提前致谢.
我正在尝试使用IP地址NSHost.使用该NSHost对象,我可以使用addresses方法访问对象数组,其中一个对象是IP地址.我担心IP地址可能会改变阵列中从一台机器到另一台机器的位置.有没有办法以通用的方式访问这些信息?
试图在之前的帖子中回答这个问题,但是你可以看到它不足.
这是我的代码:
+(NSString *) ipAddress {
NSHost * h = [[[NSHost currentHost] addresses] objectAtIndex:1];
return h ;
}
Run Code Online (Sandbox Code Playgroud) 我目前正在使用此代码
NSHost *host = [NSHost hostWithAddress:hostname];
if (host == nil) {
host = [NSHost hostWithName:hostname];
if (host == nil) {
[self setMessage:@"Invalid IP address or hostname:"];
return;
}
}
Run Code Online (Sandbox Code Playgroud)
检索我正在处理的网络应用程序的IP地址,但是我知道NSHost是一个将被拒绝的私有API.任何人都可以帮助我使用此代码生成相同的结果而不使用NSHost?我不确定从哪里开始.
编辑:
以下建议看起来非常接近完美,我已将此代码添加到我的应用程序中代替上面的代码
Boolean result;
CFHostRef hostRef;
CFArrayRef addresses;
NSString *hostname = @"www.apple.com";
hostRef = CFHostCreateWithName(kCFAllocatorDefault, (CFStringRef)hostname);
if (hostRef) {
result = CFHostStartInfoResolution(hostRef, kCFHostAddresses, NULL); // pass an error instead of NULL here to find out why it failed
if (result == TRUE) {
addresses = CFHostGetAddressing(hostRef, &result);
}
}
if …Run Code Online (Sandbox Code Playgroud)