読者です 読者をやめる 読者になる 読者になる

ふるすたっくえんじにあっぽい人の日記

ASP .NET MVC (C#)、.NET Framework、iOS (Objective-c) アプリ、Androidアプリ (Java)、AWS、たまにLAMPとかプロジェクトマネジメントあたりのお話

【iOS/Objective-c】Method Swizzling 修正版

ちょっと前にこんなん書きましたシリーズ第二弾

【iOS/Objective-c】Method Swizzling - ふるすたっくえんじにあっぽい人の日記

いや、動くんだけどさ。
[[Hoge sharedInstance] applicationWillResignActive:application];
って呼ぶと、AppDelegate内で定義されているapplicationWillResignActive内の処理が実行されるんすけどね。こんなかでselfは当然AppDelegateのインスタンスは参照してないんすよね。Hogeインスタンス参照してるわ。そらそうだ。

で、以下のように修正

#pragma mark - swizzled AppDelegate Methods
- (void)applicationWillResignActive:(UIApplication *)application {
    [[Hoge sharedInstance] swizzleMethod:@selector(applicationWillResignActive:)];
    [self applicationWillResignActive:application];
    [[Hoge sharedInstance] swizzleMethod:@selector(applicationWillResignActive:)];
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    [[Hoge sharedInstance] swizzleMethod:@selector(applicationDidEnterBackground:)];
    [self applicationDidEnterBackground:application];
    [[Hoge sharedInstance] swizzleMethod:@selector(applicationDidEnterBackground:)];
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    [[Hoge sharedInstance] swizzleMethod:@selector(applicationWillEnterForeground:)];
    [self applicationWillEnterForeground:application];
    [[Hoge sharedInstance] swizzleMethod:@selector(applicationWillEnterForeground:)];
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    [[Hoge sharedInstance] swizzleMethod:@selector(applicationDidBecomeActive:)];
    [self applicationDidBecomeActive:application];
    [[Hoge sharedInstance] swizzleMethod:@selector(applicationDidBecomeActive:)];
}

- (void)applicationWillTerminate:(UIApplication *)application {
    [[Hoge sharedInstance] swizzleMethod:@selector(applicationWillTerminate:)];
    [self applicationWillTerminate:application];
    [[Hoge sharedInstance] swizzleMethod:@selector(applicationWillTerminate:)];
}

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
    [[Hoge sharedInstance] swizzleMethod:@selector(application:didReceiveRemoteNotification:)];
    [self application:application didReceiveRemoteNotification:userInfo];
    [[Hoge sharedInstance] swizzleMethod:@selector(application:didReceiveRemoteNotification:)];
}