iOS鎖屏踩坑記

本文首發(fā)在我的個人博客:http://blog.shenyuanluo.com/,喜歡的朋友歡迎訂閱。

最近公司有個項目需要對鎖屏進行監(jiān)控以便進行一些操作,然后在更新新版本的時候,審核竟然被拒絕了。原因竟然是調(diào)用了 Apple 不允許使用的 鎖屏API ,如下方法一;后來改成方法二,終于審核通過了。

鎖屏監(jiān)聽

  1. 方法一:(審核會被拒)

    • 導入頭文件和宏定義

      //  AppDelegate.m
      
      #import <notify.h>
      
      #define NotificationLock CFSTR("com.apple.springboard.lockcomplete")
      #define NotificationChange CFSTR("com.apple.springboard.lockstate")
      #define NotificationPwdUI CFSTR("com.apple.springboard.hasBlankedScreen")
      #define LOCK_SCREEN_NOTIFY @"LockScreenNotify"
      
    • 定義監(jiān)聽鎖屏函數(shù)

      //  AppDelegate.m
      
      static void screenLockStateChanged(CFNotificationCenterRef center,
                                         void *observer,
                                         CFStringRef name,
                                         const void *object,
                                         CFDictionaryRef userInfo)
      {
          NSString *lockstate = (__bridge NSString *)name;
          if ([lockstate isEqualToString:(__bridge NSString *)NotificationLock])
          {
              // 發(fā)送鎖屏通知
              [[NSNotificationCenter defaultCenter] postNotificationName:LOCK_SCREEN_NOTIFY
                                                                  object:nil];
              NSLog(@"Lock screen.");
          }
          else
          {
              // 此處監(jiān)聽到屏幕解鎖事件(鎖屏也會掉用此處一次,所有鎖屏事件要在上面實現(xiàn))
              NSLog(@"Lock state changed.");
          }
      }
      
    • 添加監(jiān)聽函數(shù)

      - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
      {
         CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(),
                                          NULL,
                                          screenLockStateChanged,
                                          NotificationLock,
                                          NULL,
                                          CFNotificationSuspensionBehaviorDeliverImmediately);
          CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(),
                                          NULL,
                                          screenLockStateChanged,
                                          NotificationChange,
                                          NULL,
                                          CFNotificationSuspensionBehaviorDeliverImmediately);
      
      }
      
    • 注意:該方法已被 Apple 禁止使用,上傳的 App 審核會被拒絕!

  2. 方法二:(Apple 推薦使用的方法)

    • 實現(xiàn) applicationProtectedDataWillBecomeUnavailable: 方法監(jiān)聽鎖屏

      //  AppDelegate.m
      
      #define LOCK_SCREEN_NOTIFY @"LockScreenNotify"
      
      - (void)applicationProtectedDataWillBecomeUnavailable:(UIApplication *)application
      {
          [[NSNotificationCenter defaultCenter] postNotificationName:LOCK_SCREEN_NOTIFY
                                                              object:nil];
          NSLog(@"Lock screen.");
      }
      
    • 實現(xiàn) applicationProtectedDataDidBecomeAvailable: 方法監(jiān)聽解鎖

      //  AppDelegate.m
      
      #define UN_LOCK_SCREEN_NOTIFY @"UnLockScreenNotify"
      
      - (void) applicationProtectedDataDidBecomeAvailable:(UIApplication *)application
      {
          [[NSNotificationCenter defaultCenter] postNotificationName:UN_LOCK_SCREEN_NOTIFY
                                                              object:nil];
          NSLog(@"UnLock screen.");
      }
      
    • 官網(wǎng) API 說明如下:

      When the user locks the device, the system calls the app delegate’s applicationProtectedDataWillBecomeUnavailable: method. Data protection prevents unauthorized access to files while the device is locked. If your app references a protected file, you must remove that file reference and release any objects associated with the file when this method is called. When the user subsequently unlocks the device, you can reestablish your references to the data in the app delegate’s applicationProtectedDataDidBecomeAvailable: method.


參考資料

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容