ios - How to listen to lock / unlock phone event while application is running in the background? -


i have app monitor when phone becomes locked , unlocked, when turns blank (after longer inactivity), while app not focused, running in background.

i can receive lock/unlock/blank events ease while app focused:

-(void) startlisteningforphonelockevent {     nslog(@"start listening lock/unlock , screen-goes-black events.");      cfnotificationcenteraddobserver(cfnotificationcentergetdarwinnotifycenter(),                                 (void*)self,                                 lockstatechanged,                                 cfstr("com.apple.springboard.lockstate"),                                 null,                                 cfnotificationsuspensionbehaviordeliverimmediately);      cfnotificationcenteraddobserver(cfnotificationcentergetdarwinnotifycenter(),                                 (void*)self,                                 hasblankedscreen,                                 cfstr("com.apple.springboard.hasblankedscreen"),                                 null,                                 cfnotificationsuspensionbehaviordeliverimmediately); } 

and callback functions:

static void lockstatechanged( cfnotificationcenterref center, void*observer, cfstringref name, const void *object, cfdictionaryref userinfo ) {     nslog(@"lock event received!"); }  static void hasblankedscreen( cfnotificationcenterref center, void*observer, cfstringref name, const void *object, cfdictionaryref userinfo ) {     nslog(@"blanked screen event received!"); } 

i've enabled background mode:

  • background fetch.

however, once app goes background, not receive lock/unlock/blank screen events.

i've tried other background modes, such sound playback, location updates etc. app still not receiving lock/unlock/blank screen events when in background.

i'm not sure if possible, or if doing wrong.

i'm testing on real device updated ios9, using latest xcode ios9 sdk.

even though app configured run in background, won't run if has no work done. run in background location updates, follow these instructions ricky:

  1. i restart location manager every 1 minute in function didupdatelocations.
  2. i allow location manager locations device 10 seconds before shut down (to save battery).

partial code below:

-(void)locationmanager:(cllocationmanager *)manager didupdatelocations:(nsarray *)locations{  for(int i=0;i<locations.count;i++){     cllocation * newlocation = [locations objectatindex:i];     cllocationcoordinate2d thelocation = newlocation.coordinate;     cllocationaccuracy theaccuracy = newlocation.horizontalaccuracy;     nstimeinterval locationage = -[newlocation.timestamp timeintervalsincenow];      if (locationage > 30.0)         continue;      //select valid location , location accuracy     if(newlocation!=nil&&theaccuracy>0        &&theaccuracy<2000        &&(!(thelocation.latitude==0.0&&thelocation.longitude==0.0))){         self.mylastlocation = thelocation;         self.mylastlocationaccuracy= theaccuracy;         nsmutabledictionary * dict = [[nsmutabledictionary alloc]init];         [dict setobject:[nsnumber numberwithfloat:thelocation.latitude] forkey:@"latitude"];         [dict setobject:[nsnumber numberwithfloat:thelocation.longitude] forkey:@"longitude"];         [dict setobject:[nsnumber numberwithfloat:theaccuracy] forkey:@"theaccuracy"];         //add vallid location accuracy array         //every 1 minute, select best location based on accuracy , send server         [self.sharemodel.mylocationarray addobject:dict];     } }  //if timer still valid, return (will not run code below) if (self.sharemodel.timer)     return;  self.sharemodel.bgtask = [backgroundtaskmanager sharedbackgroundtaskmanager]; [self.sharemodel.bgtask beginnewbackgroundtask];  //restart locationmaanger after 1 minute self.sharemodel.timer = [nstimer scheduledtimerwithtimeinterval:60 target:self                                                       selector:@selector(restartlocationupdates)                                                        userinfo:nil                                                         repeats:no];  //will stop locationmanager after 10 seconds, can accurate locations //the location manager operate 10 seconds save battery nstimer * delay10seconds; delay10seconds = [nstimer scheduledtimerwithtimeinterval:10 target:self                                                 selector:@selector(stoplocationdelayby10seconds)                                                 userinfo:nil                                                  repeats:no];  } 

i able use code in github project referenced in there listen lock , unlock events on device running ios 9, using latest xcode. here logs:

2015-12-18 13:31:44.777 location[16185:3796448] startlocationtracking 2015-12-18 13:31:44.780 location[16185:3796448] authorizationstatus authorized 2015-12-18 13:31:44.788 location[16185:3796448] start listening lock/unlock , screen-goes-black events. 2015-12-18 13:31:44.834 location[16185:3796448] locationmanager didupdatelocations 2015-12-18 13:31:44.837 location[16185:3796448] started master task 1 2015-12-18 13:31:45.197 location[16185:3796448] locationmanager didupdatelocations 2015-12-18 13:31:48.079 location[16185:3796448] blanked screen event received! 2015-12-18 13:31:48.215 location[16185:3796448] lock event received!

Comments

  1. i have tried above code but it only get executed when user opens the app again . it is like it gives the notification like your app just came into for-ground . it dose not call screen lock/unlock events . is there any way possible to monitor the device lock screen ? i mean when user enters wrong password then our app should react or enter into some notification or method , while it is in background state . is that functionality is possible to implement in iOS ? in android it is possible .

    ReplyDelete

Post a Comment

Popular posts from this blog

java - Date formats difference between yyyy-MM-dd'T'HH:mm:ss and yyyy-MM-dd'T'HH:mm:ssXXX -

c# - Get rid of xmlns attribute when adding node to existing xml -