iOS锁屏踩坑记
来源:互联网 发布:2014年中国进出口数据 编辑:程序博客网 时间:2024/06/08 14:17
本文首发在我的个人博客:http://blog.shenyuanluo.com/,喜欢的朋友欢迎订阅。
最近公司有个项目需要对锁屏进行监控以便进行一些操作,然后在更新新版本的时候,审核竟然被拒绝了。原因竟然是调用了 Apple 不允许使用的 锁屏API ,如下方法一;后来改成方法二,终于审核通过了。
锁屏监听
方法一:
导入头文件和宏定义
// 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.mstatic 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 审核会被拒绝!
方法二:
实现
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’sapplicationProtectedDataDidBecomeAvailable:
method.
参考资料
- 官方文档
- Unsupported operation - Apps are not allowed to listen to device lock notifications
- iOS锁屏踩坑记
- iOS
- iOS
- IOS
- iOS
- iOS
- IOS
- ios
- iOS
- iOS
- IOS
- iOS
- ios
- ios ~~~~~
- ios
- IOS
- IOS
- IOS
- JS数组
- NGUI之scroll view制作,以及踩的坑总结
- 关键字final
- java中静态方法中调用非静态方法
- Java *.properties 用法
- iOS锁屏踩坑记
- Data mining基础之认识数据(2)
- 深入理解计算机操作系统(2.2.7)
- Hibernate--增删改查
- 用idea运行SSM时无法绑定Mapper.xml( Invalid bound statement (not found))
- 练习16
- oracal存储过程与存储函数
- 【C++】POJ 刷题指南
- 三角剖分算法