iOS开发 ☞ 获取本机ip地址

本文提供了一种在iOS设备上获取IPv4和IPv6地址的方法,并通过Objective-C代码实现。文章详细介绍了如何遍历网络接口并针对不同的网络类型(如Wi-Fi和蜂窝数据)获取相应的IP地址。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

有些支付的第三方是要用到下面这段代码的,顺便吐槽一下名气越大的支付SDK,封装的越LOW,我没有说微信。。。。。
一些封装的比较好的SDK像极光推送,也难怪人家过了C轮即将上市。

//iPv4

- (NSString *)getIpAddresses{
    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;
}

//ipv6

NSString *address = @"an error occurred when obtaining ip address";
    struct ifaddrs *interfaces = NULL;
    struct ifaddrs *temp_addr = NULL;
    struct sockaddr_in *s4;
    struct sockaddr_in6 *s6;
    char buf[64];
    int success = 0;

    Reachability *reach = [Reachability reachabilityWithHostName:@"www.baidu.com"];
    NetworkStatus netStatus = [reach currentReachabilityStatus];

    //pdp_ip0 手机网络ip地址, en0 wifi 网络地址
    NSString *netType = @"en0";
    if (netStatus == ReachableViaWWAN) {
        netType = @"pdp_ip0";
    }
    // 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:netType])//en1 on simulator if mac on wifi
                  {
                      s4 = (struct sockaddr_in *)temp_addr->ifa_addr;

                      if (inet_ntop(temp_addr->ifa_addr->sa_family, (void *)&(s4->sin_addr), buf, sizeof(buf)) == NULL)
                      {
                      }
                      else{
                          address = [NSString stringWithUTF8String:buf];
                      }

                  }
              } else if (temp_addr->ifa_addr->sa_family == AF_INET6) {
                  // Check if interface is en0 which is the wifi connection on the iPhone
                  if([[NSString stringWithUTF8String:temp_addr->ifa_name] isEqualToString:netType])
                  {
                      s6 = (struct sockaddr_in6 *)(temp_addr->ifa_addr);

                      if (inet_ntop(temp_addr->ifa_addr->sa_family, (void *)&(s6->sin6_addr), buf, sizeof(buf)) == NULL)
                      {
                      }
                      else{
                          address = [NSString stringWithUTF8String:buf];
                      }

                  }
              }

              temp_addr = temp_addr->ifa_next;
          }
    }
    // Free memory
    freeifaddrs(interfaces);

    NSLog(@"手机的IP是:%@", address);
    return address;  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值