How can i terminate my application in iPhone depends on time - iphone

Iam displaying time in my application using NSTimer Class starting with 00:00.00 (mm:ss.SS) when my application launched. I want to terminate my application when time reaches to 75:00.00 (mm:ss.SS) in iPhone.

The below code snippet will do the work for you. But its not recommended to use the exit() method to quit the app as it will appear that the app has been crashed to the end user. I recommend you to show an alert after 75 mins and ask the user to quit the app.
[NSTimer scheduledTimerWithTimeInterval:270000
target:self
selector:#selector(quitTheApp)
userInfo:nil
repeats:NO];
- (void)quitTheApp
{
exit(0);
}

Once you start the timer,immediately next line call the below method:
[self performSelector:#selector(stopTimer) withObject:nil afterDelay:75.0];
And stopTimer method should be like this:
-(void) stopTimer
{
[timer invalidate];
timer = nil;
}

Related

show counter while application is in background

I'm making an application that listening on a UDP socket all the time, even if the application is in the background. when a certain data arrived throw the socket, I want to show the user timer that count 10 seconds from 10 to 0.
is it possible to show this counter while the application is in the background?
can I show the user any message in the background (except [[UIApplication sharedApplication] presentLocalNotificationNow:...] that not good for me), like apple alarm clock?
note
I don't need the application to be approved for app store. I just need it to work at home.
I already successfully keeping the communication in the background and I know that the application running
thanx
You could try using the badge icon, and updating the number from 10 to 0 every second. I'll assume you put these methods in the AppDelegate, but they could go anywhere. Try:
.h
#interface AppDelegate : NSObject <UIApplicationDelegate> {
int _countDownNumber;
NSTimer * _countDownTimer;
}
.m
- (void)updateTimer; {
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:_countDownNumber];
_countDownNumber--;
if(_countDownNumber < 0){
[_countDownTimer invalidate];
}
}
- (void)methodThatStartsTheCountDown; {
_countDownNumber = 10;
_countDownTimer = [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:#selector(updateTimer) userInfo:nil repeats:YES];
}
Hope That Helps!

Update tableview with regular time intervals in iphone

I have created a sample application. It has fetched data from server for every 1 sec and updated the result in UITableView. I had already done. But it crashes my app rapidly. What I do is call NSTimer every sec.
timer = [NSTimer scheduledTimerWithTimeInterval:1
target:self
selector:#selector(MainLoop)
userInfo:nil repeats:YES];
In that timer function i have called NSThread function.
-(void)MainLoop
{
if(isPreviousThreadFinished)
{
NSLog(#"Thread Opened");
isPreviousThreadFinished = NO;
[NSThread detachNewThreadSelector:#selector(MainLoopThread) toTarget:self withObject:nil];
}
}
-(void)MainLoopThread
{
MainLoopPool=[[NSAutoreleasePool alloc]init];
//Get data from the server
[self performSelectorOnMainThread:#selector(UpdateTable) withObject:nil waitUntilDone:YES];
[MainLoopPool release];
}
-(void)UpdateTable
{
[self.tableView reloadData];
isPreviousThreadFinished = YES;
NSLog(#"Thread closed");
}
It works fine. And reloads data correctly from the server. I stop NSTimer in viewWillDisappear method. When I go to previous page sometimes it crashes application. In console i have seen the error is given below,
bool _WebTryThreadLock(bool), 0xb2aa410: Tried to obtain the web lock from a thread other than the main thread or the web thread. This may be a result of calling to UIKit from a secondary thread. Crashing now...
What is wrong with my code? Crashes appear randomly.
When I work on my application I discovered very good thing - NSOperation. It's thread safe. You can simply add you operations to the queue without fear to crash. There are a lot of tutorials about NSOperation
http://www.icodeblog.com/2010/03/04/iphone-coding-turbo-charging-your-apps-with-nsoperation/
http://www.cimgf.com/2008/02/16/cocoa-tutorial-nsoperation-and-nsoperationqueue/
I strongly recommend you use NSOperation-s instead of threads in your project.

How to pause and resume NStimer [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
How can I programmatically pause an NSTimer?
I have a question. How can I pause a countdown using a timer? I am developing a game. In the game, I need to go to next view when the timer pauses, and after coming back I want to resume it.
I try this code in the view:
[mytimer pause];
// to resume
[mytimer resume];
I try that code, but I get a warning saying: "NSTimer may not respond to 'pause'"
I build with that warning and when I press the pause button, the app crashes.
NSTimer indeed doesn't have resume and pause methods so you can end up with a crash in runtime after such a warning. Generally you can create 2 kinds of timers (see NSTimer class reference) one that implements only once and the second, that repeats. Example:
This way you create a timer, that will enter callback myMethod each second.
NSTimer *myTimer = [NSTimer scheduledTimerWithTimeInterval:1
target:self
selector:#selector(myMethod:)
userInfo:nil
repeats:YES];
You probably will choose this one for your purpose where in your class you should maintain some
BOOL pausevariable and in the callback myMethod do the following:
- (void) myMethod:(NSTimer *) aTimer
{
if (!pause) {
// do something
// update your GUI
}
}
where you update pause accordingly somewhere in your code.
To stop the timer (and release it) call
[myTimer invalidate];
good luck
What you want, is what OpenGLES application brings up to you. You should create 2 methods like this:
- (void)startAnimation
{
self.animationTimer = [NSTimer scheduledTimerWithTimeInterval:animationInterval target:self selector:#selector(selector) userInfo:nil repeats:YES];
}
- (void)stopAnimation
{
[animationTimer invalidate];
animationTimer = nil;
}
It's something like this.
Refer to the NSTimer Class Reference, there is no pause method.

Using NSTimer to shutdown app

Bit of a noob here.
I am developing an app that plays some looped sounds. I would like to give the user the ability to shut down the app after a certain amount of time using a timer. The idea is the user presses a button and the app will shut down once the timer runs out.
At the moment if the button is pressed, the app crashes.
Here's what I got so far:
- (IBAction)timer:(id)sender{
timer = [NSTimer scheduledTimerWithInterval: 10.0 target:self selector:#selector(targetMethod:) userInfo:nil repeats: YES];
}
-(void) targetMethod: (NSTimer*) theTimer {
NSLog(#"timer?");
exit(0);
}
You need to properly define your timer reference:
NSTimer *timer = [NSTimer scheduledTimerWithInterval: 10.0 target:self selector:#selector(targetMethod:) userInfo:nil repeats: YES];

I should able to delay 2 seconds using NSTimer. How to do it?

I want to produce a delay of 2 seconds using NSTimer how to initialize timer in program?
Multiple options here.
If you just want a delay of 2 seconds you could use the sleep() function
#include<unistd.h>
...
sleep(2);
Or you may be able to use NSTimer like so
[NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:#selector(fireMethod) userInfo:nil repeats:NO];
And in your class you would have a method defined as
-(void)fireMethod
{
//Do stuff
}
Here you go...
[NSTimer scheduledTimerWithTimeInterval:2
target:self
selector:#selector(action)
userInfo:nil
repeats:NO];
Simple answer: [NSThread sleepForTimeInterval:10.0];
Note that you should not really be thinking about delays in an event driven UI/OS. You should thinking about tasks you want to do now, and tasks you want to do later, and code these subtasks and schedule them appropriately. e.g. instead of:
// code that will block the UI when done in the main thread
- (void) methodC {
doA();
delay(2);
doB();
}
you might want to have code that looks more like:
- (void) methodA {
doA();
return; // back to the run loop where other useful stuff might happen
}
- (void) methodB {
doB();
}
and you can then schedule methodB with an NSTimer at the end of methodA, an NSTimer started by whatever called methodA, or, the best option, by the asynchronous completion routine of something started by methodA.