How can I tell when the device screen is locked? - ios

Is there anyway I can know if a user has locked the screen or not? I have two AVPlayers playing music, but I only want one of them to be able to play in the background. If I hooked up the lock screen to call a method, I could stop the AVPlayer before it plays in the background. Any direction would be much appreciated.

When the device is locked, -(void)applicationWillResignActive is called
followed by -(void)applicationDidEnterBackground.
Write your code in the second method.

When the screen is being locked, applicationDidEnterBackground will be called.
You can do your work there :)

So I tried both suggestions of -(void)applicationWillResignActive and -(void)applicationDidEnterBackground, and it may work for you but for some reason it didn't even trigger my NSLogs in the console when i locked the screen. What would Trigger however is if I used NSNotifcationCenter.
[[NSNotificationCenter defaultCenter] addObserver: self
selector: #selector(handleEnteredBackground)
name: UIApplicationDidEnterBackgroundNotification
object: nil];
and then put the code I needed to run inside the selector.
-(void)handleEnteredBackground{
NSLog(#"This Shows inside the Console!");
}
I don't know if this is the best way to handle things, but it works for me, and might work for anyone else with the same issue, it is triggered by BOTH the Lock Screen Button and the Home Button on my iphone currently running iOS 7.0.4 which is just what I needed.

Related

Why is my iOS app getting launched automatically?

I have an audio app that plays when being launched. It has CarPlay entitlements, plays from the network and naturally plays in the background.
Now, every one in a while the phone launchs the app in the background without notice. Music simply starts to play randomly. I can't determine the cause or the situation in which this reproduces. It might be related to Bluetooth connections, phone calls, CarPlay interactions, network changes or other reasons.
I do know the following things for sure:
The app is launched from scratch as I have examined remote logs and seen the app is being launched.
Pausing the music from control center will kill the app, and it will be relaunching after 2-3 minutes again (and again)
There are not too many clues about your issue.
But I would try to find some possible causes base on my experiences.
First of all, check the notifications/listeners that you use in the app.
Something would be like bellow:
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(reachabilityChanged:) name:kReachabilityChangedNotification object:nil];
for this one, once the network status changed, you would get the notification, and you might do something after that(play the music).
Second, check out if there is Timer in the app or not, it would cause problem if
the timer was set improperly and you do something by the timer.

Application functionality changes when run on device

I've found that my application stops its timer when the device goes to the lock screen on actual device, but it works perfectly on the simulator.
Is that because I haven't released the app, so an actual device won't let me keep working on the lock screen? Or is it a bug in my code?
I've written an NSLog() statement to monitor my application's timer action, it keeps working when the simulator goes to the lock screen,
but on an actual device it stops.
I'm so frustrated because I don't know why there are these differences between the device and the simulator, and I don't know how to solve it.
What you need to do is implement applicationWillEnterForeground and applicationDidEnterBackground to persist the state of your timer, and resume it whenever the app opens again.
You may also want to use UILocalNotification to schedule a notification when your timer finishes.
Here is another post about using UILocalNotification with timers that you may find useful.

UILocalNotification not firing properly on iPhone when app not foregrounded

UPDATE - a warning to anyone with similar symptoms... check Do Not Disturb is not active!
I finally paid to get an app I was working on running on my iPhone instead of on the simulator.
Having done so I've spotted a strange behaviour with regards to the UILocalNotifications that I create, which are set to use the default sound UILocalNotificationDefaultSoundName, and the default timezone ([NSTimeZone defaultTimeZone]).
If the app is foregrounded then the local notification fires on time and I receive it within
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
and I create an alert view to display it.
If however I lock my phone just before a notification is expected then nothing appears on the screen, but when I press the home button to show the lock screen I can see my app's notification in the notification centre on the lock screen. No sound was played, but then again the phone didn't wake up to show the notification properly.
If however I just press the home button so the app is put in the background leaving me looking at the springboard apps then I don't see any notifications whatsoever, and again no sound is heard.
On the simulator the same code correctly fires the notification banner when looking at the app icons in the spring board, although I've never been able to hear any sounds for the notification. But as the simulator doesn't let you configure the notification centre for apps I always presumed it was perhaps set to not allow sounds. On my device sounds for this apps notifications are enabled, and i've tried it with banner and alert modes, with no joy. Very frustrating.
Has anyone experienced this, or have an idea what could be going on?
Oh the irony of this answer.. think it's time to go to bed. My iPhone thinks that too for nearly 3 hours now... My question happily describes Do Not Disturbs behaviour, which was active.. Sorry to waste everyones time!!

Safely Pause/Resume Remote UIWebView WebSockets when the app enters Background/Active states

My app is primarily a full screen UIWebView. It does not currently have any method implemented to handle pausing/resuming the WebSockets activity inside the UIWebView.
In theory my app could stop the WebView from doing everything, and simply refresh the page? I don't know if that's the best method though, which is why I'm here.
The app crashes whenever I press the physical Home button, wait a while, and attempt to resume the app by pressing the App Icon and going into it. The app will usually just crash and resume back to the Home Screen again. When I tap the App Icon again the app starts over fresh like it was just opened for the first time after startup of the device.
During the crash I see things in the debug information like...
Thread 7 WebThread: EXC_BAD_ACCESS (code=1, address=0x5)
void std::_1::_push_heap_front(WebCore::TimerHeapIterator,
WebCore::TimerHeapIterator, WebCore::TimerHeapLessThanFunction&,
std::__1::iterator_traits::difference_type)
+ 194
I'm not sure what to do, I obviously have nothing setup in my app delegate for "applicationDidEnterBackground" or "applicationDidBecomeActive" or any of the other states, as I'm not sure what is the best method to somehow "pause" the content and "resume" it, or even HOW to do that.
You can stop it in JS.
From ObjC, listen to the app state change event (UIApplicationWillResignActiveNotification or UIApplicationDidEnterBackgroundNotification).
Inside the callback, you'll call stringByEvaluatingJavaScriptFromString to stop your websockets.
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(willResignActive) name:UIApplicationWillResignActiveNotification object:nil];
-(void)willResignActive
{
[m_webView stringByEvaluatingJavaScriptFromString:#"stopWS();"]; // stopWS is implemented in JS
}

How to create a custom alarm wake up screen on iOS?

Is it possible to create a custom alarm wake up screen on iOS via Xcode, such as the carrot alarm app on the App Store? If so, could anyone point to some documentation or examples?
I'm pretty confused at the moment, because I don't understand how it can be done as long as the iPhone locks itself and the app stops working. This is my main concern, because locking the iPhone kills the app.
I would like to what's the best way to create a wake up screen like carrot app's one, avoiding all the screen lock problems. How did they do it?
Thanks
Using the code below you can disable the lock screen. You can place it in your viewDidLoad method.
[[UIApplication sharedApplication] setIdleTimerDisabled: YES];
As for creating the screen, I'd suggest picking up a book on iPhone development as it's a lot to explain in a follow-up message.

Resources