強火で進め

このブログではプログラム関連の記事を中心に書いてます。

UIApplicationのNotificationsの一覧

パラメータ 説明
UIApplicationDidBecomeActiveNotification アプリケーションがアクティブになった時に通知。アプリ起動時やロック解除時
UIApplicationDidChangeStatusBarFrameNotification ステータスバーのサイズ変更が発生した時に通知。デバイスが回転した時など
UIApplicationDidChangeStatusBarOrientationNotification バイスの向きが変わった後に通知
UIApplicationDidEnterBackgroundNotification アプリケーションがバックグラウンドに入る時に通知
UIApplicationDidFinishLaunchingNotification アプリケーションが起動した直後に通知
UIApplicationDidReceiveMemoryWarningNotification メモリ不足の警告発生時に通知
UIApplicationProtectedDataDidBecomeAvailable 保護されたファイル(※)へコードからアクセスが可能になった時に通知
UIApplicationProtectedDataWillBecomeUnavailable 保護されたファイルへコードからアクセス出来なくなる前に通知
UIApplicationSignificantTimeChangeNotification 時間に大きな変更が有った場合に通知。日付が変わった時や夏時間に変更など
UIApplicationWillChangeStatusBarOrientationNotification バイスの向きが変わる直前に通知
UIApplicationWillChangeStatusBarFrameNotification ステータスバーのサイズ変更される直前に通知
UIApplicationWillEnterForegroundNotification アプリケーションがアクティブになる直前に通知
UIApplicationWillResignActiveNotification アプリケーションがアクティブで無くなる直前に通知。通知アラートが表示される時などにも通知される
UIApplicationWillTerminateNotification アプリケーションが終了される直前に通知

※保護されたファイル(保護ファイル)については詳しくは「iOS App Programming Guid」の「Protecting Data Using On-Disk Encryption」の項目を参照

通知の受信を開始

基本的には以下のプログラムで通知を受ける事が出来ます。 selector: や name: の引数は実際に使用したいものに変更して下さい。

    [[NSNotificationCenter defaultCenter] addObserver:self
               selector:@selector(applicationDidBecomeActiveNotification:)
                   name:UIApplicationDidBecomeActiveNotification
                 object:nil];

通知を受信を停止

1つのみ削除する時。

    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name:UIApplicationDidBecomeActiveNotification
                                                  object:nil];

すべての通知の受信を停止する場合。

    [[NSNotificationCenter defaultCenter] removeObserver: self];

通知時にデータが含まれている場合の取得方法

UIApplicationDidChangeStatusBarFrameNotification/UIApplicationWillChangeStatusBarFrameNotification

実際の使用例。以下のStack Overflowのページからの引用。

iphone - How to position view below green bar during phone call? - Stack Overflow
http://stackoverflow.com/questions/3303997/how-to-position-view-below-green-bar-during-phone-call

- (void)viewDidLoad {
    [super viewDidLoad];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(statusBarFrameChanged:) name:UIApplicationDidChangeStatusBarFrameNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(statusBarFrameWillChange:) name:UIApplicationWillChangeStatusBarFrameNotification object:nil];
}

// これから変更される直前に通知
- (void)statusBarFrameWillChange:(NSNotification*)notification {
    NSValue* rectValue = [[notification userInfo] valueForKey:UIApplicationStatusBarFrameUserInfoKey];
    CGRect newFrame;
    [rectValue getValue:&newFrame];
    NSLog(@"statusBarFrameWillChange: newSize %f, %f", newFrame.size.width, newFrame.size.height);
    // Move your view here ...
}

// 変更後に通知
- (void)statusBarFrameChanged:(NSNotification*)notification {
    NSValue* rectValue = [[notification userInfo] valueForKey:UIApplicationStatusBarFrameUserInfoKey];
    CGRect oldFrame;
    [rectValue getValue:&oldFrame];
    NSLog(@"statusBarFrameChanged: oldSize %f, %f", oldFrame.size.width, oldFrame.size.height);
    // ... or here, whichever makes the most sense for your app.
}
name:UIApplicationDidChangeStatusBarOrientationNotification

以前の向きが通知される。

- (void)applicationDidChangeStatusBarOrientationNotification:(NSNotification*)notification {
    NSValue* value = [[notification userInfo] valueForKey:UIApplicationStatusBarOrientationUserInfoKey];
    NSInteger orientation;
    [value getValue:&orientation];
    switch (orientation) {
        case UIInterfaceOrientationPortrait:
            NSLog(@"UIInterfaceOrientationPortrait");
            break;
        case UIInterfaceOrientationPortraitUpsideDown:
            NSLog(@"UIInterfaceOrientationPortraitUpsideDown");
            break;
        case UIInterfaceOrientationLandscapeLeft:
            NSLog(@"UIInterfaceOrientationLandscapeLeft");
            break;
        case UIInterfaceOrientationLandscapeRight:
            NSLog(@"UIInterfaceOrientationLandscapeRight");
            break;
            
        default:
            break;
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(applicationDidChangeStatusBarOrientationNotification:)
                                                 name:UIApplicationDidChangeStatusBarOrientationNotification
                                               object:nil];
}