iOS 获取设备uuid,公网ip,手机ip等信息

最近公司app需要添加获取用户信息的新功能。将这些
功能写下来,以备不时之需。

获取手机uuid

+ (String *)getUUID 
{  
    return [[[UIDevice currentDevice] identifierForVendor] UUIDString];

}

获取操作系统版本

+ (float)getIOSVersion  
{  
    return [[[UIDevice currentDevice] systemVersion] floatValue];  
}  

获取位置信息

设置请求访问信息

<key>NSLocationWhenInUseUsageDescription</key>
<string>when</string>
<key>NSLocationAlwaysUsageDescription</key>
<string>always</string>

完整代码

#import <CoreLocation/CoreLocation.h>
@interface MainViewController ()<CLLocationManagerDelegate>
{
    CLLocationManager *locationmanager;//定位服务
    NSString *currentCity;//当前城市
    NSString *strlatitude;//经度
    NSString *strlongitude;//纬度
}
@end

@implementation MainViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self startLocation];
    // Do any additional setup after loading the view.
}

//开始定位
- (void)startLocation {
    if ([CLLocationManager locationServicesEnabled]) {
        //        CLog(@"--------开始定位");
        self.locationManager = [[CLLocationManager alloc]init];
        self.locationManager.delegate = self;
        //控制定位精度,越高耗电量越大
        self.locationManager.desiredAccuracy = kCLLocationAccuracyKilometer;
        // 总是授权
        [self.locationManager requestAlwaysAuthorization];
        self.locationManager.distanceFilter = 10.0f;
        [self.locationManager requestAlwaysAuthorization];
        [self.locationManager startUpdatingLocation];
    }
}

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
    if ([error code] == kCLErrorDenied) {
        CLog(@"访问被拒绝");
    }
    if ([error code] == kCLErrorLocationUnknown) {
        CLog(@"无法获取位置信息");
    }
}

//定位代理经纬度回调
#pragma mark 定位成功后则执行此代理方法
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations
{
    [locationmanager stopUpdatingHeading];
    //旧址
    CLLocation *currentLocation = [locations lastObject];
    CLGeocoder *geoCoder = [[CLGeocoder alloc]init];
    //打印当前的经度与纬度
    NSLog(@"%f,%f",currentLocation.coordinate.latitude,currentLocation.coordinate.longitude);
    
    //反地理编码
    [geoCoder reverseGeocodeLocation:currentLocation completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
        if (placemarks.count > 0) {
            CLPlacemark *placeMark = placemarks[0];
            currentCity = placeMark.locality;
            if (!currentCity) {
                currentCity = @"无法定位当前城市";
            }
            
            /*看需求定义一个全局变量来接收赋值*/
            NSLog(@"----%@",placeMark.country);//当前国家
            NSLog(@"%@",currentCity);//当前的城市
//            NSLog(@"%@",placeMark.subLocality);//当前的位置
//            NSLog(@"%@",placeMark.thoroughfare);//当前街道
//            NSLog(@"%@",placeMark.name);//具体地址
            
        }
    }];
    
}

获取公网ip

+(NSString *)deviceWANIPAddress  
{  
    NSURL *ipURL = [NSURL URLWithString:@"http://ip.taobao.com/service/getIpInfo.php?ip=myip"];  
    NSData *data = [NSData dataWithContentsOfURL:ipURL];  
    NSDictionary *ipDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];?  
    NSString *ipStr = nil;  
    if (ipDic && [ipDic[@"code"] integerValue] == 0) { //获取成功  
        ipStr = ipDic[@"data"][@"ip"];  
    }  
    return (ipStr ? ipStr : @"");  
}  

获取当前时间

获取当前时间

- (NSString *)currentDateStr{
    NSDate *currentDate = [NSDate date];//获取当前时间,日期
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];// 创建一个时间格式化对象
    [dateFormatter setDateFormat:@"YYYY/MM/dd hh:mm:ss SS "];//设定时间格式,这里可以设置成自己需要的格式
    NSString *dateString = [dateFormatter stringFromDate:currentDate];//将时间转化成字符串
    return dateString;
}

获取当前时间戳

