2013-10-25 14 views
11

applicationDidEnterBackgroundで何かする必要があります。しかし、私はどのユーザーアクションが「背景を入力」するのかを区別する必要があります:スクリーンロックまたはホームボタンを押します。私はこのpost - How to differentiate between screen lock and home button press on iOS5?からであるこのコードを、使用していたiOS7の画面ロックとホームボタンの押下を区別する

UIApplicationState state = [application applicationState]; 
if (state == UIApplicationStateInactive) { 
    NSLog(@"Sent to background by locking screen"); 
} else if (state == UIApplicationStateBackground) { 
    NSLog(@"Sent to background by home button/switching to other app"); 
} 

それはiOS6に正常に動作します。 iOS7(デバイスとシミュレーターの両方)では、ユーザーが自宅またはロックボタンをクリックしても、常にUIApplicationStateBackgroundが表示されます。

誰かがこれを引き起こす原因について考えていますか? iOS 7のマルチタスクバックグラウンド処理の更新または私のアプリのいくつかの設定(私のアプリのバックグラウンドモードはオフです)?

代替ソリューションがありますか?

+0

[画面の位置を区別する方法kとiOS5のホームボタンを押しますか?](http://stackoverflow.com/questions/8303703/how-to-differentiate-between-screen-lock-and-home-button-press-on-ios5) – jmort253

+0

私はそれをはっきりと述べていませんでした。私はあなたのリンクの投稿を読んだが、それはiOS7ではこれ以上働かない。私はそれが重複しているとは思わない。しかし、とにかく、私はそれを明確にするために私の質問を編集する。 – Perisheroy

+0

明確にすることをお勧めします。また、編集した投稿があなたの投稿を先頭に戻し、他の人がもう一度それを見るようにします。 :D – jmort253

答えて

17

これは、iOS6 & iOS7 :)でお手伝いできます。

ユーザーがロックボタンを押すと、com.apple.springboard.lockcompleteという通知が表示されます。

//new way 
//put this in - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), 
            NULL, 
            displayStatusChanged, 
            CFSTR("com.apple.springboard.lockcomplete"), 
            NULL, 
            CFNotificationSuspensionBehaviorDeliverImmediately); 

//put this function in AppDelegate 
static void displayStatusChanged(CFNotificationCenterRef center, 
           void *observer, 
           CFStringRef name, 
           const void *object, 
           CFDictionaryRef userInfo) { 
    if (name == CFSTR("com.apple.springboard.lockcomplete")) { 
     [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"kDisplayStatusLocked"]; 
     [[NSUserDefaults standardUserDefaults] synchronize]; 
    } 
} 

//put this in onAppEnterBackground 
UIApplicationState state = [[UIApplication sharedApplication] applicationState]; 
    if (state == UIApplicationStateInactive) { 
     NSLog(@"Sent to background by locking screen"); 
    } else if (state == UIApplicationStateBackground) { 
     if (![[NSUserDefaults standardUserDefaults] boolForKey:@"kDisplayStatusLocked"]) { 
      NSLog(@"Sent to background by home button/switching to other app"); 
     } else { 
      NSLog(@"Sent to background by locking screen"); 
     } 
    } 

//put this in - (void)applicationWillEnterForeground:(UIApplication *)application 
[[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"kDisplayStatusLocked"]; 
[[NSUserDefaults standardUserDefaults] synchronize]; 

CGFloat screenBrightness = [[UIScreen mainScreen] brightness]; 

NSLog(@"Screen brightness: %f", screenBrightness); 

UIApplicationState state = [[UIApplication sharedApplication] applicationState]; 

if (state == UIApplicationStateInactive) { 

    NSLog(@"Sent to background by locking screen"); 

} else if (state == UIApplicationStateBackground) { 
    if (screenBrightness > 0.0) { 
     NSLog(@"Sent to background by home button/switching to other app"); 
    } else { 
     NSLog(@"Sent to background by locking screen"); 
    } 
} 

+2

重複した質問が見つかった場合、正しい行動は、15人の担当者がいれば、その投稿を重複としてフラグを立てることです。単にサイトの周りに同じコピー/ペーストの回答を投稿することは、私たちがしていることではなく、ノイズを発生させます。 – jmort253

+0

@ jmort253申し訳ありませんが、私はこのようにしなければならないのか分かりません。私は新しいユーザーですが、私の答えは正しいと思います。 – wqq

+0

私のために働いていない。 iOS 7(シミュレータとデバイス)でテストしてください。私はこれが[[UIScreen mainScreen] brightness]を使う正しい方法だとは思わない。Appleのライブラリのドキュメントによると、このアプリの画面の明るさを設定するだけで、現在の画面の明るさのレベルは必要ありません。 – Perisheroy

0

それはスウィフトでは動作しません、あなたはそれが続く

1.create Aとしてスウィフトに動作させるためにいくつかの変更を行う必要があります次のようにLockNotifierCallback.mという名前のobjectiveCファイルを作成します。

static void displayStatusChanged(CFNotificationCenterRef center, 
           void *observer, 
           CFStringRef name, 
           const void *object, 
           CFDictionaryRef userInfo) { 
    if ([(__bridge NSString *)name isEqual: @"com.apple.springboard.lockcomplete"]) { 
     NSLog(@"Screen Locked"); 
     [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"kDisplayStatusLocked"]; 
     [[NSUserDefaults standardUserDefaults] synchronize]; 
    } 
} 

@implementation LockNotifierCallback 

+ (void(*)(CFNotificationCenterRef center, void *observer, CFStringRef name, const void *object, CFDictionaryRef userInfo))notifierProc { 
return displayStatusChanged; 
} 

@end 

にも頭を作成します。

CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), nil, LockNotifierCallback.notifierProc(), "com.apple.springboard.lockcomplete", nil, CFNotificationSuspensionBehavior.DeliverImmediately) 

PS:UIApplicationState APPdelegate.swiftに迅速

3.add機能へ の#import

@interface LockNotifierCallback : NSObject 


+ (void(*)(CFNotificationCenterRef center, void *observer, CFStringRef name, const void *object, CFDictionaryRef userInfo))notifierProc; 


@end 

2.bridgeこのファイルをここでは完全には機能しません

関連する問題