其他分享
首页 > 其他分享> > iOS 一些设备信息的获取方法

iOS 一些设备信息的获取方法

作者:互联网

设备启动时间

- (NSString *)bootTimeInSec
{
    NSString *temp = [NSString stringWithFormat:@"%ld",bootSecTime()];
//    NSLog(@"bootTimeInSec : %@",temp);
    return temp;
}
static time_t bootSecTime(){
    struct timeval boottime;
    size_t len = sizeof(boottime);
    int mib[2] = {CTL_KERN, KERN_BOOTTIME};
    if( sysctl(mib, 2, &boottime, &len, NULL, 0) < 0)
    {
        return 0;
    }
    return boottime.tv_sec;
}

国家

- (NSString *)countryCode
{
    NSLocale *locale = [NSLocale currentLocale];
    NSString *countryCode = [locale objectForKey:NSLocaleCountryCode];
//    NSLog(@"countryCode : %@",countryCode);
    
    return countryCode;
}

语言

// 语言
- (NSString *)language {
    
    NSString *language;
    NSLocale *locale = [NSLocale currentLocale];
    if ([[NSLocale preferredLanguages] count] > 0) {
        language = [[NSLocale preferredLanguages]objectAtIndex:0];
        
    } else {
        language = [locale objectForKey:NSLocaleLanguageCode];
        
    }
//    NSLog(@"language : %@",language);
    
    return language;
}

设备名称

- (NSString *)deviceName
{
    
    if ([[[UIDevice currentDevice] name] length] == 0) {
        return @"";
    }
    
// md5 32位 小写
    NSString *deviceName = [CryptionUtils MD5ForLower32Bate:[[UIDevice currentDevice] name]];
//    NSLog(@"deviceName : %@",deviceName);
    return deviceName;
    
}

系统版本

- (NSString *)systemVersion
{
    NSString *version = [[UIDevice currentDevice] systemVersion];
    return version;
    
}

设备machine

- (NSString *)machine
{
    NSString *machine =getSystemHardwareByName(SIDFAMachine);
//    NSLog(@"machine: %@",machine);
    return machine == nil ? @"" : machine;
    
}
static const char *SIDFAMachine = "hw.machine";
static NSString *getSystemHardwareByName(const char *typeSpecifier)
{
    size_t size;
    sysctlbyname(typeSpecifier, NULL, &size, NULL, 0);
    char *answer = malloc(size);
    sysctlbyname(typeSpecifier, answer, &size, NULL,0); NSString *results = [NSString stringWithUTF8String:answer]; 
    free(answer);
    return results;
}

设备model

static const char *SIDFAModel = "hw.model";
- (NSString *)model
{
    NSString *model =getSystemHardwareByName(SIDFAModel);
//    NSLog(@"model : %@", model);
    return model == nil ? @"" : model;
}

运营商

- (NSString *)carrierInfo {
#if TARGET_IPHONE_SIMULATOR
    return @"SIMULATOR";
#else
    static dispatch_queue_t _queue;
    static dispatch_once_t once;
    dispatch_once(&once, ^{
        _queue = dispatch_queue_create([[NSString stringWithFormat:@"com.carr.%@", self] UTF8String], NULL);
    });
        __block NSString *carr = nil;
        dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
        dispatch_async(_queue, ^{
            CTTelephonyNetworkInfo *info = [[CTTelephonyNetworkInfo alloc] init];
            CTCarrier *carrier = nil;
            if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 12.1) {
                if ([info respondsToSelector:@selector(serviceSubscriberCellularProviders)]) {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wunguarded-availability-new"
                    NSArray *carrierKeysArray = [info.serviceSubscriberCellularProviders.allKeys sortedArrayUsingSelector:@selector(compare:)];
                    carrier = info.serviceSubscriberCellularProviders
                    [carrierKeysArray.firstObject];
                    if (!carrier.mobileNetworkCode) {
                        carrier =info.serviceSubscriberCellularProviders
                        [carrierKeysArray.lastObject];
                    }
#pragma clang diagnostic pop
                }
            }
            
            if(!carrier) {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
                carrier = info.subscriberCellularProvider;
#pragma clang diagnostic pop
            }
            if (carrier != nil) {
                NSString *networkCode = [carrier mobileNetworkCode];
                NSString *countryCode = [carrier mobileCountryCode];
                if (countryCode && [countryCode isEqualToString:@"460"] && networkCode) {
                    if ([networkCode isEqualToString:@"00"] ||
                        [networkCode isEqualToString:@"02"] ||
                        [networkCode isEqualToString:@"07"] ||
                        [networkCode isEqualToString:@"08"]) {
                        carr= @"中国移动";
                    }
                    if ([networkCode isEqualToString:@"01"] ||
                        [networkCode isEqualToString:@"06"] ||
                        [networkCode isEqualToString:@"09"]) {
                        carr= @"中国联通";
                    }
                    if ([networkCode isEqualToString:@"03"] ||
                        [networkCode isEqualToString:@"05"] ||
                        [networkCode isEqualToString:@"11"]) {
                        carr= @"中国电信";
                    }
                    if ([networkCode isEqualToString:@"04"]) {
                        carr= @"中国卫通";
                    }
                    if ([networkCode isEqualToString:@"20"]) {
                        carr= @"中国铁通";
                    }
                } else {
                    carr = [carrier.carrierName copy];
                }
                if (carr.length <= 0) {
                    carr = @"unknown";
                }
                dispatch_semaphore_signal(semaphore);
            }
        });
        dispatch_time_t t = dispatch_time(DISPATCH_TIME_NOW, 0.5*NSEC_PER_SEC);
        dispatch_semaphore_wait(semaphore, t);
        return [carr copy];
