iOS最简单获取IP方法
2015-02-12 14:09
183 查看
复制一下代码,就可以直接使用了!
[objc] view
plaincopyprint?
//
// HYBIPHelper.h
// XiaoYaoUser
//
// Created by 黄仪标 on 14/12/9.
// Copyright (c) 2014年 xiaoyaor. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface HYBIPHelper : NSObject
/*!
* get device ip address
*/
+ (NSString *)deviceIPAdress;
@end
[objc] view
plaincopyprint?
//
// HYBIPHelper.m
// XiaoYaoUser
//
// Created by 黄仪标 on 14/12/9.
// Copyright (c) 2014年 xiaoyaor. All rights reserved.
//
#import "HYBIPHelper.h"
#include <ifaddrs.h>
#include <arpa/inet.h>
@implementation HYBIPHelper
+ (NSString *)deviceIPAdress {
NSString *address = @"an error occurred when obtaining ip address";
struct ifaddrs *interfaces = NULL;
struct ifaddrs *temp_addr = NULL;
int success = 0;
success = getifaddrs(&interfaces);
if (success == 0) { // 0 表示获取成功
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;
}
}
freeifaddrs(interfaces);
DDLogVerbose(@"手机的IP是:%@", address);
return address;
}
@end
[objc] view
plaincopyprint?
//
// HYBIPHelper.h
// XiaoYaoUser
//
// Created by 黄仪标 on 14/12/9.
// Copyright (c) 2014年 xiaoyaor. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface HYBIPHelper : NSObject
/*!
* get device ip address
*/
+ (NSString *)deviceIPAdress;
@end
[objc] view
plaincopyprint?
//
// HYBIPHelper.m
// XiaoYaoUser
//
// Created by 黄仪标 on 14/12/9.
// Copyright (c) 2014年 xiaoyaor. All rights reserved.
//
#import "HYBIPHelper.h"
#include <ifaddrs.h>
#include <arpa/inet.h>
@implementation HYBIPHelper
+ (NSString *)deviceIPAdress {
NSString *address = @"an error occurred when obtaining ip address";
struct ifaddrs *interfaces = NULL;
struct ifaddrs *temp_addr = NULL;
int success = 0;
success = getifaddrs(&interfaces);
if (success == 0) { // 0 表示获取成功
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;
}
}
freeifaddrs(interfaces);
DDLogVerbose(@"手机的IP是:%@", address);
return address;
}
@end
相关文章推荐
- IOS获取自身网络信息的方法,包括自身ip,路由地址等
- Python获取linux主机ip的简单实现方法
- ASP.NET获取IP及电脑名等信息的简单方法+通用类文件源码 (转载)
- 获取外网IP最简单的方法
- Linux系统下设置静态IP或自动获取动态IP的简单方法
- C#/.NET获取本机的外网IP地址的简单方法
- iOS 中简单的获取精准本地时间的方法
- iOS获取设备外网IP的方法
- php获取ip及网址的简单方法(必看)
- thrift/swift/nifty:获取客户端ip的简单方法
- ASP.NET获取IP及电脑名等信息的简单方法+通用类文件源码
- Python简单获取自身外网IP的方法
- Python获取linux主机ip的简单实现方法
- iOS---iOS9中获取通讯录的方法:Contacts. framework简单使用
- iOS手机屏幕朝向和简单的获取屏幕方向的方法
- C#获取本机IP的最简单方法
- ASP.NET获取IP及电脑名等信息的简单方法+通用类文件源码
- 简单介绍几种获取客户端真实IP可行方法
- 用C#编写获取远程IP,MAC的方法
- 获取ASP.NET连接数据库字符串最简单方法