Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Tutorials > Tutorial Requests

Reply
 
LinkBack Thread Tools Display Modes
Old 07-02-2010, 02:43 AM   #1 (permalink)
Registered Member
 
Join Date: Jul 2010
Posts: 1
TGDrakaRG is on a distinguished road
Default Request for Tutorial on how to make an NSTimer run in the background (multitasking)

Could anyone post some sample code, or write a tutorial on how to make an NSTimer keep on running when the app that contains it is in the background?
The Apple documentation is limited/non-existant. I think it involves: beginBackgroundTaskWithExpirationHandler, but I haven't successfully used it to make an app where the NSTimer keeps running when in the background.

Help please!
TGDrakaRG is offline   Reply With Quote
Old 07-12-2010, 02:18 AM   #2 (permalink)
New to the SDK
 
franzwarning's Avatar
 
Join Date: Jul 2010
Location: Portland, Oregon
Age: 19
Posts: 165
franzwarning is on a distinguished road
Send a message via AIM to franzwarning Send a message via Skype™ to franzwarning
Default

agreed.
franzwarning is offline   Reply With Quote
Old 09-30-2010, 08:01 AM   #3 (permalink)
Registered Member
 
Join Date: May 2009
Posts: 14
dineshkumarm12 is on a distinguished road
Default have u found the answer for your query,I have the same issue,

Quote:
Originally Posted by TGDrakaRG View Post
Could anyone post some sample code, or write a tutorial on how to make an NSTimer keep on running when the app that contains it is in the background?
The Apple documentation is limited/non-existant. I think it involves: beginBackgroundTaskWithExpirationHandler, but I haven't successfully used it to make an app where the NSTimer keeps running when in the background.

Help please!
have u found the answer for your query,I have the same issue,If u know please post here.....It will be great helpfull
dineshkumarm12 is offline   Reply With Quote
Old 03-15-2011, 09:56 PM   #4 (permalink)
Registered Member
 
Join Date: Apr 2010
Posts: 10
Rober1337 is on a distinguished road
Default

We should make a club of people who really want/need to learn this... I could be president.
Rober1337 is offline   Reply With Quote
Old 03-18-2011, 02:32 PM   #5 (permalink)
Registered Member
 
Join Date: Sep 2010
Location: KSA
Posts: 109
aziz is on a distinguished road
Send a message via MSN to aziz Send a message via Yahoo to aziz Send a message via Skype™ to aziz
Default

Please please any one ???
I will pay to get a sample code or Tutorial !!! $$$
7.5 Days of searching !!!!!
I need it for Getting GPS location.
aziz is offline   Reply With Quote
Old 03-22-2011, 05:47 PM   #6 (permalink)
Registered Member
 
Join Date: Sep 2010
Location: KSA
Posts: 109
aziz is on a distinguished road
Send a message via MSN to aziz Send a message via Yahoo to aziz Send a message via Skype™ to aziz
Default

Hi again ,,,

in AppDelegate.h add:

Code:
UIBackgroundTaskIdentifier bgTask;
BOOL inBackground;
in AppDelegate.m:
Code:
- (void)applicationDidEnterBackground:(UIApplication *)application {
	
	inBackground=YES;
	UIApplication*    app = [UIApplication sharedApplication];
	
    bgTask = [app beginBackgroundTaskWithExpirationHandler:^{
        [app endBackgroundTask:bgTask];
        bgTask = UIBackgroundTaskInvalid;
    }];
	
    // Start the long-running task and return immediately.
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
		while (inBackground == YES) {
			
			  // check lat and lon 
                          // if its what you want call the local Notification
                           
			[NSThread sleepForTimeInterval:(10*60)];
		}
		

        [app endBackgroundTask:bgTask];
        bgTask = UIBackgroundTaskInvalid;
    });
}
in applicationWillEnterForeground:
Code:
inBackground = NO;
I think apple will throw this code in my face, but this is the only way i could manage this.

please any suggestions ??

Last edited by aziz; 03-22-2011 at 05:51 PM.
aziz is offline   Reply With Quote
Old 07-28-2011, 03:33 PM   #7 (permalink)
Registered Member
 
Join Date: Jun 2011
Posts: 1
flashharry is on a distinguished road
Default NSTimers?

//add these two functions. The clock will stay in time even when the app is in the background!
int running;
int startTime;
int timerNow;
NSTimer *timer;

-(void) start{
startTime=[[NSDate date] timeIntervalSince1970];
timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(startTimer) userInfo:nil repeats:YES];

}



-(void)startTimer{

running = [[NSDate date] timeIntervalSince1970];

timerNow = running-startTime;
NSLog(@"running %i:",timerNow);
}
- (void)viewDidLoad
{
[self start];
[super viewDidLoad];
}
flashharry is offline   Reply With Quote
Reply

Bookmarks

Tags
background, multitasking, nstimer in background

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Stats
Members: 175,696
Threads: 94,139
Posts: 402,961
Top Poster: BrianSlick (7,990)
Welcome to our newest member, jasper_muc
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 02:02 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0