2013-03-14 3 views
2

Lorsque vous disposez d'une connexion mobile, vous avez souvent 2 adresses IP, une adresse IP externe qui peut être consulté à http://www.whatsmyip.org/comment obtenir mon Xcode IP dynamique

résultat IP: 190.106.XX.XX

et lorsque je connecte mon PC à un BAM je reçois un autre IP comme indiqué dans

enter image description here

résultat IP: 10.69.XX.XX

l'iPAD Je dois habiller d'obtenir est celle de la fenêtre pour l'utiliser (10.69.XX.XX)

j'ai vérifié une application Android et montré ce résultat comme RMNET0

et m'a dit en java il a utilisé demande. getRemoteHost(). pour obtenir ces données

Toute information que je vais apprécier

grâce à l'avance

Répondre

1

Je l'ai essayé avec succès, je l'espère pour afficher l'adresse IP que vous vous aide

+(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; 
} 

peut utiliser:

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

merci cela fonctionne très bien! –

Questions connexes