iOS使用锁屏监听的私有API被拒 com.apple.springboard.lockcomplete

简介: 最近公司有个项目需要对锁屏进行监控以便进行一些操作,然后在更新新版本的时候,审核竟然被拒绝了。原因竟然是调用了 Apple 不允许使用的 锁屏API ,如下方法一;后来改成方法二,终于审核通过了。

最近公司有个项目需要对锁屏进行监控以便进行一些操作,然后在更新新版本的时候,审核竟然被拒绝了。原因竟然是调用了 Apple 不允许使用的 锁屏API ,如下方法一;后来改成方法二,终于审核通过了。

如果不确定是哪个第三方使用了,这个可以cd 到项目根目录:

grep -R 'com.apple.springboard.lockcomplete' *

他会检索出来,哪使用了

锁屏监听

  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"
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
      • 17
      • 18
    • 定义监听锁屏函数

      //  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])
          {
              // 发送锁屏通知
              [[NSNotificationCenter defaultCenter] postNotificationName:LOCK_SCREEN_NOTIFY
                                                                  object:nil];
              NSLog(@"Lock screen.");
          }
          else
          {
              // 此处监听到屏幕解锁事件(锁屏也会掉用此处一次,所有锁屏事件要在上面实现)
              NSLog(@"Lock state changed.");
          }
      }
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
      • 17
      • 18
      • 19
      • 20
      • 21
      • 22
    • 添加监听函数

      - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
      {
         CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(),
                                          NULL,
                                          screenLockStateChanged,
                                          NotificationLock,
                                          NULL,
                                          CFNotificationSuspensionBehaviorDeliverImmediately);
          CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(),
                                          NULL,
                                          screenLockStateChanged,
                                          NotificationChange,
                                          NULL,
                                          CFNotificationSuspensionBehaviorDeliverImmediately);
      
      }
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
    • 注意:该方法已被 Apple 禁止使用,上传的 App 审核会被拒绝! 
  2. 方法二: 

    • 实现 applicationProtectedDataWillBecomeUnavailable: 方法监听锁屏

    //  AppDelegate.m
    
    
    #define LOCK_SCREEN_NOTIFY @"LockScreenNotify"
    
    
    - (void)applicationProtectedDataWillBecomeUnavailable:(UIApplication *)application
    {
        [[NSNotificationCenter defaultCenter] postNotificationName:LOCK_SCREEN_NOTIFY
                                                            object:nil];
        NSLog(@"Lock screen.");
    }
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
  3. 实现 applicationProtectedDataDidBecomeAvailable: 方法监听解锁

    //  AppDelegate.m
    
    
    #define UN_LOCK_SCREEN_NOTIFY @"UnLockScreenNotify"
    
    
    - (void) applicationProtectedDataDidBecomeAvailable:(UIApplication *)application
    {
        [[NSNotificationCenter defaultCenter] postNotificationName:UN_LOCK_SCREEN_NOTIFY
                                                            object:nil];
        NSLog(@"UnLock screen.");
    }
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
  4. 官网 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.





目录
相关文章
|
12月前
|
API
26JavaWeb基础 - 监听器API
26JavaWeb基础 - 监听器API
40 0
|
文字识别 API iOS开发
iOS小技能:iOS13 证件扫描 & 文字识别API
1. 应用场景:证件扫描、文字识别 2. 原理:利用iOS13 VNDocumentCameraViewController的证件扫描和VNRecognizeTextRequest文字识别功能进行实现
375 0
iOS小技能:iOS13 证件扫描 & 文字识别API
|
3月前
|
机器学习/深度学习 API iOS开发
探索iOS开发中的SwiftUI框架深入理解RESTful API设计原则与最佳实践
【7月更文挑战第30天】本文深入探讨了SwiftUI框架在iOS开发中的应用,分析了其对用户界面构建的简化方法及性能优化。通过比较传统UI构建方式与SwiftUI的差异,揭示了SwiftUI如何提高开发效率和用户体验。文章还讨论了SwiftUI在实际项目中的集成策略,并展望了其未来的发展方向。 【7月更文挑战第30天】在数字时代的浪潮中,RESTful API如同一座桥梁,连接着不同的软件系统。本文将探讨RESTful API的核心设计原则,揭示其背后的哲学思想,并通过实例分析展示如何将这些原则应用于实际开发中。我们将从资源定位、接口一致性到HTTP方法的恰当使用,逐一剖析,旨在为开发者提供
53 1
|
5天前
|
JavaScript API
|
5月前
|
缓存 JSON API
IOS网络编程:什么是 RESTful API?如何使用 RESTful 风格设计 API?
IOS网络编程:什么是 RESTful API?如何使用 RESTful 风格设计 API?
138 3
|
5月前
|
API
使用commons-io-2.0 API来实现监听文件变化
使用commons-io-2.0 API来实现监听文件变化
230 0
15-iOS之Runtime常用API以及使用
15-iOS之Runtime常用API以及使用
86 0
|
JavaScript 前端开发 API
Vue3中监听数据变化,以及composition Api
Vue3中监听数据变化,以及composition Api
532 0
|
小程序 API Android开发
小程序获取WIFI的API(IOS conncetWifi()自动跳转设置页)
小程序获取WIFI的API(IOS conncetWifi()自动跳转设置页)
390 0
|
Java API 数据安全/隐私保护
翻阅必备,一看必知——Java -GUI界面设计(容器,布局,监听,事件 API大全
翻阅必备,一看必知——Java -GUI界面设计(容器,布局,监听,事件 API大全
223 0