Global Touch Event in ios - ios

my question is related to touch event which is Global in app
This is my scenario,
I Have an App with 15 screens, When first screen launches the timer starts, User goes from one screen to another screen randomly, In that case i need to reset the timer, If timer reaches to (say 120 seconds ) i have to destroy the current user session and logout n starts from first screen.
The implementation code for startLogoutTimer resetLogoutTimer and stopLogoutTimer is in AppDelegate class, if User navigates to any screen and touches the screen, i need to execute the methods of startLogoutTimer, resetLogoutTimer and stopLogoutTimer based on action but without message call to AppDelegate from every class (Screen/ViewController).
Thank You.

My above Problem is Solved.
I added the below code into AppDelegate class
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(resetLogoutTimer) name:UITouchPhaseBegan object:nil];
Now where ever I touch on screen, my timer restarts.

Create category for UIView and handle touches in it.
#implementation UIView (Touch)
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
[super touchesEnded:touches withEvent:event];
[((Appdelegate*)[UIApplication sharedApplication].delegate) resetLogoutTimer];
}
Or you cand do it the same with swizzling

Hi below two links solved my problem
iOS perform action after period of inactivity (no user interaction)
Timing Out An Application Due To Inactivity

Related

Overriding sendEvent in custom UIApplication to detect hardware keyboard event

I am developing an iPad app that needs to read input from a hardware keyboard. A primary user will be touching the screen normally while another user controls certain aspects of the app from a nearby Bluetooth keyboard that is paired with the iPad.
Overriding the keyCommands property in UIResponder has worked perfectly until now. But when we moved the app to Cocos2d (which uses its own responder chain) all the keyCommands stuff stopped working.
I tried subclassing UIApplication with an overridden sendEvent: method, something as simple as this:
#import "MyUIApplication.h"
#implementation MyUIApplication // subclass of UIApplication
-(void)sendEvent:(UIEvent *)event {
[super sendEvent:event];
NSLog(#"Event detected");
}
As far as I can tell, this successfully detects all events except for hardware keyboard events, which appear to be totally ignored. Is there some way to detect these events without using keyCommands and UIKeyCommands?

iAd banner unpauses spritekit game

I am developing an iOS game using sprite kit with an iAd banner, and this iAd is causing the game to unpause itself, and by unpausing I mean SKActions are continuing, here are some details:
1) I have several objects in my game that have a sequence (in series not parallel) of SKActions.
2) clicking the 'pause game' button on my game's screen and 'resuming' work fine - everything is pauses as desired. NSLog statement in "Note" below not displayed.
3) I've coded the following notification which has an observer that calls a selector to pause the game (which gets called properly). NSLog statement in "Note" below not displayed.
-(BOOL)bannerViewActionShouldBegin:(ADBannerView *)banner willLeaveApplication:(BOOL)willLeave
[[NSNotificationCenter defaultCenter] postNotificationName:#"iAdsoPauseGame" object:self];
return YES;
}
4) iAd appears lalala... everything is paused as it should be. NSLog statement in "Note" below not displayed.
5) when I click the "x" in the iAd, the following occur in this order:
a) the SKAction that my object in game was running while I 'paused' the game by going to iAd goes to completion, and the NSLog statement (in Note below) IS listed.
b) around 0.5sec later, my ViewController's "viewDidAppear" is called
c) less than 1ms after 5b, my iAd's "bannerViewActionDidFinish" is called (I have a notification here that tells my game to pause ... problem is there is this 0.5sec downtime between me closing the iAd and this method being called...)
Note: In my game's main .m file, in the "update" method (called every frame), I have an NSLog statement (below), and this does display as soon as I click the 'x' in the iAd, but then stops (when 5b/5c are called).
if (NodeGamePlay.paused==FALSE) {
NSLog(#"NodeGamePlay.paused==FALSE");
}
So my question: How can I prevent the SKaction from continuing (step that occurs in 5a) when I exit the iAd? Thank you for your time and help - this has been bugging me for quite some time!
just try this one
- (BOOL)bannerViewActionShouldBegin:(ADBannerView *)banner willLeaveApplication:(BOOL)willLeave
{
skView.scene.paused = YES;
return true;
}

Resetting sprite node in background

I am having a simple game in which if user goes to background, the game upon starting has to hide the player sprite node.
My code is as follows:
- (void)didMoveToView:(SKView *)view
{
[super didMoveToView:view];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(appDidEnterBackground:) name:UIApplicationDidEnterBackgroundNotification object:nil];
}
- (void)appDidEnterBackground:(NSNotification *)notification
{
[self hidePlayerNode];
}
- (void)hidePlayerNode
{
[self.playerNode runAction:[SKAction fadeOutWithDuration:0.0]];
}
The problem is that when the game is launched from background, I can see the player node for a split second and then it disappears. I need to make the player node invisible, the moment it goes to background and user shouldn't see it when the app is relaunched.
I tried with UIApplicationWillResignActiveNotification but the result is the same. Also, I tried running the code inside the selector method by main thread, but same result again.
You are probably seeing the state of your app that was saved as you went into the background.
Apple docs: "When your applicationDidEnterBackground: method returns, the system takes a picture of your app’s user interface and uses the resulting image for transition animations. If any views in your interface contain sensitive information, you should hide or modify those views before the applicationDidEnterBackground: method returns."

