I am new to iOS development, I am using monotouch for developing the iOS apps, i want to know the time since the app was idle, i got the ObjC code but am unable to convert it into c#. Here is the code:
- (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");
}
Can anyone help me in converting this to c#.
There's certainly better way to achieve this, but let's do a almost line to line translation fo this
Obj-C
code toXamarin.iOS
C#
:sendEvent
is a method ofUIApplication
. It's very uncommon to subclass it, see Subclassing notes on UIApplication class referenceOnce you subclassed it, you have to instruct the runtime to use it, that's done in the
Main
method, normally found inMain.cs
. Here's what the modifiedMain.cs
looks like now.Notice the
Register
attribute on the class, used as second argument ofUIApplication.Main
.Now, let's translate your code for real:
I replaced
maxIdleTime
byTimeSpan.FromHours (1)
. Otherwise, you'll have the same behaviour as the Obj-C one, including bugs if any (it looks ok though).