【问题标题】:Server IP address of wifi in iphone deviceiphone设备中wifi的服务器IP地址
【发布时间】:2012-11-07 07:14:05
【问题描述】:

我想在 iphone 设备中获取 wifi 的服务器 IP 地址。我不想要 iphone 设备中 wifi 的设备 IP 地址...如果有任何 API 用于获取 iphone 中 wifi 的服务器 IP 地址..请参考并分享这些链接...

例如。我的 iPhone 连接到一些 XYZ wifi。是否可以获取提供 WIFI 访问的服务器的 Live IP 地址?

【问题讨论】:

  • wifi的服务器IP地址是什么意思?
  • 我们连接的wifi服务器,我需要那个服务器的ip地址。
  • 无法理解您的尝试,请进一步澄清您的问题
  • 你的意思是你的路由器/接入点IP在局域网?还是您的 ISP 授予您的路由器的 ip?

标签: iphone objective-c


【解决方案1】:

Objective-C 方法以 NSString 形式检索 wifi 连接的 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) 

{
         // Check if interface is en0 which is the wifi connection on the iPhone
         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; 

}

如果这对您不起作用,您可能还需要在类实现的顶部包含以下 C 标头

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

【讨论】:

猜你喜欢
  • 2023-03-03
  • 2013-12-14
  • 1970-01-01
  • 2012-08-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-09-14
相关资源
最近更新 更多