Coder Social home page Coder Social logo

mebrunet / phonegap-backgroundjs Goto Github PK

View Code? Open in Web Editor NEW

This project forked from jocull/phonegap-backgroundjs

0.0 2.0 0.0 9.52 MB

Background JavaScript on iOS with PhoneGap

Home Page: http://www.codefromjames.com/wordpress/?p=154

Objective-C 71.26% JavaScript 28.74%

phonegap-backgroundjs's Introduction

PhoneGap + Background.js

Whoa! Now you can background JavaScript operations on iOS with PhoneGap!

Read more here: http://www.codefromjames.com/wordpress/?p=154

Uses a sleep countdown timer paired with a UIBackgroundTaskIdentifier. The magic looks like this:

	- (void)doBackgroundTimeLoop
	{
		__block UIBackgroundTaskIdentifier task;
		UIApplication* app = [UIApplication sharedApplication];
		task = [app beginBackgroundTaskWithExpirationHandler:^{
			[app endBackgroundTask:task];
			task = UIBackgroundTaskInvalid;
		}];

		while(YES){
			@synchronized(self){
				backgroundSecondsCounter--;
				NSLog(@"Remaining background seconds: %i", backgroundSecondsCounter);
				if (backgroundSecondsCounter <= 0)
					break; // Exit loop
			}
			[NSThread sleepForTimeInterval:1];
		}

		// End this background task now
		[app endBackgroundTask:task];
		task = UIBackgroundTaskInvalid;
	}

While the background task is active, anything goes! You can run JavaScript in the background.

Add the BackgroundJS plugin to your Phonegap project:

phonegap local plugin add <location of BackgroundJS git repository>

...enable the plugin in config.xml:

    <feature name="BackgroundJS">
      <param name="ios-package" value="BackgroundJS" />
    </feature>

...and request some background time from JavaScript!

BackgroundJS is available in JavaScript via:

  window.plugins.backgroundjs

Get a block of secondsi (e.g. use a background thread for 10 seconds):

	window.plugins.backgroundjs.setBackgroundSeconds(10);

Run in the background indefinitely:

	window.plugins.backgroundjs.lockBackgroundTime();

Stop running background tasks immediately:

 window.plugins.backgroundjs.unlockBackgroundTime();

Be careful when using this, as Apple specifications are picky. If you're not using background audio or tracking location, your app may be rejected for background tasking guidelines. Read up, and write some code!

ALSO... Please don't kill my battery life just because you backgrounded more than you had to. You're not a special snowflake (yet) so play by the rules. Code smart, and make your app work right!

Have a patch? Pull requests appreciated!

Thanks! -James

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.