2013-03-14 43 views
2

當你有一個移動連接,你往往有2個IP地址,可以在http://www.whatsmyip.org/如何讓我的動態IP的Xcode

IP結果進行協商外部IP:190.106.XX.XX

和當我在我的PC連接到BAM我得到另一個IP如圖

enter image description here

IP結果:10.69.XX.XX

了iPad打扮我需要從窗口使用它(10.69.XX.XX)的一個

我查了一個Android應用程序,且該結果作爲RMNET0

所示,並告訴我在java中他用請求。 getRemoteHost()。獲得這個數據

的任何信息,我會很高興提前

感謝

回答

1

我已經成功嘗試這樣做,我希望它可以幫助你

+(NSString *)getIpLocally:(NSString *)networkInterface ipVersion:(int)ipVersion 
{ 
    if(ipVersion != 4 && ipVersion != 6) 
    { 
     NSLog(@"getIpLocally unknown version of IP: %i",ipVersion); 
     return nil; 
    } 

    NSString *networkInterfaceRef; 

    if ([networkInterface isEqualToString:@"ios_cellular"]) 
    { 
     networkInterfaceRef = @"pdp_ip0"; 
    } 
    else if([networkInterface isEqualToString:@"ios_wifi"]) 
    { 
     networkInterfaceRef = @"en0"; //en1 on simulator if mac on wifi 
    } 
    else 
    { 
     NSLog(@"getIpLocally unknown interface: %@",networkInterface); 
     return nil; 
    } 

    NSString *address = nil; 
    struct ifaddrs *addrs = NULL; 
    struct ifaddrs *cursor = NULL; 
    struct sockaddr_in *s4; 
    struct sockaddr_in6 *s6; 
    char buf[64]; 
    int success = 0; 

    // retrieve the current interfaces - returns 0 on success 
    success = getifaddrs(&addrs); 
    if (success == 0) 
    { 
     // Loop through linked list of interfaces 
     cursor = addrs; 
     while(cursor != NULL) 
     { 
      if((ipVersion == 4 && cursor->ifa_addr->sa_family == AF_INET) || 
       (ipVersion == 6 && cursor->ifa_addr->sa_family == AF_INET6)) 
      { 
       NSLog(@"Network Interface: %@",[NSString stringWithUTF8String:cursor->ifa_name]); 

       // Check if interface is en0 which is the wifi connection on the iPhone 
       if([[NSString stringWithUTF8String:cursor->ifa_name] isEqualToString:networkInterfaceRef]) 
       { 
        if(ipVersion == 4) 
        { 
         s4 = (struct sockaddr_in *)cursor->ifa_addr; 

         if (inet_ntop(cursor->ifa_addr->sa_family, (void *)&(s4->sin_addr), buf, sizeof(buf)) == NULL) 
         { 
          NSLog(@"%s: inet_ntop failed for v4!\n",cursor->ifa_name); 
         } 
         else{ 
          address = [NSString stringWithUTF8String:buf]; 
         } 
        } 
        if (ipVersion == 6) 
        { 
         s6 = (struct sockaddr_in6 *)(cursor->ifa_addr); 

         if (inet_ntop(cursor->ifa_addr->sa_family, (void *)&(s6->sin6_addr), buf, sizeof(buf)) == NULL) 
         { 
          NSLog(@"%s: inet_ntop failed for v6!\n",cursor->ifa_name); 
         } 
         else{ 
          address = [NSString stringWithUTF8String:buf]; 
         } 
        } 
       } 
      } 
      cursor = cursor->ifa_next; 
     } 
    } 
    // Free memory 
    freeifaddrs(addrs); 
    return address; 
} 

顯示的IP地址,你可以使用:

NSLog(@"ip v4: %@",[NSString getIpLocally:@"ios_cellular" ipVersion:4]); 
+0

感謝它很好用! – 2013-05-16 17:25:55

相關問題