как получить ip-адрес iphone программно
Я получил веб-сервер и работал в iphone, используя Mongoose . Но проблема в том, как я могу получить ip-адрес своего iphone / ipad, чтобы пользователь знал, где он может получить доступ к серверу. Я обнаружил, что [NSHost addresses]
могут выполнять эту работу, но я разрабатываю для магазина приложений, и это недокументированный метод.
- UISwitch в ячейке UITableView
- Как заставить приложение запускать NSTimer в фоновом режиме?
- Как я могу перебирать все подпункты UIView, а также их подзоны и их подпрограммы
- Рисование поэтапно в UIView (iPhone)
- Как получить счетчик ссылок NSObject?
- 2D-массивы с использованием NSMutableArray
- Создание UIImage в форме круга
- Не удалось добавить объекты в NSMutableArray в Objective C
#include #include // Get the INTERNAL ip address - (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; }
Я считаю [NSHost address];
документировано, оно перечислено здесь: http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSHost_Class/Reference/Reference.html
Используйте [[NSHost currentHost] address];
для получения IP.