iOS锁屏踩坑记

作者: shenyuanluo | 来源:发表于2017-08-13 23:51 被阅读457次

本文首发在我的个人博客:http://blog.shenyuanluo.com/,喜欢的朋友欢迎订阅。

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

锁屏监听

  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"
      
    • 定义监听锁屏函数

      //  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.");
          }
      }
      
    • 添加监听函数

      - (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 推荐使用的方法)

    • 实现 applicationProtectedDataWillBecomeUnavailable: 方法监听锁屏

      //  AppDelegate.m
      
      #define LOCK_SCREEN_NOTIFY @"LockScreenNotify"
      
      - (void)applicationProtectedDataWillBecomeUnavailable:(UIApplication *)application
      {
          [[NSNotificationCenter defaultCenter] postNotificationName:LOCK_SCREEN_NOTIFY
                                                              object:nil];
          NSLog(@"Lock screen.");
      }
      
    • 实现 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.");
      }
      
    • 官网 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.


参考资料

相关文章

  • iOS锁屏踩坑记

    本文首发在我的个人博客:http://blog.shenyuanluo.com/,喜欢的朋友欢迎订阅。 最近公司有...

  • 好文章整理

    1、iOS自动化打包上传的踩坑记http://www.cocoachina.com/ios/20160624/16...

  • IOS 踩坑记

    1. 标题栏 这句隐藏代码会对所有Controller 的标题栏产生影响,最好在调用以上隐藏代码时能够在cont...

  • iOS 锁屏问题

    iOS实现关闭/开启自动锁屏1 不自动锁屏[UIApplication sharedApplication]....

  • iOS中刘海屏幕适配

    iOS适配刘海屏,看我避免踩坑 终生程序员小松哥关注 0.9892018.12.31 17:03:52字数 1,0...

  • iOS 关于自动锁屏

    关于iOS 开发关闭自动锁屏

  • iOS和Android代码中实现禁止手机休眠

    (一)iOS平台默认,所有iOS设备在过了设定的休眠时间后,都会自动锁屏。如果你的应用不希望iOS设备自动锁屏,需...

  • Xcode10和iOS12踩坑

    Xcode10和iOS12踩坑

  • iOS10的适配

    每次出了新系统,必然要踩很多坑,这次来踩一踩iOS10的坑吧。 一、证书问题 直接选择Automatically ...

  • iOS 12 踩坑记

    1.常见的就不写了2.UIImage imageNamed: 读取不到图片 发生在第二次及以后的运行并且有个警告 ...

网友评论

    本文标题:iOS锁屏踩坑记

    本文链接:https://www.haomeiwen.com/subject/xtuerxtx.html