Determine device (iPhone, iPod Touch) with iOS

后端 未结 30 1810
礼貌的吻别
礼貌的吻别 2020-11-21 11:29

Is there a way to determine the device running an application. I want to distinguish between iPhone and iPod Touch, if possible.

30条回答
  •  故里飘歌
    2020-11-21 12:01

    More usable

    #include 
    #include 
    
    @interface UIDevice(Hardware)
    
    - (NSString *) platform;
    
    - (BOOL)hasRetinaDisplay;
    
    - (BOOL)hasMultitasking;
    
    - (BOOL)hasCamera;
    
    @end
    
    @implementation UIDevice(Hardware)
    
    - (NSString *) platform{
        int mib[2];
    size_t len;
    char *machine;
    
    mib[0] = CTL_HW;
    mib[1] = HW_MACHINE;
    sysctl(mib, 2, NULL, &len, NULL, 0);
    machine = malloc(len);
    sysctl(mib, 2, machine, &len, NULL, 0);
    
        NSString *platform = [NSString stringWithCString:machine encoding:NSASCIIStringEncoding];
        free(machine);
    return platform;
    }
    
    - (BOOL)hasRetinaDisplay {
        NSString *platform = [self platform];
        BOOL ret = YES;
        if ([platform isEqualToString:@"iPhone1,1"]) {
            ret = NO;
        }
        else
            if ([platform isEqualToString:@"iPhone1,2"])    ret = NO;
        else 
            if ([platform isEqualToString:@"iPhone2,1"])    ret = NO;
        else 
            if ([platform isEqualToString:@"iPod1,1"])      ret = NO;
        else
            if ([platform isEqualToString:@"iPod2,1"])      ret = NO;
        else
            if ([platform isEqualToString:@"iPod3,1"])      ret = NO;
        return ret;
    }
    
    - (BOOL)hasMultitasking {
        if ([self respondsToSelector:@selector(isMultitaskingSupported)]) {
            return [self isMultitaskingSupported];
        }
        return NO;
    }
    
    - (BOOL)hasCamera {
       BOOL ret = NO;
       // check camera availability
       return ret;
    }
    
    @end
    

    you can reading properties with

    NSLog(@"platform %@, retita %@, multitasking %@", [[UIDevice currentDevice] platform], [[UIDevice currentDevice] hasRetinaDisplay] ? @"YES" : @"NO" , [[UIDevice currentDevice] hasMultitasking] ? @"YES" : @"NO");
    

提交回复
热议问题