Detecting touch activity in ios app

I need to store actual NSDate value in AppDelegate when I recognize that user used app.
But how to define that user uses app?
I tried -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event on my base UIViewController but this only detecting touches on non-clickable areas, so I don't capture it when user clicks button (or any clickable component actually).
By using app I mean pretty much any interaction with an app, like touching the screen when the app is active.
What would be the way of doing that?
You can do this from the AppDelegate. Whenever the app is launched, it will call applicationDidBecomeActive: And with this, you will get be able to tell the user launched the app, or brought it back up.
You can then log this however you want and use it however you want
UPDATE
If you specifically are looking to detect every time someone touches inside of your app, you may want to just make sure that every button click, table scroll, etc calls back to a logging class and does whatever you need. And you can still have that touchesBegan method to get all other touches
The answer is right here:
Listen to all touch events in an iOS app
I overwriten UIWindow class and captured every touch in app using - (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event

Termination of an app running in the background

I am currently developing an app that will need to terminate after running in the background for more than five minutes. In order to do this, I will have to have a timer running in the background after the the Home button has been pressed or in case of an interruptions such as an SMS or a telephone call, then, after five minutes the applicationWillTerminate method will be called. My first question is should I put the applicationWillTerminate in the applicationWillResignActive method or in the applicationDidEnterBackground method? My second question is since this is an app with more that one view, Should I write these things in the AppDelegate class or elsewhere? Thank you for your response.
1) You can't force your app to finish programatically.
2) You should never call these AppDelegate methods by yourself. They're meant to be called only by the system.
Reference: UIApplicationDelegate Protocol Reference.
This is pretty ghetto, but what you can do is make your app crash when you want it to exit, and it will close automatically, granted that's not closing the app, but there's no real harm in it as long as you are in control of how it crashes try to go for a bad access error, aka trying to access something that has been deallocated
as for running a timer in the background, i don't know per say if you can do that, but as an alternative you can save the time when they leave the app aka the app goes into the background and then you can have all the events return to your app of the view controller that is first responder, and each UIEvent has a time stamp, and regardless of which event it is you can compare the time stamps and see if it's greater than 5 minutes
Regardless i don't suggest any of the above, but that is the best answer i can come up with for your question
the code for receiving events out side of your app
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
will start the event tracking and the call back is:
- (void)remoteControlReceivedWithEvent:(UIEvent *)event { }
but you have to remember to
[self becomeFirstResponder];
this tells the device which view controller to go to for the event tracking, oh and don't forget to resign first responder, and endReceivingRemotecontrolEvents

Resources