在iPhone中查找IP地址

时间:2010-03-02 06:43:03

标签: iphone ip-address nshost

我想在应用程序中找到IP地址。我能找到它。但是,问题是,它在iphone os 2.0左右工作。但是,在iphone os 3.0中它给了我一个警告:

warning: no '+currentHost' method found

warning: (Messages without a matching method signature)

我正在使用此代码,它可以与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]; 

}

如何在iphone os 3.0或更高版本的os版本中找到IP地址?

提前致谢。

8 个答案:

答案 0 :(得分:6)

#include <arpa/inet.h>
#include <netdb.h>
#include <net/if.h>
#include <ifaddrs.h>

// retun the host name
+ (NSString *)hostname
{
    char baseHostName[256]; 
    int success = gethostname(baseHostName, 255);
    if (success != 0) return nil;
    baseHostName[255] = '\0';

#if !TARGET_IPHONE_SIMULATOR
    return [NSString stringWithFormat:@"%s.local", baseHostName];
#else
    return [NSString stringWithFormat:@"%s", baseHostName];
#endif
}

// return IP Address
+ (NSString *)localIPAddress
{
    struct hostent *host = gethostbyname([[self hostname] UTF8String]);
    if (!host) {herror("resolv"); return nil;}
    struct in_addr **list = (struct in_addr **)host->h_addr_list;
    return [NSString stringWithCString:inet_ntoa(*list[0]) encoding:NSUTF8StringEncoding];
}

答案 1 :(得分:4)

据我所知,只有一种黑客方法可以做到这一点。您基本上打开一个套接字并使用POSIX函数获取其地址。这是我用于此的代码:

http://iphonesdksnippets.com/post/2009/09/07/Get-IP-address-of-iPhone.aspx

[NSHost currentHost]也可以使用,但它已被弃用并被Apple视为“私有API”,因此您将无法将应用程序提交到App Store。

答案 2 :(得分:4)

将此脚本放在运行PHP的Web服务器上:

<?php
$ip = getenv("REMOTE_ADDR");
echo $ip;
?>

在设备上调用此方法:

NSURL *scriptURL = [[NSURL alloc] initWithString:@"http://yourserver.com/script.php"]; 
NSString *ip = [NSString stringWithContentsOfURL: scriptURL encoding:NSASCIIStringEncoding error:nil];

答案 3 :(得分:3)

- (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) {
        // Check if interface is en0 which is the wifi connection on the iPhone
        // it may also be en1 on your ipad3.
            if([[NSString stringWithUTF8String:temp_addr->ifa_name] isEqualToString:@"en0"]) { 
                // 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; 
}

使用此来获取您的IP

如果有任何错误 请使用

#include <ifaddrs.h>
#include <arpa/inet.h>

答案 4 :(得分:1)

获取IP地址有点hacky。您确定无法使用每个iPhone独有的设备ID(UDID),并且可以通过公共API轻松检索吗?

[UIDevice currentDevice].uniqueIdentifier

答案 5 :(得分:1)

还有一种获取IP地址的方法和全球IP

NSString*  ip=@"http://www.whatismyip.org/";
NSURL *url = [[NSURL alloc] initWithString:ip]; 
NSString *ans = [NSString stringWithContentsOfURL:url encoding:NSASCIIStringEncoding error:&error];
NSLog(@"%@",ans);

上述网站将为您提供全球IP。 只需将它放在您的代码中并使用您想要的IP地址,并使用您的应用获取用户的位置,因为这将提供全局IP。

答案 6 :(得分:1)

你应该看看这个好项目: uidevice-extension

特别this class

在项目中导入UIDevice-Reachability.h然后尝试使用以下命令之一:

NSString *myIp = [UIDevice localIPAddress];
NSString *myIp = [UIDevice localWiFiIPAddress];
NSString *myIp = [UIDevice whatismyipdotcom]; // is using @aamritrao solution

答案 7 :(得分:0)

bool success;
struct ifaddrs *addrs;
const struct ifaddrs *cursor;
const struct sockaddr_dl *dlAddr;
const uint8_t *base;
int i;

success = getifaddrs(&addrs) == 0;
if (success) {
    cursor = addrs;
    while (cursor != NULL) {
        if ( (cursor->ifa_addr->sa_family == AF_LINK)
            && (((const struct sockaddr_dl *) cursor->ifa_addr)->sdl_type ==IFT_ETHER)
            ) {
            dlAddr = (const struct sockaddr_dl *) cursor->ifa_addr;
            //      fprintf(stderr, " sdl_nlen = %d\n", dlAddr->sdl_nlen);
            //      fprintf(stderr, " sdl_alen = %d\n", dlAddr->sdl_alen);
            base = (const uint8_t *) &dlAddr->sdl_data[dlAddr->sdl_nlen];
            printf(" MAC address ");
            for (i = 0; i < dlAddr->sdl_alen; i++) {
                if (i != 0) {
                    printf(":");
                }
                printf("%02x", base[i]);
            } 
            printf("\n");
        }
        cursor = cursor->ifa_next;
    }
}