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

}
Issei
  • 51
  • 1
  • 4

2 Answers2

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.

Pushkraj Lanjekar
  • 2,254
  • 1
  • 21
  • 34
  • 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? – Issei Jun 07 '16 at 18:01
  • @Issei, you need to define `notify_token` as ivar at AppDelegate – malex Mar 06 '17 at 11:22
  • 1
    Where did you get `notify.h` ?? – Abhishek Mitra May 02 '18 at 13:25
  • You do not need to import anything for notify.h, you can use it directly. More information is on https://developer.apple.com/library/content/documentation/Darwin/Reference/usr_APIs/notify/index.html – Pushkraj Lanjekar May 02 '18 at 13:38
  • 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 ? – Abhishek Mitra May 02 '18 at 13:58
  • Okay. Give me some time. I will edit my answer with latest OS compatibility – Pushkraj Lanjekar May 02 '18 at 14:12
  • @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](https://developer.apple.com/forums/thread/69333?answerId=245623022#245623022) you mustn't use this key. For more information regarding this [here](https://developer.apple.com/forums/thread/69333?answerId=207516022#207516022) – Pratik Jamariya Dec 22 '20 at 06:13
  • @pratik: Answer I posted was in 2016 and that time it was allowed. Now apple changed its guidelines massively. – Pushkraj Lanjekar Dec 22 '20 at 06:16
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.

Community
  • 1
  • 1
Irfan
  • 4,301
  • 6
  • 29
  • 46
  • 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) – ingconti Dec 08 '16 at 08:42