[英]IP Address? - Cocoa
如何通過單擊按鈕制作顯示您的IP地址的GUI程序? 請,沒有困難的解釋,我不久前剛剛開始了Cocoa。
謝謝,
凱文
您可以通過兩種方式獲取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;
}
為了確定IP地址,我發現了這一點 。
至於將它變成Cocoa應用程序,在Interface Builder的主窗口中添加一個NSTextField
(標簽),放入一個按鈕,添加一個應用程序控制器(你制作的NSObject
的子類),放入插座和動作,做正確的連接,並在“獲取IP”方法,輸入該代碼並設置標簽的stringValue
的值。
您可以使用[[NSHost currentHost] address]
,但它不會始終顯示您喜歡的內容。 例如,在我的系統上,它提供了我的IPv6地址。
編輯:在我的系統上, [[[NSHost currentHost] addresses] objectAtIndex:0]
有我的IPv4地址。
我剛剛寫了這個,可能需要一些工作,但似乎在我的機器上運行良好...
- (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 @"?.?.?.?";
}
我們可以使用hostWithName:
方法和當前主機名。 這將僅返回單個本地IPv4和IPv6 IP,我們可以輕松過濾。
我們可以使用[[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.";
}
因此,這將解決其他答案的評論中提到的所有問題。 此外,這顯着比這里提到的其他解決方案更快。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.