2016-10-21 5 views

Antwort

0

Sie können diesen Code versuchen, um den Status des Bildschirms in ios zu erhalten.

CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), //center 
           NULL, // observer 
           displayStatusChanged, // callback 
           CFSTR("com.apple.iokit.hid.displayStatus"), // event name 
           NULL, // object 
           CFNotificationSuspensionBehaviorDeliverImmediately); 
+0

Sorry, dass ich diesen Teil des Codes nicht sehr verstehe. Würdest du mehr erklären? Ich bin neu in diesem Bereich. Ich werde das schnell schreiben. Irgendwelche Ratschläge? – user6539552

+0

Ich habe auch versucht, den Code, der Fehler auf "CFSTR" und CFNotificationSuspensionBehaviorDeliver Sofort – user6539552

2

In Swift 3 können Sie tun:

override func viewDidLoad() { 
     super.viewDidLoad() 
     // Observer UIApplicationDidBecomeActive,UIApplicationDidEnterBackground 
     NotificationCenter.default.addObserver(
       self, 
       selector: #selector(MyViewController.applicationDidBecomeActive(notification:)), 
       name: NSNotification.Name.UIApplicationDidBecomeActive, 
       object: nil) 

     NotificationCenter.default.addObserver(
       self, 
       selector: #selector(MyViewController.applicationDidEnterBackground(notification:)), 
       name:NSNotification.Name.UIApplicationDidEnterBackground, 
       object: nil) 
} 

func applicationDidBecomeActive(notification: NSNotification) { 
    // here my app did become active 
} 
func applicationDidEnterBackground(notification: NSNotification) { 
    // here my app did enter background 
} 

Sie weitere Details in der official guide finden.


Einzelheiten von den tatsächlichen Quellen:

extension NSNotification.Name { 
    // These notifications are sent out after the equivalent delegate message is called 
    @available(iOS 4.0, *) 
    public static let UIApplicationDidEnterBackground: NSNotification.Name 
    @available(iOS 4.0, *) 
    public static let UIApplicationWillEnterForeground: NSNotification.Name 
    public static let UIApplicationDidFinishLaunching: NSNotification.Name 
    public static let UIApplicationDidBecomeActive: NSNotification.Name 
    public static let UIApplicationWillResignActive: NSNotification.Name 
    public static let UIApplicationDidReceiveMemoryWarning: NSNotification.Name 
    public static let UIApplicationWillTerminate: NSNotification.Name 
    public static let UIApplicationSignificantTimeChange: NSNotification.Name 
    public static let UIApplicationWillChangeStatusBarOrientation: NSNotification.Name // userInfo contains NSNumber with new orientation 
    public static let UIApplicationDidChangeStatusBarOrientation: NSNotification.Name // userInfo contains NSNumber with old orientation 
    // userInfo dictionary key for status bar orientation 
    public static let UIApplicationWillChangeStatusBarFrame: NSNotification.Name // userInfo contains NSValue with new frame 
    public static let UIApplicationDidChangeStatusBarFrame: NSNotification.Name // userInfo contains NSValue with old frame 
    // userInfo dictionary key for status bar frame 
    @available(iOS 7.0, *) 
    public static let UIApplicationBackgroundRefreshStatusDidChange: NSNotification.Name 
    @available(iOS 4.0, *) 
    public static let UIApplicationProtectedDataWillBecomeUnavailable: NSNotification.Name 
    @available(iOS 4.0, *) 
    public static let UIApplicationProtectedDataDidBecomeAvailable: NSNotification.Name 
    // Key in options dict passed to application:[will | did]FinishLaunchingWithOptions and info for UIApplicationDidFinishLaunchingNotification  
    // This notification is posted after the user takes a screenshot (for example by pressing both the home and lock screen buttons) 
    @available(iOS 7.0, *) 
    public static let UIApplicationUserDidTakeScreenshot: NSNotification.Name 
} 
+0

Vielen Dank. Scheint, dass dies nur den Status meiner App überprüfen kann. Was ich tun muss, ist den Bildschirmstatus zu überprüfen, der ein- oder ausgeschaltet ist – user6539552

+0

Irgendwelche Ratschläge dazu? – user6539552

+0

Es gibt keine öffentliche API zu tun, was Sie wollen: Warnung, wenn Sie eine private API verwenden, könnte Ihre App als nicht gültig von Apple Bewertung beurteilt werden, müssen Sie die offiziellen API-Richtlinien verwenden –

Verwandte Themen