iOS 获取设备uuid,公网ip,手机ip等信息
【摘要】 最近公司app需要添加获取用户信息的新功能。将这些 功能写下来,以备不时之需。获取手机uuid+ (String *)getUUID { return [[[UIDevice currentDevice] identifierForVendor] UUIDString];}获取操作系统版本+ (float)getIOSVersion { return [[[UIDevi...
最近公司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; }
以上方法没有进行测试,使用时需要自己测试。
最后给出自己的个人博客。
转载声明:本文转载自异步社区
原文链接:https://www.epubit.com/articleDetails?id=Ndd4c5a92-8d92-4f42-abfe-337718b71243
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)