#endif
}

物理内存

- (NSString *)memory
{
    return [NSString stringWithFormat:@"%lld", [NSProcessInfo processInfo].physicalMemory];
}

硬盘容量

- (NSString *)disk
{
    int64_t space = -1;
    NSError *error = nil;
    NSDictionary *attrs = [[NSFileManager defaultManager] attributesOfFileSystemForPath:NSHomeDirectory() error:&error];
    if (!error) {
        space = [[attrs objectForKey:NSFileSystemSize] longLongValue];
    }
    
    if(space < 0) {
        space = -1;
    }
    NSString *temp = [NSString stringWithFormat:@"%lld",space];
//    NSLog(@"disk : %@", temp);
    return temp;
}

系统更新时间

- (NSString *)sysFileTime
{
    NSString *result = @"";
    NSString *information = @"L3Zhci9tb2JpbGUvTGlicmFyeS9Vc2VyQ29uZmlndXJhdGlvblByb2ZpbGVzL1B1YmxpY0luZm8vTUNNZXRhLnBsaXN0";
    NSData *data=[[NSData alloc]initWithBase64EncodedString:information options:0] ;
    NSString *dataString = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
    NSError *error = nil;
    NSDictionary *fileAttributes = [[NSFileManager defaultManager] attributesOfItemAtPath:dataString error:&error];
    if (fileAttributes) {
        id singleAttibute = [fileAttributes objectForKey:NSFileCreationDate];
        if ([singleAttibute isKindOfClass:[NSDate class]]) {
            NSDate *dataDate = singleAttibute;
            result = [NSString stringWithFormat:@"%f",[dataDate timeIntervalSince1970]];
        }
    }
//    NSLog(@"sysFileTime : %@", result);
    return result;
}

时区

- (NSString *) timeZone
{
    NSInteger offset = [NSTimeZone systemTimeZone].secondsFromGMT;
    NSString *temp = [NSString stringWithFormat:@"%ld",(long)offset];
    return temp;
}

国际区号

- (NSString *)internationalCode {
    // 1. 先获取手机号所在的国家地区码
    NSString *interCode = @"";
    NSString *mccCountryCode = [self MCCCountryCode];
    
    // 2. 根据国家地区码映射国际区号
    // 例如: 中国的国家地区码为 460   对应的国际区号为 86
    // 部分国家 地区码对应的国际区号请参考: https://www.mobibrw.com/2018/10871
    if ([mccCountryCode isEqualToString:@"460"]) {
        interCode = @"86";
    }
    
    return interCode;
     
}

国家地区码

- (NSString *)MCCCountryCode {
#if TARGET_IPHONE_SIMULATOR
    return @"SIMULATOR";
#else
    static dispatch_queue_t _queue;
    static dispatch_once_t once;
    dispatch_once(&once, ^{
        _queue = dispatch_queue_create([[NSString stringWithFormat:@"com.carr.%@", self] UTF8String], NULL);
    });
        __block NSString *mccCountryCode = nil;
        dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
        dispatch_async(_queue, ^{
            CTTelephonyNetworkInfo *info = [[CTTelephonyNetworkInfo alloc] init];
            CTCarrier *carrier = nil;
            if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 12.1) {
                if ([info respondsToSelector:@selector(serviceSubscriberCellularProviders)]) {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wunguarded-availability-new"
                    NSArray *carrierKeysArray = [info.serviceSubscriberCellularProviders.allKeys sortedArrayUsingSelector:@selector(compare:)];
                    carrier = info.serviceSubscriberCellularProviders
                    [carrierKeysArray.firstObject];
                    if (!carrier.mobileNetworkCode) {
                        carrier =info.serviceSubscriberCellularProviders
                        [carrierKeysArray.lastObject];
                    }
#pragma clang diagnostic pop
                }
            }
            
            if(!carrier) {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
                carrier = info.subscriberCellularProvider;
#pragma clang diagnostic pop
            }
            if (carrier != nil) {
                mccCountryCode = [carrier mobileCountryCode];
                

                
                dispatch_semaphore_signal(semaphore);
            }
        });
        dispatch_time_t t = dispatch_time(DISPATCH_TIME_NOW, 0.5*NSEC_PER_SEC);
        dispatch_semaphore_wait(semaphore, t);
    
        // countryCode 为国家/地区码
        //
    
        return [mccCountryCode copy];
#endif
}


 

标签:networkCode,isEqualToString,return,iOS,dispatch,获取,NSString,carrier,设备
来源: https://blog.csdn.net/u013712343/article/details/120525765