UIDevice - iOS 设备

感觉就 获取系统版本用的最多。。
** [[UIDevice currentDevice] systemVersion] **

基本信息

- (void)deviceInfo {
    UIDevice *currentDevice = [UIDevice currentDevice];// 当前设备

    NSLog(@"%@",currentDevice.name);
    NSLog(@"%@",currentDevice.model);
    NSLog(@"%@",currentDevice.localizedModel);
    NSLog(@"%@",currentDevice.systemName);
    NSLog(@"%@",currentDevice.systemVersion);// 一般拿这个 判断 iOS 9.2 还是啥啥啥的。基本都搞成宏定义了是吧。
    NSLog(@"%@",currentDevice.identifierForVendor);
    NSLog(@"%zi",currentDevice.multitaskingSupported);
    NSLog(@"%zi",currentDevice.userInterfaceIdiom);
    /*! 设备交互界面类型
     typedef NS_ENUM(NSInteger, UIUserInterfaceIdiom) {
     UIUserInterfaceIdiomUnspecified = -1,
     UIUserInterfaceIdiomPhone NS_ENUM_AVAILABLE_IOS(3_2), // iPhone and iPod touch style UI
     UIUserInterfaceIdiomPad NS_ENUM_AVAILABLE_IOS(3_2), // iPad style UI
     UIUserInterfaceIdiomTV NS_ENUM_AVAILABLE_IOS(9_0), // Apple TV style UI
     };
     */
}

设备方向

 
- (void)orientationControl {
    // 通知 开关,默认开
    [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
    [[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications];
    
    // 判断是否开启通知
    if ([UIDevice currentDevice].generatesDeviceOrientationNotifications) {
        // 添加通知
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(OrientationDidChange:) name:UIDeviceOrientationDidChangeNotification object:nil];
    }
}

// 通知 回调 发现改变
- (void)OrientationDidChange:(NSNotification *)notification {
    NSLog(@"%@",notification.userInfo);// 貌似没什么软用
    NSLog(@"%zi",[UIDevice currentDevice].orientation);// 还是查看这个比较靠谱。
    /*! 上 下 左 右 正 反
    typedef NS_ENUM(NSInteger, UIDeviceOrientation) {
        UIDeviceOrientationUnknown,
        UIDeviceOrientationPortrait,            // Device oriented vertically, home button on the bottom
        UIDeviceOrientationPortraitUpsideDown,  // Device oriented vertically, home button on the top
        UIDeviceOrientationLandscapeLeft,       // Device oriented horizontally, home button on the right
        UIDeviceOrientationLandscapeRight,      // Device oriented horizontally, home button on the left
        UIDeviceOrientationFaceUp,              // Device oriented flat, face up
        UIDeviceOrientationFaceDown             // Device oriented flat, face down
    } __TVOS_PROHIBITED;
    */
    
    
// 系统默认 inline 函数,判断是否竖屏,横屏,其实就是 上面的 (上或者下)和 (左或者右)。
    if (UIDeviceOrientationIsPortrait([UIDevice currentDevice].orientation)) {
        NSLog(@"Portrait");
    }
    if (UIDeviceOrientationIsLandscape([UIDevice currentDevice].orientation)) {
        NSLog(@"Landscape");
    }
}

电池信息

- (void)batteryInfo {
    NSLog(@"电池状态 %zi",[UIDevice currentDevice].batteryState);
    NSLog(@"电池电量等级百分比0-1 : %f",[UIDevice currentDevice].batteryLevel);

    // 是否开启 电池信息检查
    [UIDevice currentDevice].batteryMonitoringEnabled = YES;
    // 添加 通知,有2 个。
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(batteryStateDidChange) name:UIDeviceBatteryStateDidChangeNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(batteryLevelDidChange) name:UIDeviceBatteryLevelDidChangeNotification object:nil];
    
}

- (void)batteryStateDidChange {
    NSLog(@"电池状态 %zi",[UIDevice currentDevice].batteryState);
    /*! 几个状态,恩~~
     typedef NS_ENUM(NSInteger, UIDeviceBatteryState) {
     UIDeviceBatteryStateUnknown,
     UIDeviceBatteryStateUnplugged,   // on battery, discharging
     UIDeviceBatteryStateCharging,    // plugged in, less than 100%
     UIDeviceBatteryStateFull,        // plugged in, at 100%
     };
     */
}

- (void)batteryLevelDidChange {
    NSLog(@"电池电量等级%f",[UIDevice currentDevice].batteryLevel);
}

顶部的距离感应

- (void)proximityControl {

    // 是否 接近 顶部的 距离传感器(正中央上面的原点!贴膜的朋友,可以看到膜是缺口的哦~),遮住屏幕会变黑,打电话时贴脸就黑一样,对应有通知。
    [UIDevice currentDevice].proximityMonitoringEnabled = YES;
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(proximityStateDidChange) name:UIDeviceProximityStateDidChangeNotification object:nil];
}

- (void)proximityStateDidChange {
        if ([UIDevice currentDevice].proximityState) {
            NSLog(@"要遮住了");
        } else {
            NSLog(@"走开了");
        }
}

其他

1

你可能感兴趣的:(UIDevice - iOS 设备)