Application session timeOut in cocoa application - objective-c

Hi friends i'm developing MAC desktop application using cocoa. I want to add session time out for in the app. Example my application running in background user dont touch and do nothing in app. After 20(we need to set) app will return home page(login page) for session time out.
Will help me how to set session in cocoa application

Use a custom NSApplication class and override sendEvent:. Something like this:
- (void)sendEvent:(NSEvent *)event
{
[super sendEvent:event];
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:#selector(applicationSessionTimeout:) object:nil];
[self performSelector:#selector(applicationSessionTimeout:) withObject:self afterDelay:SESSION_TIMEOUT];
}
Basically all mouse and keyboard events enter your app through this method. You just need to override it to set your timers.

Related

applicationDidEnterBackground and applicationWillTerminate both called when user swipes up to quit app

I am adding some notifications for when the user sends the application to the background or when they completely quit the app. However when they quit the app, both the methods applicationDidEnterBackground and applicationWillTerminate are called. Why is this so and how can I just have applicationWillTerminate get called when the user quits the app?
This is objective-c for if anyone is wondering.
applicationWillTerminate method will be called only when application is not in suspended state while being terminated.
Following links may help you -
Which function is called when iPhone app is terminated?
applicationWillTerminate when is it called and when not
Seeing that applicationWillTerminate is called after applicationDidEnterBackground I have decided to set a notification.fireDate of 1sec from the point at which applicationDidEnterBackground is called. When applicationWillTerminate is called it first cancels the notification that was scheduled within applicationDidEnterBackground.
Pseudocode:
-applicationDidEnterBackground()
{
[self scheduleLocalNotificationAtTimeIntervalFromNow:1.0f identifier: #"someIdentifier"];
}
-applicationWillTerminate
{
[self cancelLocalNotificationWithIdentifier: #"someIdentifier"];
[self scheduleLocalNotificationAtTimeIntervalFromNow:0.0f identifier: #"irrelevantIdentifier"];
}

Load Title ViewController When applicationDidBecomeActive:

I've created an app that has two viewcontrollers. The app opens to a title screen (general UIViewController titled 'Title') with a segue connection to the second view that is a custom class (OSViewController titled 'MapView'). As it is, the app suspends when entered into the background state so it opens right where you left off which is typically in MapView.
I want to know what I need to do to have the app start at the title screen when it becomes active. Preferably, I'd like it to open to the title screen if it is inactive for more than 1 minute. From what I've been reading, it seems like I would make a call in applicationDidBecomeActive: method in my AppDelegate to code this in. Please provide me the code to put in the applicationDidBecomeActive: method (if that's the right place to put it) that will reopen my app to the title screen when transitioning from the inactive state to the active state. My app is almost finished but I'd like to fix this issue and I don't have a lot of experience dealing with app states. Thanks in advance for your time.
If you need more information just ask.
You can also register a class as an observer of the "didBecomeActive" notification. You should place this in the viewDidLoad or the init method of your class.
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(willBecomeActive:) name:UIApplicationDidBecomeActiveNotification object:nil];
In this case, willBecomeActive: is a method that you have defined in your class that get's called when the app becomes active again. That might look something like this:
- (void)willBecomeActive:(NSNotification *)notification {
if (self.navigationController.topViewController == self) {
[self.navigationController popToRootViewControllerAnimated:YES];
}
}
You'll also need to add this in your viewDidUnload method
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationDidBecomeActiveNotification object:nil];
EDIT:
Thanks #AMayes for the advice. I don't believe key/value observing is necessary in this instance.

UIImagePickerController and application background modes

I’m building an application which supports both video playback and recording (not simultaneously, it’s just two separate features it provides). In order to make the videos play after the app enters background and gets back, I had to add an App plays audio item to Required background modes in the plist (I’m using MPMoviePlayerController for playback).
This, however, causes a problem with my video recording (I’m using UIImagePickerController for it). Basically, even after the picker is dismissed (either by the Cancel button or when it’s finished picking media), the app still keeps the audio recording session running.
If I remove the App plays audio item from the plist, the ImagePickerController’s audio session stops misbehaving, but then I can’t resume the playback of MPMoviePlayerViewController on switching to app from background mode.
Is there a way I can customise the handling of the audio session so that both the MPMoviePlayerController and UIImagePickerController can work properly?
yes, there is a way you can customize the handling of the audio session for what you desire: don't try to set the App plays audio setting.
instead, in your AppDelegate code (which is usually in AppDelegate.m from the simple wizard projects provided), supply code in the applicationWillResignActive: method that will stop the playback in your MPMoviePlayerController, and then use applicationDidBecomeActive: to resume the playback at the point at which you paused it if so desired.
this will not only allow the video to be resumed after a temporary pause, but it will allow you to save the state so that the video can be resumed in case the app is removed from memory or in case the user causes it to be quit.
You can scratch the background modes and instead use notifications to pause/resume your player. See UIApplicationDidBecomeActiveNotification and UIApplicationWillResignActiveNotification in the UIApplication class reference.
You can snag some code and see this implemented in this class. Here is some of the relevant code from that class:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(_didBecomeActive:)
name:UIApplicationDidBecomeActiveNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(_willResignActive:)
name:UIApplicationWillResignActiveNotification
object:nil];
- (void) _didBecomeActive:(NSNotification*)notification {
if (_wasPlaying) {
[_movieController play];
}
}
- (void) _willResignActive:(NSNotification*)notification {
_wasPlaying = _movieController.currentPlaybackRate != 0.0;
if (_wasPlaying) {
[_movieController pause];
}
}

Stop CLLocationManager when user stops app using Fast App Switcher

I am developing an iOS Application which uses CLLocationManager to get GPS Data.
What's a proper way to stop CLLocationManager when the user presses the home button and then kills the app using fast app switcher?
How can I stop the CLLocationManager then to stop polling for GPS data? Does anybody knows a good way to achieve this, cause normally I can't execute any code when the user kills the application...
EDIT:
When I am sending the application to background, it should still get significantLocationChanges, so I can't stop CLLocationManager when the application is sent to the background!
In the AppDelegate there is a method called applicationWillResignActive. This method runs when your app is about to be sent to the background. If you have access to the CLLocationManager from your AppDelegate you can stop it right there. Otherwise you can post a Notification from applicationWillResignActive and whatever class has your CLLocationManager can subscribe to that Notification and handle it from there.
Post a Notification like this:
[[NSNotificationCenter defaultCenter] postNotificationName:#"appWillResignActive" object:nil];
And subscribe like this:
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(handleResign:) name:#"appWillResignActive" object:nil];
Then some method like this:
- (void)handleResign: (NSNotification *)notification
{
// stop CLLocationManager
}
EDIT:
If you want to run code when the app is being killed then you can run that code in this method in the AppDelegate:
- (void)applicationWillTerminate:(UIApplication *)application

How do I run modals windows from code with cocoa?

I`m trying to run a window as modal directly from the code.
My program starts and a main NSWindows is showed.A thread still running to see if the user has a valid distribution. if he doesn't I need to run a modal.I mean, I have no buttons clicked in the interface.I've designed a NSWindow on the interface builder for a password set, and I want to call it only when my validation is not successful.
I have tested and realized that these methods which are responsible for modal windows running only work in a IBAction environment.
//This doesn't work
-(void) showPasswordWindow
{
[NSApp runModalForWindow:[self window]];
}
//this works But its not useful for me =(
- (IBAction) passwordWindowButton:(id)sender
{
[NSApp runModalForWindow:[self window]];
}
Please, help this newbie =)
One thing to check: are you calling the method on the main thread?
to check, add this to showPasswordWindow
NSLog(#"Main thread? %d", [NSThread isMainThread]);