2013-10-25 14 views
11

applicationDidEnterBackground içinde bir şeyler yapmam gerekiyor. Ancak, hangi kullanıcı eyleminin "arka plana girmesine" neden olduğunu ayırt etmem gerekiyor: ekran kilidi veya ana sayfa düğmesine basın. Bu post - How to differentiate between screen lock and home button press on iOS5? dan bu kodu, kullanıyordumEkran kilidi ve ev düğmesi arasında ayrım yapma iOS7'ye basın

:

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"); 
} 

O iOS6 üzerinde çalışıyor. ancak iOS7'de (hem cihaz hem de simülatör), kullanıcının eve veya kilit düğmesine basıp tıklamadığı her zaman UIApplicationStateBackground'u alırım.

Birinin buna neyin neden olabileceği hakkında bir fikri var mı? Çok görevli arka plan işlemede iOS 7 güncellemesi var mı? Ya da benim uygulamamın bir kısmı (uygulamamın arka planı modu kapalı) mı?

Alternatif bir çözüm var mı?

+0

'in olası bir kopyası çalışmıyor [Ekran yeri arasında ayrım nasıl yapılır? iOS5'de k ve ana ekran düğmesine basın mı?] (http://stackoverflow.com/questions/8303703/how-to-differentiate-between-screen-lock-and-home-button-press-on-ios5) – jmort253

+0

Sanırım yeterince açıklanmadı. Bağlantınızdaki gönderiyi okudum, ancak bu iOS7'de artık çalışmıyor. Ben bir kopya olduğunu sanmıyorum. Ama neyse, açıklamak için sorumu düzenliyorum. – Perisheroy

+0

Netleştirmek için iyi bir fikirdir, ayrıca düzenlemek, postanızı en üste geri döndürür, böylece başkaları tekrar görür. : D – jmort253

cevap

17

Bu, hem iOS6'da & iOS7 :) konusunda yardımcı olabilir.

Kullanıcı kilitleme düğmesine bastığınızda, bir com.apple.springboard.lockcomplete bildirimi alırsınız.

//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

Yinelenen bir soru bulursanız, 15 adımdan sonra doğru eylem, postayı kopya olarak işaretlemektir. Sadece sitenin etrafında aynı kopyala/yapıştır cevabını yayınlamak, bizim ne yaptığımız değil ve bu da gürültü yaratıyor. – jmort253

+0

@ jmort253 Üzgünüm, böyle yapmam gerektiğini bilmiyorum. Ben yeni kullanıcıyım ama cevabım haklı. – wqq

+0

benim için çalışmıyor. iOS 7'de test edin (simülatör ve cihaz). Bunun [[UIScreen mainScreen] parlaklığını kullanmanın doğru yolu olduğunu düşünmüyorum).Elma kütüphanesi belgelerine göre, bu uygulamada gerekli ekran parlaklık seviyesi değil, bu uygulamada ekran parlaklığı için bir ayardır. – Perisheroy

0

O Swift çalışmaz, sen

1.create a izleyin gibi Swift içinde çalışması için bazı değişiklik yapmak gerekir aşağıdaki gibi LockNotifierCallback.m adlı objectC dosyası şöyledir:

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 

yanı kafa oluşturun: alma

@interface LockNotifierCallback : NSObject 


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


@end 

2.bridge hızlı

3.Add işlevine bu dosyayı APPdelegate.swift için:

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

PS: UIApplicationState

İlgili konular