iOS开发之 获取手机的网络的ip地址
2015-04-22 14:44
302 查看
首先在使用的地方导入
#include <ifaddrs.h>
#include <arpa/inet.h>
然后直接调用
- (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;
}
#include <ifaddrs.h>
#include <arpa/inet.h>
然后直接调用
- (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;
}
相关文章推荐
- iOS开发 - 如何获取设备的总容量和可用容量 网络运营商 3g/wifi判断 手机型号
- iOS开发 - 如何获取设备的总容量和可用容量 网络运营商 3g/wifi判断 手机型号
- iOS 获取手机当前所连接的网络的IP地址
- iOS —— UIApplication 获取手机状态栏中的网络状态
- 【iOS开发笔记21/50】获取应用程序的名称和版本号 iOS 获取手机的型号,系统版本,软件名称,软件版本
- IOS 特定于设备的开发:获取和使用设备姿势(通过手机方向控制3d物体显示)
- 【iOS开发-从网络上获取图片尺寸】
- IOS开发-从网络获取图片并显示
- iOS开发之runtime的运用-获取当前网络状态
- iOS开发之网络编程--获取文件的MIMEType
- iOS开发之runtime的运用-获取当前网络状态
- ios开发——代码实现获得手机系统的名字和版本号&当前网络状态
- iOS项目开发实战——获取本机IP地址
- ios开发-获取手机相关信息
- iOS项目开发实战——iOS网络编程获取网页Html源代码
- iOS怎样获取手机的ip地址
- iOS开发-从iTunes中下载手机应用,并获取应用图标
- IOS开发获取手机内照片属性
- iOS 获取手机设备IP地址的两种方法