UIDevice

记录UIDevice的属性和方法

    UIDevice *device = [UIDevice currentDevice];
    //属性都是readOnly
    //设备名称 iPhone 11
    NSLog(@"%@",device.name);
    //系统名称 iOS
    NSLog(@"%@",device.systemName);
    //系统版本 13
    NSLog(@"%@",device.systemVersion);
    //设备类型 iPhone
    NSLog(@"%@",device.model);
    //设备类型的本地实例化名称
    NSLog(@"%@",device.localizedModel);
    //当前设备上使用的接口样式
    //Unspecified未指定/Phone手机/Pad/TV/CarPlay  5种方式
    NSLog(@"%ld",(long)device.userInterfaceIdiom);
    //设备的UUID
    NSLog(@"%@",device.identifierForVendor);
    //返回设备的物理方向
    //横屏/竖屏 / 设备面朝上/朝下 等
    NSLog(@"%ld",(long)[device orientation]);
    //开始获取设备的物理方向
    [device beginGeneratingDeviceOrientationNotifications];
    //设备接受者是否开启获取设备方向的通知
    NSLog(@"%d",[device isGeneratingDeviceOrientationNotifications]);
    //结束获取设备的物理方向
    [device endGeneratingDeviceOrientationNotifications];
    //设备的电池电量
    NSLog(@"%.f",device.batteryLevel);
    //是否启用电池监视(是)或不(否)
    NSLog(@"%d",device.isBatteryMonitoringEnabled);
    //设备电池状态
    //未知、不插电、插电、满电  4种
    NSLog(@"%ld",(long)device.batteryState);
    //是否接近传感器
    //如果没有接近,总是返回NO
    NSLog(@"%d",device.proximityState);
    //是否监听传感器状态改变
    NSLog(@"%d",device.isProximityMonitoringEnabled);
    //仅当屏幕上出现启用输入视图且用户启用了输入单击时,才播放单击。暂未用到
    [device playInputClick];
    //监听通知名称
    //UIDeviceOrientationDidChangeNotification 物理方向发生改变
    //UIDeviceBatteryStateDidChangeNotification 电池🔋状态发生改变
    //UIDeviceBatteryLevelDidChangeNotification 电池电量发生改变
    //UIDeviceProximityStateDidChangeNotification 传感器状态发生改变
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(respondsToDeviceOrientationDidChange:) name:UIDeviceOrientationDidChangeNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(respondsToDeviceBatteryStateDidChange:) name:UIDeviceBatteryStateDidChangeNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(respondsToDeviceBatteryLevelDidChange:) name:UIDeviceBatteryLevelDidChangeNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(respondsToDeviceProximityStateDidChange:) name:UIDeviceProximityStateDidChangeNotification object:nil];

其中,下面这个监听物理方向改变的通知有一个坑

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(respondsToDeviceOrientationDidChange:) name:UIDeviceOrientationDidChangeNotification object:nil];

可以参考iOS开发中屏幕旋转的那些坑

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容