タイトルとおりです。
UIDeviceクラスを利用してiPhoneやiPadのデバイス情報を取得することができます。
デバイスの現在の向きや、バッテリー情報、OSのバージョンなどを調べる時に使います(^ω^)
UIDeviceクラスを利用して取得できる端末情報
| プロパティ | 名前 |
| name | デバイスの名前 ( 設定 > 一般 > 情報 > 名前 で設定したもの) |
|---|---|
| model | デバイスのモデル(iPhone,iPad,iPodtouthなど) |
| localizeModel | ローカルバージョン |
| systemName | OS名 |
| systemVersion | OSのバージョン |
以下のようにUIDeviceクラスのcurrentDeviceメソッドでシングルトンのインスタンスを取得、
プロパティを参照すればOKです。
UIDevice *dev = [UIDevicecurrentDevice];
NSLog(@"name %@" , dev.name );
NSLog(@"model %@" , dev.model );
NSLog(@"systemName %@", dev.systemName );
バッテリー情報を取得する
バッテリー情報も先ほどと同じように参照して取得できますが、
取得剃る前にbatteryMonitoringEnabledプロパティをYESにして有効にする必要があるので注意。
・バッテリー関連のプロパティ
| プロパティ | 名前 |
| batteryLevel | バッテリー残量(0.0~1.0) |
|---|---|
| batteryState | バッテリー状態 |
| batteryMonitoringEnabled | バッテリ状態有効フラグ |
二番目のバッテリー状態は以下の様な定数のどれかが返ってきます。
・バッテリー状態の定数
| 定数名 | 説明 |
| UIDeviceBatteryStateUnknown | 不明 |
|---|---|
| UIDeviceBatteryStateUnplugged | 充電していない |
| stringByAppendingFormat | 充電中 |
| UIDeviceBatteryStateFull | 充電中で満タン |
・使用例
UIDevice *dev = [UIDevicecurrentDevice];
dev.batteryMonitoringEnabled = YES;
NSLog(@"batteryLevel -%f" , dev.batteryLevel );
if( UIDeviceBatteryStateUnknown == dev.batteryState ){
NSLog(@"不明");
}elseif( UIDeviceBatteryStateUnplugged == dev.batteryState ){
NSLog(@"充電していない");
}elseif( UIDeviceBatteryStateCharging == dev.batteryState ){
NSLog(@"充電なう");
}elseif( UIDeviceBatteryStateFull == dev.batteryState ){
NSLog(@"満タン!");
}
現在のデバイスの向きを調べる
デバイスの現在の向きを調べるにはorientationプロパティを調べます。
以下の定数のどれかを返します。
・デバイスの向き
| 定数名 | 説明 |
| UIDeviceOrientationUnknown | 不明 |
|---|---|
| UIDeviceOrientationPortrait | 普通の向き |
| UIDeviceOrientationPortraitUpsideDown | 逆さま |
| UIDeviceOrientationLandscapeLeft | 横向き(左側が下) |
| UIDeviceOrientationLandscapeRight | 横向き(右側が下) |
ここで注意が必要です。
ViewControllerのライフサイクル内のViewが作られる前(viewDidLoadやviewWillAppear)でこの値を取得すると、不明(UIDeviceOrientationUnknown)と返ってきてしまいます。
ビューが作られた後のviewDidAppearの中で取得すればうまく画面の向きが取得できるみたいです。
もしくは、画面が向きが切り替わる時に呼ばれるshouldAutorotateメソッドの中でも取得ができます。
(以前紹介した記事参照)
・【iPhoneアプリ】iOS6では画面の向きが変わった時に呼ばれる関数が違う
サンプルプログラム
上記のまとめです。
・ストーリーボード使用。
・ARC使用
GitHubにもアップしてあります。
github.com/ouka-tenshi/DeviceInfoSample
・ViewController.h
#import <UIKit/UIKit.h> @interface ViewController : UIViewController @property (nonatomic, retain)UILabel *output; @end
・ViewController.m
#import "ViewController.h"
@interfaceViewController ()
@end
@implementation ViewController
@synthesize output;
- (void)viewDidLoad
{
[superviewDidLoad];
output = [[UILabelalloc]initWithFrame:CGRectMake(10.0, 10.0, self.view.frame.size.height, self.view.frame.size.width)];
output.numberOfLines = 20;
output.textColor = [UIColorblackColor];
output.font = [UIFontfontWithName:@"AppleGothic"size:16];
output.textAlignment = UITextAlignmentLeft;
NSString *str = @"[端末の名称/OSの情報など]\r\n";
UIDevice *dev = [UIDevicecurrentDevice];
str = [str stringByAppendingFormat:@"name -%@\r\n" , dev.name ];
str = [str stringByAppendingFormat:@"model -%@\r\n", dev.model ];
str = [str stringByAppendingFormat:@"localizedModel -%@\r\n" , dev.localizedModel ];
str = [str stringByAppendingFormat:@"systemName -%@\r\n", dev.systemName ];
str = [str stringByAppendingFormat:@"systemVersion -%@\r\n", dev.systemVersion ];
str = [str stringByAppendingFormat:@"[バッテリーの情報]\r\n"];
dev.batteryMonitoringEnabled = YES;
str = [str stringByAppendingFormat:@"batteryLevel -%f\r\n", dev.batteryLevel ];
if( UIDeviceBatteryStateUnknown == dev.batteryState ){
str = [str stringByAppendingFormat:@"batteryLevel 不明\r\n" ];
}elseif( UIDeviceBatteryStateUnplugged == dev.batteryState ){
str = [str stringByAppendingFormat:@"batteryLevel 充電してない\r\n" ];
}elseif( UIDeviceBatteryStateCharging == dev.batteryState ){
str = [str stringByAppendingFormat:@"batteryLevel 充電なう\r\n" ];
}elseif( UIDeviceBatteryStateFull == dev.batteryState ){
str = [str stringByAppendingFormat:@"batteryLevel フル充電\r\n" ];
}
output.text = str;
[self.viewaddSubview:output];
}
-(void)viewDidAppear:(BOOL)animated{
UIDevice *dev = [UIDevicecurrentDevice];
NSString *str = output.text;
str = [str stringByAppendingFormat:@"[端末の向き]\r\n"];
if( UIDeviceOrientationPortrait == [dev orientation]){
str = [str stringByAppendingFormat:@"Portrait\r\n"];
}elseif( UIDeviceOrientationPortraitUpsideDown == [dev orientation] ){
str = [str stringByAppendingFormat:@"PortraitUpsideDown\r\n"];
}elseif( UIDeviceOrientationLandscapeLeft == [dev orientation] ||
UIDeviceOrientationLandscapeRight == [dev orientation] ){
str = [str stringByAppendingFormat:@"Landscape\r\n"];
}
output.text = str;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)viewDidUnload {
[self setOutput:nil];
[super viewDidUnload];
}
@end
【実行結果】

