1

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.

Community
  • 1
  • 1
Mahi
  • 11
  • 2
  • 1
    Uhhh... you say it's a Swift project, although you tagged Objective-C and your posted code is entirely in Objective-C? – DDPWNAGE May 07 '15 at 01:56
  • 1
    Wouldn't it be a lot easier to simply listen for the "application became active" and "application resigned active" notifications? – rmaddy May 07 '15 at 02:02
  • @rmaddy... I am planning to track the location even when the application is in the background, so to my knowledge "application became active" and "application resigned active" will work only if the app is coming into foreground or entering background, please correct me if I am wrong. – Mahi May 08 '15 at 14:15
  • @DDPWNAGE please see the edit in the post. – Mahi May 08 '15 at 14:31
  • @Mahi alright. I didn't see the edit until just now. – DDPWNAGE May 09 '15 at 01:15

0 Answers0