首页 » IOS » iOS获取当前设备的型号、分辨率、运营商以及地理位置

iOS获取当前设备的型号、分辨率、运营商以及地理位置

2017-03-22 18:24:21阅读(138)

最近做一个需求:后端需要统计使用我们APP产品的用户手机的各种信息,这些信息包括:设备品牌、设备型号、设备分辨率、操作系统、运营商、APP版本号、设备ID(UUID)、地址位置。这些信息其实获取也都不是很难,简单记录一下:

//首先获取当前设备对象
    UIDevice *device = [UIDevice currentDevice];
    /* 系统名 */
    NSString *systemName = [device systemName];
    /* 系统版本号 */
    NSString *phoneVersion = [device systemVersion];
    /* 设备品牌 */
    NSString *brand = [device model];
    /* 设备型号 */  iphoneType方法在后面实现
    NSString *model = [self iphoneType];
    /* 分辨率 */ 
    CGRect rect = [UIScreen mainScreen].bounds;
    CGSize size = rect.size;
    CGFloat scale = [UIScreen mainScreen].scale;
    CGFloat width = size.width*scale;
    CGFloat height = size.height*scale;
    /* 运营商 */
首先需要引入以下两个系统类:
#import <CoreTelephony/CTTelephonyNetworkInfo.h>
#import <CoreTelephony/CTCarrier.h>
    CTTelephonyNetworkInfo *info = [[CTTelephonyNetworkInfo alloc] init];
    CTCarrier *carrier = [info subscriberCellularProvider];
    //当前手机所属运营商名称
    NSString *mobileCarrier;
    //先判断有没有SIM卡,如果没有则不获取本机运营商
    if (!carrier.isoCountryCode) {
        mobileCarrier = @"无运营商";
    }else{
        mobileCarrier = [carrier carrierName];
    }
    /* app当前版本号 */
    NSDictionary *infoDict = [[NSBundle mainBundle] infoDictionary];
    NSString *currentVersion = infoDict[@"CFBundleShortVersionString"];
    /* 设备ID */
    NSUUID *uuid = device.identifierForVendor;
    NSString *devideID = uuid.UUIDString;
    /* 地理位置 */
    [self Collocaction];

获取设备型号的方法:

- (NSString *)iphoneType {
    //需要导入头文件:#import <sys/utsname.h>
    struct utsname systemInfo;
    uname(&systemInfo);
    NSString *platform = [NSString stringWithCString:systemInfo.machine encoding:NSASCIIStringEncoding];
    if ([platform isEqualToString:@"iPhone1,1"]) return @"iPhone 2G";
    if ([platform isEqualToString:@"iPhone1,2"]) return @"iPhone 3G";
    if ([platform isEqualToString:@"iPhone2,1"]) return @"iPhone 3GS";
    if ([platform isEqualToString:@"iPhone3,1"]) return @"iPhone 4";
    if ([platform isEqualToString:@"iPhone3,2"]) return @"iPhone 4";
    if ([platform isEqualToString:@"iPhone3,3"]) return @"iPhone 4";
    if ([platform isEqualToString:@"iPhone4,1"]) return @"iPhone 4S";
    if ([platform isEqualToString:@"iPhone5,1"]) return @"iPhone 5";
    if ([platform isEqualToString:@"iPhone5,2"]) return @"iPhone 5";
    if ([platform isEqualToString:@"iPhone5,3"]) return @"iPhone 5c";
    if ([platform isEqualToString:@"iPhone5,4"]) return @"iPhone 5c";
    if ([platform isEqualToString:@"iPhone6,1"]) return @"iPhone 5s";
    if ([platform isEqualToString:@"iPhone6,2"]) return @"iPhone 5s";
    if ([platform isEqualToString:@"iPhone7,1"]) return @"iPhone 6 Plus";
    if ([platform isEqualToString:@"iPhone7,2"]) return @"iPhone 6";
    if ([platform isEqualToString:@"iPhone8,1"]) return @"iPhone 6s";
    if ([platform isEqualToString:@"iPhone8,2"]) return @"iPhone 6s Plus";
    if ([platform isEqualToString:@"iPhone8,4"]) return @"iPhone SE";
    if ([platform isEqualToString:@"iPhone9,1"]) return @"iPhone 7";
    if ([platform isEqualToString:@"iPhone9,2"]) return @"iPhone 7 Plus";
    return platform;
}

定位相关的实现:使用CLLocationManager和CLLocation两个类

-(void)Collocaction{
    self.locationManager = [[CLLocationManager alloc] init];
    [self.locationManager  requestAlwaysAuthorization];//请求授权
    if ([CLLocationManager locationServicesEnabled]) {
        self.locationManager.delegate = self;
        self.locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters;
        self.locationManager.distanceFilter = 200.0f;
        [self.locationManager startUpdatingLocation];
    }
    else {
        NSLog(@"请开启定位功能!");
    }
}
//定位代理方法
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation
           fromLocation:(CLLocation *)oldLocation {
    // 获取当前所在的城市名
    CLGeocoder *geocoder = [[CLGeocoder alloc] init];
    //根据经纬度反向地理编译出地址信息
    [geocoder reverseGeocodeLocation:newLocation completionHandler:^(NSArray *array, NSError *error)
     {
         if (array.count > 0)
         {
             CLPlacemark *placemark = [array objectAtIndex:0];
             //获取城市
             NSString *city = placemark.locality;
             if (!city) {
                 //四大直辖市的城市信息无法通过locality获得,只能通过获取省份的方法来获得(如果city为空,则可知为直辖市)
                 city = placemark.administrativeArea;
             }
             //详细地址
             NSString *location = placemark.name;
             //拼接成最终的位置
             NSString *myLocation = [NSString stringWithFormat:@"%@%@",city,location];
         }
         else if (error == nil && [array count] == 0)
         {
             NSLog(@"No results were returned.");
         }
         else if (error != nil)
         {
             NSLog(@"An error occurred = %@", error);
         }
     }];
    //停止定位
    [manager stopUpdatingLocation];
}
// 定位失误时调用
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
    NSLog(@"error:%@",error);
}

最新发布

CentOS专题

关于本站

5ibc.net旗下博客站精品博文小部分原创、大部分从互联网收集整理。尊重作者版权、传播精品博文,让更多编程爱好者知晓!

小提示

按 Ctrl+D 键,
把本文加入收藏夹