iPhone Objective-c detect Screen Lock
Asked Answered
C

2

5

I'm new to make iPhone App with Objective-c

I want to make the App which sends a notification when iPhone screen is locked(Pressed Lock button) How can I make this app?

I'm trying to make it using "applicationWillSuspend", but

/*----------------------------------------*/
- (void)applicationWillSuspend
{
     NSLog(@"WillSuspend");
}
/*----------------------------------------*/

This code doesn't work

I'm not sure when applicationWillSuspend is called

Please, give me some knowledge

#import "AppDelegate.h"
#import <notify.h>

@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    // iOS8 Notification permit
    if ([UIApplication
         instancesRespondToSelector:@selector(registerUserNotificationSettings:)]) {
        [[UIApplication sharedApplication]
         registerUserNotificationSettings:[UIUserNotificationSettings
                                           settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeSound
                                           categories:nil]];
    }
    return YES;

    int notify_token;
    notify_register_dispatch("com.apple.springboard.lockstate",
                             &notify_token,
                             dispatch_get_main_queue(),
                             ^(int token)
                             {
                                 uint64_t state = UINT64_MAX;
                                 notify_get_state(token, &state);
                                 if(state == 0) {
                                     NSLog(@"unlock device");
                                 } else {
                                     NSLog(@"lock device");
                                 }
                             }
                             );

}
Citric answered 6/6, 2016 at 4:59 Comment(4)
Use for it - (void)applicationDidEnterBackground:(UIApplication *)application delegate methodGyn
May be this is what you looking for:https://mcmap.net/q/415728/-lock-unlock-events-iphoneCupellation
refere following link , i think it will help you #7888990Horizon
@Citric kindly check my updated answer.Boots
P
7

Import this in app delegate #import <notify.h>

Write this piece of code in didFinishLaunchingWithOptions

int notify_token;
    notify_register_dispatch("com.apple.springboard.lockstate",
                         &notify_token,
                         dispatch_get_main_queue(),
                         ^(int token)
                         {
                             uint64_t state = UINT64_MAX;
                             notify_get_state(token, &state);
                             if(state == 0) {
                                 NSLog(@"unlock device");
                             } else {
                                 NSLog(@"lock device");
                             }
                         }
                         );

So once your iPhone gets locked, you will get "lock device" as log. So you can write your code in that block. This will help you.

Pessary answered 6/6, 2016 at 6:20 Comment(8)
I added my code to my question, but it doesn't work. Xcode says notify_token will never be executed. Could you check my code?Citric
@Issei, you need to define notify_token as ivar at AppDelegateEsbenshade
Where did you get notify.h ??Grounds
You do not need to import anything for notify.h, you can use it directly. More information is on developer.apple.com/library/content/documentation/Darwin/…Pessary
Thanks @Pushkraj for quick replying, now I have gone through many error by putting this peace of code in objective c file to use in swift. Can you share some more information to how to use it in a proper manner ?Grounds
Okay. Give me some time. I will edit my answer with latest OS compatibilityPessary
@Pushkraj Using this solution may get your app rejected as the notification key com.apple.springboard.lockstate is considered to be a private API. As described here you mustn't use this key. For more information regarding this hereNkvd
@pratik: Answer I posted was in 2016 and that time it was allowed. Now apple changed its guidelines massively.Pessary
B
3

You can't do that on the iPhone. But through, Darwin notifications. You can detect the event when the device is locked by "com.apple.springboard.lockcomplete".

Have a look at these links too hopefully it may help you:

1) Lock Unlock events iphone

2) How can I detect screen lock/unlock events on the iPhone?

applicationWillSuspend method doesn't exist natively, but in the AppDelegate.m you can play with applicationWillResignActive and applicationWillResignActive these methods will be called when the user hits the home button and the app will go to the background (here you can keep your connection live, but you should read the apple documentation regarding background tasks because your connection cannot be live forever if the app remains in the background. There are other ways to keep your app up to date like update on push notification etc):

- (void)applicationWillResignActive:(UIApplication *)application
{
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

and this method will be called when the app will get terminated (closed completely from multitasking).

- (void)applicationWillTerminate:(UIApplication *)application
{
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

You can handle your connections within these methods.

Boots answered 6/6, 2016 at 7:6 Comment(1)
note: applicationWillTerminate is called only if you explicitly enable it in PLIST: by default iOS (and OSX) simply sigkills apps (that is faster than passing back control to app; it simply throws away all your pages from memory)Cambell

© 2022 - 2024 — McMap. All rights reserved.