IP地址? - 可可

时间:2009-08-26 01:32:24

标签: objective-c cocoa xcode user-interface

如何通过点击按钮制作显示您的IP地址的GUI程序?请问,没有困难的解释,我不久前就开始了Cocoa。

谢谢,

凯文

4 个答案:

答案 0 :(得分:8)

您可以通过两种方式获取IP地址:

1-如果你想获得当前使用的netwrok上的本地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-如果你想获得外部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;
}

答案 1 :(得分:7)

为了确定IP地址,我找到了 this

至于将它变成Cocoa应用程序,在Interface Builder的主窗口中添加NSTextField(标签),放入一个按钮,添加一个应用程序控制器(NSObject的子类, (制作),插入插座和动作,进行正确的连接,然后在“获取IP”方法中输入代码并设置标签stringValue的值。

您可以使用[[NSHost currentHost] address],但不会始终显示您喜欢的内容。例如,在我的系统上,它提供了我的IPv6地址。

编辑:在我的系统上,[[[NSHost currentHost] addresses] objectAtIndex:0]拥有我的IPv4地址。

答案 2 :(得分:4)

[[NSHost currentHost] addresses]将为您提供一系列IP。阅读documentation for NSHost

至于在GUI中显示,我建议让Aaron Hillegass为Mac OS X编写Cocoa Programming,或者任何Cocoa初学者书都应该教它。

答案 3 :(得分:4)

我刚刚写了这篇文章,可能需要一些工作,但似乎在我的机器上运行良好......

- (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 @"?.?.?.?";
}