2013-10-25 2 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]를 사용하는 올바른 방법이라고 생각하지 않는다.사과 라이브러리 문서에 따르면,이 앱에서 화면 밝기를 설정하는 것일뿐 현재 필요한 화면 밝기 수준이 아닙니다. – Perisheroy

0

그것은 스위프트에서 작동하지 않습니다, 당신은

1.create A를 따라대로 스위프트에 작동하도록 일부 수정을 할 필요가 다음과 같이 LockNotifierCallback.m이라는 objectiveC 파일을 만듭니다.

,

아니라 머리를 만들 : # import에

@interface LockNotifierCallback : NSObject 


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


@end 

2.bridge을 신속

3.add 기능이 파일에 APPdelegate.swift :

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

을 PS : UIApplicationState을 여기서는 완벽하게 작동하지 않습니다.