I have a clock program that needs to stay alive all night. I use the idletimer disabled function, but getting a text, missed call, or other notification overrides the idletimer setting back to enabled and allows the phone to sleep. I am thinking that I either need to disable the popups somehow while my app is running, or do something else to keep the phone awake (and running my app) through the popups.
Ideas?
Developing with Iphone 3GS and 3.1.2
How about a timer that periodically disables the idletimer?
You can't do this, at least not with public APIs for App Store inclusion. An incoming phone call will terminate you app and you cannot prevent this.
If you aren't worried about App Store rules, then write a daemon and run your app as a background task.
Although a good idea, the timer does not work. As long as the text message popup is not dismissed manually the phone will go to sleep no matter what idleTimerDisabled state is. Does this make sense, and is there a way to prevent sleep even when a popup is on the screen.
Related
I am building a word game and I want to hide the board when application is suspended?
the code looks fine however it givs a strange behaviour!!,
when I suspend the app nothing will happen but when i resume the application then the board will hide!!
local onSystem = function( event )
if event.type == "applicationSuspend" then
print("suspend")
board_group.alpha = 0
end
end
Runtime:addEventListener( "system", onSystem )
Note: you might wonder how do I know how the application looks when suspended?
the answer is: by pressing the home button twice.
example
SpellTower in normal state
https://dzwonsemrish7.cloudfront.net/items/430k0c0b0y0b413d0b42/Image%202012.11.12%208:08:24%20AM.png?v=4822f549
SpellTower after pressing the home button twice
https://dzwonsemrish7.cloudfront.net/items/280a1y0r2U3W321y1B2z/Image%202012.11.12%208:08:31%20AM.png?v=09c37567
you can see how they are hiding the letters, this is exactly what I want to do for my game, the only difference is i am using Corona SDK
When you do board_group.alpha = 0 you only has set a variable, the result will only take effect after a screen update.
But since the application is suspended... it won't update! So, changing any graphics on applicationSuspend don't work.
I believe the reason is because the application is not considered as suspended. In normal objective c programming it means that applicationWillResignActive is called when the user double clicks on the home button. So what you want to do is to add that code for this part.
Here is a flow of events:
http://www.cocoanetics.com/2010/07/understanding-ios-4-backgrounding-and-delegate-messaging/
Corona seems to have these events:
"applicationStart" occurs when the application is launched and all code
in main.lua is executed.
"applicationExit" occurs when the user quits the application.
"applicationSuspend" occurs when the device needs to suspend the application such as during a phone call or if the phone goes to sleep
from inactivity. In the simulator, this corresponds to the simulator
running in the background. During suspension, no events (not even
enterFrame events) are sent to the application while suspended, so if
you have code that depends on time, you should account for the time
lost to an application being suspended.
"applicationResume" occurs when the application resumes after a suspend. On the phone, this occurs if the application was suspended
because of a phone call. On the simulator, this occurs when the simulator was in the background and now is the foreground application.
So my guess is that you have to implement it outside of the corona API.
According to the corona documents you can implement them in the delegate:
You can intercept UIApplicationDelegate events via your implementation
of the CoronaDelegate protocol.
This protocol conforms to the UIApplicationDelegate protocol. Corona's
internal delegate will call your protocol's method if it is
implemented.
Please keep in mind the following:
Methods that Apple has deprecated will be ignored.
In most cases, your class' version will be invoked after Corona's corresponding version of the UIApplicationDelegate method. There is one situation in which your version will be called before.
In situations where the app is about to suspend or go to the background, your method will be called before Corona's version, e.g.
applicationWillResignActive: and applicationDidEnterBackground:.
http://docs.coronalabs.com/native/enterprise/ios/CoronaDelegate.html
But this is just a guess. Hope it helps!
Edit:
I was thinking, something really simple you could do is catch it outside and present a "pause" screen, then just hide it when the application enters foreground.
So if you can't do that (for now), one other option is to save application state when the application is about to terminate, and then set UIApplicationExitsOnSuspend = true in your plist file. This will cause the application to exit instead of suspending, which will avoid any screenshots, effectively "hiding" the board, etc. The downfall is, the app will have to read the session state when it launches again... this is only useful if your application can be designed to actually exit without losing your state, and is quite honestly, a little extreme. That said, it may be the only way to effectively do what you're trying to do.
Other ideas would be to see if you can add a large black layer to the screen, even though the application is suspending; perhaps this will somehow trigger an internal screen update by natively setting setNeedsDisplay. Also, instead of modifying the alpha, you might consider temporarily removing all of your layers and see if that has a similar effect.
I have an app in Xcode which handles sensitive data. I would like to terminate the app / exit forcefully if there is inactivity for 5 mins. Can someone please tell me how to do this?
Thank you!
Click here for a tutorial on how to make a timer. Every action that the user takes, reset the timer. When the 5 minutes are up, you can use exit(0).
However, this method of programatically closing your app is discouraged by Apple, so use it at your own discretion.
Edit: In order to stop the timer, you need to store a pointer to the timer that you create, and then call:
[pointerToTimer invalidate];
pointerToTimer = nil;
Edit 2: An alternative to using exit(0) would be to make a almost blank screen except for some text that states:
You have been inactive for too long. Please exit and restart this application.
Make this screen appear once the timer gets to 5 minutes. Therefore, the user can't do anything on the app but look at the screen, or exit the app.
If you're writing an app to submit to the app store, you can't (according to the guidelines). See details in this note: http://developer.apple.com/library/ios/ipad/#qa/qa1561/_index.html
If you don't care about the store or interface guidelines, it suggests that exit() is available.
I have an app that imports data on start using Core Data. In this part of the app, I have disabled resuming (multitasking) by calling exit(0) in applicationDidEnterBackground when a flag is set. E.g.:
-(void)applicationDidEnterBackground:(UIApplication *)application {
if (allowResuming==NO){
NSLog(#"Terminating...");
exit(0);
}
}
If I then attempt to 'resume' the app (by pressing its icon) within 11 seconds of having pressed the Home button (and, therefore, exit(0) having been called), the app crashes. This doesn't happen when running through the debugger, but the crash logs suggest that it looks like the app is trying to resume the data import where it left off, which, of course, is not what I want.
Attempting to 'resume' the app again straight after this crash (i.e. within a second) is successful.
If I attempt to 'resume' the app after 11 seconds, it's fine.
I would be really grateful if anyone has any ideas and/or can point me in the right direction here.
I don't think you should be doing exit(0). To disable Multitasking, set the key UIApplicationExitsOnSuspend key in Info.plist to YES. For details, see the section Opting Out of Background Execution at
http://developer.apple.com/library/ios/#documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/CoreApplication/CoreApplication.html#//apple_ref/doc/uid/TP40007072-CH3-SW1.
To discard the partial work done when the app leaves foreground, add a listener for UIApplicationWillResignActiveNotification or UIApplicationDidEnterBackgroundNotification. In the handler method, you could do something like-
[self discardPartialWorkDone];
HTH,
Akshay
You should not use exit(0) like Akshay said. In addition to that Apple will probably reject your App, because Apps which consist of code which interrupt the App like this wont be approved.
My App needs a Internet Connection which is checkt in the ViewDidLoad if there is no Internet connection I want to terminate the app when the Home Button is clickt so that the app start in its initial state next time but only in this case.
If there is a Internet connection from the start the homeButton should bring the app just to the background.
Apple does strongly discourages quitting from application programmatically.
I think you can handle your case without quitting application - when application goes to background(in applicationDidEnterBackground method of application delegate) save some flag indicating that you want to reinit it on resume, then when application comes back to foreground (applicationWillEnterForeground method in delegate) apply your initialization logic in case flag is set.
Programatically terminate an App is a behaviour that will be rejected to be published in the AppStore, as it seems that the application crashed.
If you don't mind that your application will never see the light at the AppStore, you can simply use exit(0).
When your app does not find an internet connection, switch to a view that exactly imitates your Default start-up image, then force that view to stay visible the usual amount of start-up time after any call to making your app active. You can also kill and recreate fresh all your other MVC objects during this time.
That way, no one will know that your app wasn't freshly started when brought back from the background.
When my iPhone app resigns active status, the sharedAccelerometer singleton instance of UIAccelerometer stops sending accelerometer:didAccelerate: messages to my delegate.
Is it at all possible to continue receiving those messages, similarly to how the CLLocationManagerDelegate continues to receive updates when the app is inactive?
I would prefer not to have to disable the idleTimer altogether if it can be helped.
No, not in the most recent available form of the iPhone SDK at least.
I would say that it is possible since this app is on the AppStore:
However, since you move differently in
bed during the different phases, the
Sleep Cycle alarm clock is able to use
the accelerometer in your iPhone to
monitor your movement and determine
which sleep phase you are in.
I can't believe it has to run all night with the screen turned on. EDIT: looks like it has to! :P
So they haven't got around that issue either. More chances that you can't achieve it then...
Have you read the documentation of the UIAccelerometer, does it mention anything there? If it just stops sending acceleration events there is not much you can do.
I would however try to set the accelerometer delegate again to my class when my app has lost focus (when the phone got locked). You can get that notification (lost focus notification) from the UIApplication. Try that just in case the accelerometer's delegate gets set to nil when the app loses focus.