有些支付的第三方是要用到下面这段代码的,顺便吐槽一下名气越大的支付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;  

更多推荐