//获取当前时间戳
- (NSString *)currentTimeStr{
    NSDate* date = [NSDate dateWithTimeIntervalSinceNow:0];//获取当前时间0秒后的时间
    NSTimeInterval time=[date timeIntervalSince1970]*1000;// *1000 是精确到毫秒,不乘就是精确到秒
    NSString *timeString = [NSString stringWithFormat:@"%.0f", time];
    return timeString;
}

获取移动端ip

需要的头文件

//IP地址需求库  
#import <sys/socket.h>  
#import <sys/sockio.h>  
#import <sys/ioctl.h>  
#import <net/if.h>  
#import <arpa/inet.h>  

获取ip地址

//获取设备IP地址  
+ (NSString *)getDeviceIPAddresses  
{  
    int sockfd = socket(AF_INET,SOCK_DGRAM, 0);  
    // if (sockfd <</span> 0) return nil; //这句报错,由于转载的,不太懂,注释掉无影响,懂的大神欢迎指导  
    NSMutableArray *ips = [NSMutableArray array];  
  
    int BUFFERSIZE =4096;  
      
    struct ifconf ifc;  
      
    char buffer[BUFFERSIZE], *ptr, lastname[IFNAMSIZ], *cptr;  
      
    struct ifreq *ifr, ifrcopy;  
      
    ifc.ifc_len = BUFFERSIZE;  
      
    ifc.ifc_buf = buffer;  
      
    if (ioctl(sockfd,SIOCGIFCONF, &ifc) >= 0){  
          
        for (ptr = buffer; ptr < buffer + ifc.ifc_len; ){  
              
            ifr = (struct ifreq *)ptr;  
              
            int len =sizeof(struct sockaddr);  
              
            if (ifr->ifr_addr.sa_len > len) {  
                len = ifr->ifr_addr.sa_len;  
            }  
              
            ptr += sizeof(ifr->ifr_name) + len;  
              
            if (ifr->ifr_addr.sa_family !=AF_INET) continue;  
              
            if ((cptr = (charchar *)strchr(ifr->ifr_name,':')) != NULL) *cptr =0;  
              
            if (strncmp(lastname, ifr->ifr_name,IFNAMSIZ) == 0)continue;  
              
            memcpy(lastname, ifr->ifr_name,IFNAMSIZ);  
              
            ifrcopy = *ifr;  
              
            ioctl(sockfd,SIOCGIFFLAGS, &ifrcopy);  
              
            if ((ifrcopy.ifr_flags &IFF_UP) == 0)continue;  
  
            NSString *ip = [NSString stringWithFormat:@"%s",inet_ntoa(((struct sockaddr_in *)&ifr->ifr_addr)->sin_addr)];  
            [ips addObject:ip];  
        }  
    }  
    close(sockfd);  
  
    NSString *deviceIP =@"";  
      
    for (int i=0; i < ips.count; i++){  
        if (ips.count >0){  
            deviceIP = [NSString stringWithFormat:@"%@",ips.lastObject];  
        }  
    }  
      
    return deviceIP;  
}  

以上方法没有进行测试,使用时需要自己测试。

最后给出自己的个人博客

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 219,490评论 6 508
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,581评论 3 395
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 165,830评论 0 356
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,957评论 1 295
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,974评论 6 393
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,754评论 1 307
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,464评论 3 420
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,357评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,847评论 1 317
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,995评论 3 338
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,137评论 1 351
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,819评论 5 346
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,482评论 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 32,023评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,149评论 1 272
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,409评论 3 373
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 45,086评论 2 355

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,672评论 18 139
  • feisky云计算、虚拟化与Linux技术笔记posts - 1014, comments - 298, trac...
    不排版阅读 3,855评论 0 5
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 172,185评论 25 707
  • 后来他总是在怀念那半年的时光、甚至是偶尔低喃着诉说那一段时期的故事、似乎有了这样的经历让他感觉自己高人一等了一...
    破孩殇醇阅读 238评论 0 1
  • 其實真的還有霹靂無敵多的事還沒做,但是到七點的時候整個毫無猶豫的就想離開公司了,我都不知道這是好還是壞,以前事情不...
    tracyni阅读 208评论 1 0