IPhone:通过3G连接时获取IP地址 [英] IPhone : Obtaining Ip address when connected over 3G

查看:125
本文介绍了IPhone:通过3G连接时获取IP地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用iphone SDK 3.1.2

using iphone SDK 3.1.2

如果通过iphone上的3G连接到互联网,我如何找出正在使用的IP地址
这个界面。

If connected via 3G on the iphone to internet, how do i find out what ip address is being used for this interface.

谢谢

推荐答案

这是一个代码片段可能会有所帮助:

Here is a code piece that might help:

// Matt Brown's get WiFi IP addy solution
// Author gave permission to use in Cookbook under cookbook license
// http://mattbsoftware.blogspot.com/2009/04/how-to-get-ip-address-of-iphone-os-v221.html
+ (NSString *) localAddressForInterface:(NSString *)interface {
    BOOL success;
    struct ifaddrs * addrs;
    const struct ifaddrs * cursor;

    success = getifaddrs(&addrs) == 0;
    if (success) {
        cursor = addrs;
        while (cursor != NULL) {
            // the second test keeps from picking up the loopback address
            if (cursor->ifa_addr->sa_family == AF_INET && (cursor->ifa_flags & IFF_LOOPBACK) == 0) 
            {
                NSString *name = [NSString stringWithUTF8String:cursor->ifa_name];
                if ([name isEqualToString:interface])  // Wi-Fi adapter
                    return [NSString stringWithUTF8String:inet_ntoa(((struct sockaddr_in *)cursor->ifa_addr)->sin_addr)];
            }
            cursor = cursor->ifa_next;
        }
        freeifaddrs(addrs);
    }
    return nil; 
}

+ (NSString *) localWiFiIPAddress
{
    return [UIDevice localAddressForInterface:@"en0"];
}

+ (NSString *) localCellularIPAddress
{
    return [UIDevice localAddressForInterface:@"pdp_ip0"];
}

我自己并没有试图获得这段代码。这是原始网址

I am not trying to get credit for this piece of code myself. Here's the original URL.

函数调用 getifaddrs

希望它有所帮助。

这篇关于IPhone:通过3G连接时获取IP地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