vendredi 8 mai 2015

How to Recieve iOS Screen Lock Status

I am a noob just learning to code, so please bear with me.

I am trying to implement a location tracking application and here is my problem. I want to stop GPS updates when iPhone is locked or went into sleep mode.

I have used this discussion as reference point. This is what I have so far.

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 compiles this without any error but I don't think it is working as I don't see any logs in Console when I run the app in simulator.

Its a SWIFT project and I have #import "LockNotifierCallback.h" in my bridging header.

Please answer this in detail as I am still learning to code.

Appreciate your help.

EDIT

I am using Objective C only to receive Darwin notification for lockstate (device sleep / awake) to optimize battery life as my app will run in the background, apart from this I am planning to implement location tracking and other functions in SWIFT.

Aucun commentaire:

Enregistrer un commentaire