Is there any notification that indicates iOS will put the device into a sleep” state?
"com.apple.springboard.lockcomplete" is the notification that I used to fix my problem.
To receive "com.apple.springboard.lockcomplete" notification , I used CFNotificationCenterAddObserer in applicationDidFinishLaunching.(It is said to a private api~)
However, if my app is running in background, it cannot receive the notification. To fix this problem, I added UIBackgroundModes(= audio) to play mute audio consequently. At last, the app works well.
Thanks all for your help!
Get a callback when iOS device is picked up
iOS does not provide such a public API.
More generally, iOS won't provide any API that could let your app know about user actions on the system or on other apps due to the sandbox even more if your app is not active:
For security reasons, iOS places each app (including its preferences and data) in a sandbox at install time. A sandbox is a set of fine-grained controls that limit the app’s access to files, preferences, network resources, hardware, and so on. As part of the sandboxing process, the system installs each app in its own sandbox directory, which acts as the home for the app and its data.
A few exceptions to this rule exists, like Locations updates.
iPhone: Detecting user inactivity/idle time since last screen touch
Here's the answer I had been looking for:
Have your application delegate subclass UIApplication. In the implementation file, override the sendEvent: method like so:
- (void)sendEvent:(UIEvent *)event {
[super sendEvent:event];
// Only want to reset the timer on a Began touch or an Ended touch, to reduce the number of timer resets.
NSSet *allTouches = [event allTouches];
if ([allTouches count] > 0) {
// allTouches count only ever seems to be 1, so anyObject works here.
UITouchPhase phase = ((UITouch *)[allTouches anyObject]).phase;
if (phase == UITouchPhaseBegan || phase == UITouchPhaseEnded)
[self resetIdleTimer];
}
}
- (void)resetIdleTimer {
if (idleTimer) {
[idleTimer invalidate];
[idleTimer release];
}
idleTimer = [[NSTimer scheduledTimerWithTimeInterval:maxIdleTime target:self selector:@selector(idleTimerExceeded) userInfo:nil repeats:NO] retain];
}
- (void)idleTimerExceeded {
NSLog(@"idle time exceeded");
}
where maxIdleTime and idleTimer are instance variables.
In order for this to work, you also need to modify your main.m to tell UIApplicationMain to use your delegate class (in this example, AppDelegate) as the principal class:
int retVal = UIApplicationMain(argc, argv, @"AppDelegate", @"AppDelegate");
iOS device On/Off notification
I assume you're talking about notifications when device is turned on/off.
I was having the same problem and notifications were my first choice. But I found much simpler and more robust solution. You see, usually notifications are sent just one time. For example, if you're building a daemon that will listen to those notifications then you need to be sure that it will be running at the moment when device turn on notification is sent. That's the problem - you can't be sure and probably will miss notification that might help you. It just doesn't look that robust to me.
So obvious solution is to look at system uptime. You can obtain it with this [NSProcessInfo processInfo].systemUptime
. In my case I don't need to know right away when device is turned off. I periodically save [NSProcessInfo processInfo].systemUptime
value and current date and time at some file in device file system. When device is turned on and my daemon is launched I compare value from the file with current uptime. If current uptime is smaller then device was turned off and turned on. And from the date and time in the file I know when device was turned off.
Is there a way to check if the iOS device is locked/unlocked?
I solved it like this:
//call back
static void displayStatusChanged(CFNotificationCenterRef center, void *observer, CFStringRef name, const void *object, CFDictionaryRef userInfo)
{
// the "com.apple.springboard.lockcomplete" notification will always come after the "com.apple.springboard.lockstate" notification
CFStringRef nameCFString = (CFStringRef)name;
NSString *lockState = (NSString*)nameCFString;
NSLog(@"Darwin notification NAME = %@",name);
if([lockState isEqualToString:@"com.apple.springboard.lockcomplete"])
{
NSLog(@"DEVICE LOCKED");
//Logic to disable the GPS
}
else
{
NSLog(@"LOCK STATUS CHANGED");
//Logic to enable the GPS
}
}
-(void)registerforDeviceLockNotif
{
//Screen lock notifications
CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), //center
NULL, // observer
displayStatusChanged, // callback
CFSTR("com.apple.springboard.lockcomplete"), // event name
NULL, // object
CFNotificationSuspensionBehaviorDeliverImmediately);
CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), //center
NULL, // observer
displayStatusChanged, // callback
CFSTR("com.apple.springboard.lockstate"), // event name
NULL, // object
CFNotificationSuspensionBehaviorDeliverImmediately);
}
Note: the "com.apple.springboard.lockcomplete" notification will always come after the "com.apple.springboard.lockstate" notification
Update
The order of the two notifications can no longer be relied upon, as of recent versions of iOS
Related Topics
How to Remove Cache in Wkwebview
How to Check Internet Connection in Alamofire
Swift iOS Check If Remote Push Notifications Are Enabled in iOS9 and iOS10
How to Get Uiscrollview Vertical Direction in Swift
Prevent Dismissal of Modal View Controller in Swiftui
Uicollectionview Header Dynamic Height Using Auto Layout
Launch Image Not Showing Up in iOS Application (Using Images.Xcassets)
Nsdateformatter and Current Language in iOS11
How to Prevent Keyboard Push Up Webview at iOS App Using Phonegap
Xcode 4/iOS - Send an Email Using Smtp from Inside My App
Making a Phone Call in an iOS Application
Upload Image from iPhone to the Server Folder
How to Set a Uitableview to Grouped Style
How to Restrict Special Characters in Uitextfield Except Dot and Underscores
Black Screen After Adding Scenedelegate and Updating Info.Plist
This Certificate Has an Invalid Issuer:Keychain Marks All Certificates as "Invalid Issuer"
How Could I Get an Array of Emojis Found in iOS8.3
What Is "Error in _Connection_Block_Invoke_2: Connection Interrupted" in iOS