美文网首页
锁屏监听

锁屏监听

作者: 北方的冬天008 | 来源:发表于2018-01-23 14:49 被阅读0次

方法一:

导入头文件和宏定义

//  AppDelegate.m#import #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.mstaticvoidscreenLockStateChanged(CFNotificationCenterRef center,void*observer,                                  CFStringRef name,constvoid*object,                                  CFDictionaryRef userInfo){NSString*lockstate = (__bridgeNSString*)name;if([lockstate isEqualToString:(__bridgeNSString*)NotificationLock])    {// 发送锁屏通知[[NSNotificationCenterdefaultCenter] 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 审核会被拒绝! 

方法二: 

实现 applicationProtectedDataWillBecomeUnavailable: 方法监听锁屏

//  AppDelegate.m#define LOCK_SCREEN_NOTIFY @"LockScreenNotify"- (void)applicationProtectedDataWillBecomeUnavailable:(UIApplication*)application{    [[NSNotificationCenterdefaultCenter] postNotificationName:LOCK_SCREEN_NOTIFY                                                        object:nil];NSLog(@"Lock screen.");}

1

2

3

4

5

6

7

8

9

10

11

12

实现 applicationProtectedDataDidBecomeAvailable: 方法监听解锁

//  AppDelegate.m#define UN_LOCK_SCREEN_NOTIFY @"UnLockScreenNotify"- (void) applicationProtectedDataDidBecomeAvailable:(UIApplication*)application{    [[NSNotificationCenterdefaultCenter] postNotificationName:UN_LOCK_SCREEN_NOTIFY                                                        object:nil];NSLog(@"UnLock screen.");}

1

2

3

4

5

6

7

8

9

10

11

12

官网 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.

相关文章

  • Android 手势密码锁的使用细说

    监听屏幕锁屏和解锁事件 启动时进行检测 基类中全局进行监听 构造广播监听锁屏截屏事件 应用Application里...

  • 锁屏监听

    方法一: 导入头文件和宏定义 // AppDelegate.m#import #define Notificati...

  • 监听开屏、锁屏广播问题

    最近做项目,需要监听开屏、锁屏(待机)的广播,发现这里还是有一些坑的。开屏、锁屏需要监听的广播分别是:ACTION...

  • Android自定义锁屏

    一、使用场景 某些场景,需要监听用户的锁屏事件,再次打开锁屏之后显示自己的锁屏页面,这个锁屏页面有可能在做一些计时...

  • IOS 监听锁屏

    小编最近在开发一项功能,需要监听APP进入后台的时候通知用户回到APP,但是锁屏的时候不用发通知,所以要区分用户点...

  • iOS 锁屏和息屏监听

    锁屏和息屏监听 一些说明如下方法是监听 传感器(红外感应) YES开启 NO关闭[[UIDevice curr...

  • Android 锁屏&解锁&开屏监听

    必须动态注册才可以监听到 必须动态注册才可以监听到 必须动态注册才可以监听到 锁屏广播Action 解锁广播Act...

  • iOS开发——锁屏监听

    第一步:AppDelegate.m 头部导入#import#define NotificationLock CFS...

  • applicationProtectedDataWillBeco

    在做iOS监听开屏锁屏时,网上查到的资料说,苹果推荐使用applicationProtectedDataWillB...

  • H5监听浏览器被切换到后台或者手机锁屏

    1.visibilitychange可以监听手机锁屏和浏览器切换到后台的动作。

网友评论

      本文标题:锁屏监听

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