我想在应用程序中找到IP地址.我能找到它.但是,问题是,它在iphone os 2.0左右工作.但是,在iphone os 3.0中它给了我一个警告:
warning: no '+currentHost' method found warning: (Messages without a matching method signature)
我正在使用此代码,它与os 2.0版一起工作正常.
-(NSString*)getAddress { char iphone_ip[255]; strcpy(iphone_ip,"127.0.0.1"); // if everything fails NSHost* myhost = [NSHost currentHost]; if (myhost) { NSString *ad = [myhost address]; if (ad) strcpy(iphone_ip,[ad cStringUsingEncoding: NSISOLatin1StringEncoding]); } return [NSString stringWithFormat:@"%s",iphone_ip];
}
如何在iphone os 3.0或更高版本的os版本中找到IP地址?
提前致谢.
#include#include #include #include // retun the host name + (NSString *)hostname { char baseHostName[256]; int success = gethostname(baseHostName, 255); if (success != 0) return nil; baseHostName[255] = '\0'; #if !TARGET_IPHONE_SIMULATOR return [NSString stringWithFormat:@"%s.local", baseHostName]; #else return [NSString stringWithFormat:@"%s", baseHostName]; #endif } // return IP Address + (NSString *)localIPAddress { struct hostent *host = gethostbyname([[self hostname] UTF8String]); if (!host) {herror("resolv"); return nil;} struct in_addr **list = (struct in_addr **)host->h_addr_list; return [NSString stringWithCString:inet_ntoa(*list[0]) encoding:NSUTF8StringEncoding]; }