8

我想在应用程序中查找 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 或更高版本的操作系统中查找 IP 地址?

提前致谢。

4

8 回答 8

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];
}
于 2012-05-05T04:28:59.517 回答
4

As far as I know there is only one hacky way to do that. You basically open a socket and get its address using POSIX functions. Here is the code I used for this:

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

[NSHost currentHost] will also work, but it is deprecated and considered a "Private API" by Apple, so you won't be able to submit your application to App Store.

于 2010-03-02T11:45:23.090 回答
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];
于 2012-05-14T10:46:28.003 回答
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>
于 2011-09-05T12:48:26.393 回答
1

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

特此一课

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

NSString *myIp = [UIDevice localIPAddress];
NSString *myIp = [UIDevice localWiFiIPAddress];
NSString *myIp = [UIDevice whatismyipdotcom]; // is using @aamritrao solution
于 2012-05-05T06:20:34.417 回答
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。

于 2011-09-25T18:06:23.377 回答
1

获取 IP 地址有点麻烦。你确定你不能忍受每部 iPhone 唯一的设备 ID (UDID) 并且可以通过公共 API 轻松检索吗?

[UIDevice currentDevice].uniqueIdentifier

于 2010-03-14T21:00:31.683 回答
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;
    }
}
于 2011-10-01T21:02:21.593 回答