- (void)viewDidLoad { [super viewDidLoad]; //檢測設備朝向使用UIDevice,beginGeneratingDeviceOrientationNotifications方法向通知中心發送朝向信息 [[UIDevice currentDevice]beginGeneratingDeviceOrientationNotifications]; //建立通知中心 [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(orientationChanged:) name:UIDeviceOrientationDidChangeNotification object:nil]; }
-(void)orientationChanged:(NSNotification *)notification { UIDeviceOrientation orientation=[[UIDevice currentDevice]orientation]; switch (orientation) { case UIDeviceOrientationFaceUp: NSLog(@"設備正面朝上"); break; case UIDeviceOrientationFaceDown: NSLog(@"設備正面朝下"); break; case UIDeviceOrientationPortrait: NSLog(@"設備處於正常朝向,主屏幕按鈕在下方"); break; case UIDeviceOrientationPortraitUpsideDown: NSLog(@"設備處於縱向,主屏幕按鈕在上方"); break; case UIDeviceOrientationLandscapeLeft: NSLog(@"設備側立,左邊朝下"); break; case UIDeviceOrientationLandscapeRight: NSLog(@"設備側立,右邊朝下"); break; default: break; } }
附:檢測手機版本信息使用的也是UIDevice.
//獲取硬件信息 UIDevice *device=[UIDevice currentDevice]; //輸出版本號 NSLog(@"%@",device.systemVersion);