Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iPhone Objective-c detect Screen Lock

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");
                                 }
                             }
                             );

}
like image 871
Issei Avatar asked Jun 06 '16 04:06

Issei


People also ask

Can I bypass the screen lock on iPhone?

Another way to bypass the passcode is using iTunes to bypass your iPhone passcode with the “Restore iPhone” feature or using another way to restore your iPhone. Learn more about 3 methods to restore your iPhone.

How do I lock my iPhone screen for my cat?

Question: Q: How to lock the screen on your iPhone To enable, go to Settings > General > Accessibility > Guided Access. Then once you need the function, triple tap the home button, set a code that will allow you to unlock the screen, choose the screen area that should be locked and enjoy!

How do you check phone is locked or unlocked in Swift?

DeviceStatus.swift import Foundation class DeviceStatus : NSObject { func locked(){ print("device locked") // Handle Device Locked events here. } func unlocked(){ print("device unlocked") //Handle Device Unlocked events here. } }


1 Answers

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.

like image 67
Pushkraj P. Lanjekar Avatar answered Oct 17 '22 00:10

Pushkraj P. Lanjekar