如何接收iOS屏幕锁定状态

时间:2015-05-07 01:54:22

标签: ios objective-c

我只是学习编码的菜鸟,所以请耐心等待。

我正在尝试实施位置跟踪应用程序,这是我的问题。我想在iPhone锁定或进入睡眠模式时停止GPS更新。

我使用this讨论作为参考点。这是我到目前为止所做的。

LockNotifierCallback.h

    #import <Foundation/Foundation.h>
@import CoreFoundation;

@interface LockNotifierCallback : NSObject

+ (void(*)(CFNotificationCenterRef center, void *observer, CFStringRef name, const void *object, CFDictionaryRef userInfo))notifierProc;

- (void)registerForDeviceLockNotifications;

@end

LockNotifierCallback.m

#import "LockNotifierCallback.h"
@import CoreFoundation;

static void lockcompleteChanged(CFNotificationCenterRef center, void *observer, CFStringRef name, const void *object, CFDictionaryRef userInfo) {
    CFStringRef nameCFString = (CFStringRef)name;
    NSString *notificationName =  (__bridge NSString*)nameCFString;
    NSLog(@"Darwin notification NAME = %@",name);

    NSString *timerStatus = @"No Change";
    if ([notificationName isEqualToString:@"com.apple.springboard.lockcomplete"]) {
        timerStatus = @"Yes";
    } else if ([notificationName isEqualToString:@"com.apple.springboard.lockstate"]) {
       timerStatus = @"No";
    }

    NSLog(@"success");
}

@implementation LockNotifierCallback;

+ (void(*)(CFNotificationCenterRef center, void *observer, CFStringRef name, const void *object, CFDictionaryRef userInfo))notifierProc {
    return lockcompleteChanged;
}

- (void)registerForDeviceLockNotifications;
{
    NSLog(@"registering for device lock notifications");

    CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), //center
                                    NULL, // observer
                                    lockcompleteChanged, // callback
                                    CFSTR("com.apple.springboard.lockcomplete"), // event name
                                    NULL, // object
                                    CFNotificationSuspensionBehaviorDeliverImmediately);

    CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), //center
                                    NULL, // observer
                                    lockcompleteChanged, // callback
                                    CFSTR("com.apple.springboard.lockstate"), // event name
                                    NULL, // object
                                    CFNotificationSuspensionBehaviorDeliverImmediately);
}


@end

Xcode编译时没有任何错误但我不认为它正在工作,因为当我在模拟器中运行应用程序时,我在控制台中看不到任何日志。

它是一个SWIFT项目,我有#import&#34; LockNotifierCallback.h&#34;在我的桥头。

请详细回答这个问题,因为我还在学习编码。

感谢您的帮助。

修改

我正在使用Objective C仅接收锁定状态(设备睡眠/唤醒)的Darwin通知以优化电池寿命,因为我的应用程序将在后台运行,除此之外我计划在SWIFT中实现位置跟踪和其他功能。

0 个答案:

没有答案