Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

how to check if the iOS device is locked/unlocked using Swift?

how can I detect a locked/unlocked iOS device using Swift ( like the SCRENON / SCREENOFF in Android )

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I create same think using following.

You need to use the bridge for objective c code use into swift.

Here is the Link for create the bridge between the Objective c to Swift.

Once Completed then you can add following .h file into yourproject-Bridging-Header. file add the yourcontroller.h

Then add NotificationCenter.framework into your project.

Into your CustomObject.m

#import "notify.h"

-(void)registerAppforDetectLockState {

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

        NSLog(@"com.apple.springboard.lockstate = %llu", state);
        UILocalNotification *notification = [[UILocalNotification alloc]init];
        notification.repeatInterval = NSCalendarUnitDay;
        [notification setAlertBody:@"Hello world!! I come becoz you lock/unlock your device :)"];
        notification.alertAction = @"View";
        notification.alertAction = @"Yes";
        [notification setFireDate:[NSDate dateWithTimeIntervalSinceNow:1]];
        notification.soundName = UILocalNotificationDefaultSoundName;
        [notification setTimeZone:[NSTimeZone  defaultTimeZone]];

        [[UIApplication sharedApplication] presentLocalNotificationNow:notification];

    });
}

Then CustomObject.h

-(void)registerAppforDetectLockState;

Now into swift code you can directly use this method.

var instanceOfCustomObject: LockViewController = LockViewController()
instanceOfCustomObject.registerAppforDetectLockState();

May this helps lot.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...