xcode IP地址?- 可可
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1331912/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
IP Address? - Cocoa
提问by lab12
How would I make a GUI program that displays your Ip address with a click of a button? Please, no difficult explanations, I just started Cocoa not long ago.
我将如何制作一个单击按钮即可显示您的 IP 地址的 GUI 程序?拜托,没有什么困难的解释,我不久前刚开始使用 Cocoa。
Thanks,
谢谢,
Kevin
凯文
回答by Ahmed Hammad
You can get IP address through two ways:
您可以通过两种方式获取IP地址:
1- if you want to get the local ip address on the current used netwrok, you can use the following method to retrive it:
1-如果您想获取当前使用的网络上的本地IP地址,您可以使用以下方法来检索它:
-(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)
{
// 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;
}
2- if you want to get the external IP address then you need to use the following method:
2-如果要获取外部IP地址,则需要使用以下方法:
-(NSString*)getIP
{
NSUInteger an_Integer;
NSArray * ipItemsArray;
NSString *externalIP;
NSURL *iPURL = [NSURL URLWithString:@"http://www.dyndns.org/cgi-bin/check_ip.cgi"];
if (iPURL) {
NSError *error = nil;
NSString *theIpHtml = [NSString stringWithContentsOfURL:iPURL encoding:NSUTF8StringEncoding error:&error];
if (!error) {
NSScanner *theScanner;
NSString *text = nil;
theScanner = [NSScanner scannerWithString:theIpHtml];
while ([theScanner isAtEnd] == NO) {
// find start of tag
[theScanner scanUpToString:@"<" intoString:NULL] ;
// find end of tag
[theScanner scanUpToString:@">" intoString:&text] ;
// replace the found tag with a space
//(you can filter multi-spaces out later if you wish)
theIpHtml = [theIpHtml stringByReplacingOccurrencesOfString:
[ NSString stringWithFormat:@"%@>", text]
withString:@" "] ;
ipItemsArray =[theIpHtml componentsSeparatedByString:@" "];
an_Integer=[ipItemsArray indexOfObject:@"Address:"];
externalIP =[ipItemsArray objectAtIndex: ++an_Integer];
}
NSLog(@"%@",externalIP);
} else {
NSLog(@"Oops... g %d, %@", [error code], [error localizedDescription]);
}
}
return externalIP;
}
回答by Cinder6
For determining the IP address, I found this.
为了确定IP地址,我发现了 这个。
As for making it into a Cocoa app, add an NSTextField
(label) to your main window in Interface Builder, put in a button, add in an application controller (a subclass of NSObject
that you make), put in the outlet and the action, do the proper connenctions, and in the "get IP" method, put in that code and set the value for the label's stringValue
.
至于把它做成一个 Cocoa 应用程序,NSTextField
在 Interface Builder 的主窗口中添加一个(标签),放入一个按钮,添加一个应用程序控制器(NSObject
你创建的一个子类),放入插座和动作,做正确的连接,并在“获取 IP”方法中,放入该代码并设置标签的stringValue
.
You can use [[NSHost currentHost] address]
, but it won't always display what you like. On my system, for example, it gives my IPv6 address.
您可以使用[[NSHost currentHost] address]
,但它不会总是显示您喜欢的内容。例如,在我的系统上,它提供了我的 IPv6 地址。
EDIT:On my system, [[[NSHost currentHost] addresses] objectAtIndex:0]
has my IPv4 address.
编辑:在我的系统上,[[[NSHost currentHost] addresses] objectAtIndex:0]
有我的 IPv4 地址。
回答by Phillip Jacobs
[[NSHost currentHost] addresses]
will get you an array of IPs. Read the documentation for NSHost.
[[NSHost currentHost] addresses]
将为您提供一系列 IP。阅读NSHost的文档。
As for displaying that in a GUI, I recommend getting Aaron Hillegass' book Cocoa Programming for Mac OS X, or any Cocoa beginners book should teach that.
至于在 GUI 中显示它,我建议阅读 Aaron Hillegass 的书 Cocoa Programming for Mac OS X,或者任何 Cocoa 初学者书籍都应该教它。
回答by Oliver Pearmain
I just wrote this, may need some work but seems to work well on my machine...
我刚刚写了这个,可能需要一些工作,但似乎在我的机器上运行良好......
- (NSString *)getLocalIPAddress
{
NSArray *ipAddresses = [[NSHost currentHost] addresses];
NSArray *sortedIPAddresses = [ipAddresses sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
numberFormatter.allowsFloats = NO;
for (NSString *potentialIPAddress in sortedIPAddresses)
{
if ([potentialIPAddress isEqualToString:@"127.0.0.1"]) {
continue;
}
NSArray *ipParts = [potentialIPAddress componentsSeparatedByString:@"."];
BOOL isMatch = YES;
for (NSString *ipPart in ipParts) {
if (![numberFormatter numberFromString:ipPart]) {
isMatch = NO;
break;
}
}
if (isMatch) {
return potentialIPAddress;
}
}
// No IP found
return @"?.?.?.?";
}
回答by Vineet Choudhary
We can use hostWithName:
method with current host name. This will return only single local IPv4 and IPv6 IP, which we can filter easily.
我们可以使用hostWithName:
具有当前主机名的方法。这将仅返回单个本地 IPv4 和 IPv6 IP,我们可以轻松过滤它们。
We can get the current system host name using [[NSHost currentHost] name]
.
我们可以使用[[NSHost currentHost] name]
.
+(NSString *)getLocalIPAddress{
NSArray *ipAddresses = [[NSHost hostWithName:[[NSHost currentHost] name]] addresses];
for (NSString *ipAddress in ipAddresses) {
if ([ipAddress componentsSeparatedByString:@"."].count == 4) {
return ipAddress;
}
}
return @"Not Connected.";
}
So, this will solve all the problems mentions in comments of other answers. Also, this significantly work more faster than other solution mention here.
因此,这将解决其他答案评论中提到的所有问题。此外,这比此处提到的其他解决方案的工作速度要快得多。