起動時にデバイスの向きを取得する

画面の向きによって挙動を変える時、willRotateToInterfaceOrientationを実装してやることで画面の向きの変更に対応する事ができます。

ただ、このメソッドは画面の向きが変わった時にだけ呼ばれるのでホームボタンが下の状態で起動すると呼ばれません。

そこで起動時に画面の方向を取ろうと

UIInterfaceOrientation orientation = [[UIDevice currentDevice] orientation];

を呼んでみると、UIDeviceOrientationUnknownが返ってきてしまいました。
縦でも横でもUIDeviceOrientationUnknown。

困ったのでぐーぐる先生に聞いてみたところ、
http://stackoverflow.com/questions/1941482/uideviceorientationunknown-in-landscape-mode
こんなの出てきました。

UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];

これを使えば良かったんですね。
無事に画面の向きを取れました。

stackoverflowは便利ですね。