Best way of detecting fast app switching resume?

218 views
Skip to first unread message

youngpip

unread,
Nov 1, 2010, 5:31:54 AM11/1/10
to phonegap
I'd like to incorporate PIN-protection for my app, which on the
surface of it is fine until I realise the fast-app-switching feature
of iOS4 means the app can launch again bypassing it.

Is there a good way of detecting that the app has been resumed?

Or, as it's a fairly lightweight app that's fast to load anyway, am I
best to try and disable fast app switching and make it load from
scratch each time (if that's possible)?

Thanks,
Phil

Shazron Abdullah

unread,
Nov 1, 2010, 2:14:31 PM11/1/10
to youngpip, phonegap
Add the new delegates that detect when an app is resumed in your [AppName]AppDelegate.m and send out a javascript event or call a javascript function in the webview.

To prevent multitasking in your app, modify your [AppName]-Info.plist, in Xcode just add a new value and it should be one of the new values in the dropdown..., check that off.

> --
> You received this message because you are subscribed to the Google
> Groups "phonegap" group.
> To post to this group, send email to phon...@googlegroups.com
> To unsubscribe from this group, send email to
> phonegap+u...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/phonegap?hl=en?hl=en
>
> For more info on PhoneGap or to download the code go to www.phonegap.com

youngpip

unread,
Nov 1, 2010, 5:34:33 PM11/1/10
to phonegap
Hi Shazron,

That's great, thanks for the pointers. I'm intrigued about the first
method you mention. It looks like this is the relevant code to check
if it's been resumed:

- (void)applicationWillEnterForeground:(UIApplication *)application

...however I must say I'm still struggling to get the right syntax to
launch a JavaScript function. If you have any further thoughts they'd
be much appreciated!

Many thanks,
Phil


On Nov 1, 6:14 pm, Shazron Abdullah <shazron.abdul...@nitobi.com>
wrote:

Shazron Abdullah

unread,
Nov 1, 2010, 5:52:06 PM11/1/10
to youngpip, phonegap
In your app delegate function, you can do this

[[super webView] stringByEvaluatingJavaScriptFromString:@"alert('yippikiyay')"];

youngpip

unread,
Nov 1, 2010, 7:24:17 PM11/1/10
to phonegap
Hi Shazron,

Cool, this all looks good. I really appreciate your help.

I tried putting in the following as a test, but when I tried it
(closing the app and starting it again soon after), the app crashed
and the debugger quit.

- (void)applicationWillEnterForeground:(UIApplication *)application
{
[[super webView]
stringByEvaluatingJavaScriptFromString:@"alert('testing')"];
}

...Xcode also warns that "PhoneGapDelegate may not respond to '-
webview'". Am I missing something?

Lastly, where the alert test is, would I be able to call a function
within the .js already in my app?

Many thanks,
Phil


On Nov 1, 9:52 pm, Shazron Abdullah <shazron.abdul...@nitobi.com>

Jesse MacFadyen

unread,
Nov 1, 2010, 7:37:42 PM11/1/10
to youngpip, phonegap
just call :

- (void)applicationWillEnterForeground:(UIApplication *)application
{
       [ webView stringByEvaluatingJavaScriptFromString:@"alert('shaz wuz rong!')"];
--
Jesse MacFadyen
{
  blog:'blogs.nitobi.com/jesse',
  email:'jesse.m...@nitobi.com',
  company:
  {
    name:'Nitobi Software',
    site:'www.nitobi.com',
    phone:
    {
      office:'+1 (604) 685-9287',
      tollFree:'1-866-632-2777'
     }
  }
}

youngpip

unread,
Nov 2, 2010, 4:09:01 AM11/2/10
to phonegap
Thanks Jesse. Sadly I tried it and the when the app resumed it froze
and crashed again :-(

On Nov 1, 11:37 pm, Jesse MacFadyen <jesse.macfad...@nitobi.com>
wrote:
> > > >>> phonegap+u...@googlegroups.com<phonegap%2Bunsu...@googlegroups.com>
> > > >>> For more options, visit this group at
> > > >>>http://groups.google.com/group/phonegap?hl=en?hl=en
>
> > > >>> For more info on PhoneGap or to download the code go
> > towww.phonegap.com
>
> > > > --
> > > > You received this message because you are subscribed to the Google
> > > > Groups "phonegap" group.
> > > > To post to this group, send email to phon...@googlegroups.com
> > > > To unsubscribe from this group, send email to
> > > > phonegap+u...@googlegroups.com<phonegap%2Bunsu...@googlegroups.com>
> > > > For more options, visit this group at
> > > >http://groups.google.com/group/phonegap?hl=en?hl=en
>
> > > > For more info on PhoneGap or to download the code go
> > towww.phonegap.com
>
> > --
> > You received this message because you are subscribed to the Google
> > Groups "phonegap" group.
> > To post to this group, send email to phon...@googlegroups.com
> > To unsubscribe from this group, send email to
> > phonegap+u...@googlegroups.com<phonegap%2Bunsu...@googlegroups.com>
> > For more options, visit this group at
> >http://groups.google.com/group/phonegap?hl=en?hl=en
>
> > For more info on PhoneGap or to download the code go towww.phonegap.com
>
> --
> --
> Jesse MacFadyen
> {
>   blog:'blogs.nitobi.com/jesse',
>   email:'jesse.macfad...@nitobi.com <email%3A%27jesse.macfad...@nitobi.com>

Nick McCloud

unread,
Nov 2, 2010, 10:28:36 AM11/2/10
to phonegap
Have you got a JavaScript function called "MoFo"? There has to be a
corresponding function in your JavaScript that will be run - something
like:

function BruceWillis() {
alert("Just woken up!");
> > > > >>> phonegap+u...@googlegroups.com<phonegap%2Bunsubscribe@googlegroups.c om>
> > > > >>> For more options, visit this group at
> > > > >>>http://groups.google.com/group/phonegap?hl=en?hl=en
>
> > > > >>> For more info on PhoneGap or to download the code go
> > > towww.phonegap.com
>
> > > > > --
> > > > > You received this message because you are subscribed to the Google
> > > > > Groups "phonegap" group.
> > > > > To post to this group, send email to phon...@googlegroups.com
> > > > > To unsubscribe from this group, send email to
> > > > > phonegap+u...@googlegroups.com<phonegap%2Bunsubscribe@googlegroups.c om>
> > > > > For more options, visit this group at
> > > > >http://groups.google.com/group/phonegap?hl=en?hl=en
>
> > > > > For more info on PhoneGap or to download the code go
> > > towww.phonegap.com
>
> > > --
> > > You received this message because you are subscribed to the Google
> > > Groups "phonegap" group.
> > > To post to this group, send email to phon...@googlegroups.com
> > > To unsubscribe from this group, send email to
> > > phonegap+u...@googlegroups.com<phonegap%2Bunsubscribe@googlegroups.c om>

youngpip

unread,
Nov 2, 2010, 10:56:30 AM11/2/10
to phonegap
Hi Nick,

Sorry I think I'm missing the "MoFo" reference! However I think I'm
going to make a habit of calling all my functions after lines From Die
Hard :-)

I thought just running an alert function would be fine for testing
purposes, but are you saying that's where it's going wrong? This is
what I tried as per Jesse's post:

- (void)applicationWillEnterForeground:(UIApplication *)application
{
[[super webView]
stringByEvaluatingJavaScriptFromString:@"alert('testing')"];

}

...should it instead be

- (void)applicationWillEnterForeground:(UIApplication *)application
{
[[super webView]
stringByEvaluatingJavaScriptFromString:@"NakatomiPlaza()"];

}

...where "NakatomiPlaza()" is a function in my internal js?

Ta,
Phil
> > > > > >>> phonegap+u...@googlegroups.com<phonegap%2Bunsubscr...@googlegroups.c om>
> > > > > >>> For more options, visit this group at
> > > > > >>>http://groups.google.com/group/phonegap?hl=en?hl=en
>
> > > > > >>> For more info on PhoneGap or to download the code go
> > > > towww.phonegap.com
>
> > > > > > --
> > > > > > You received this message because you are subscribed to the Google
> > > > > > Groups "phonegap" group.
> > > > > > To post to this group, send email to phon...@googlegroups.com
> > > > > > To unsubscribe from this group, send email to
> > > > > > phonegap+u...@googlegroups.com<phonegap%2Bunsubscr...@googlegroups.c om>
> > > > > > For more options, visit this group at
> > > > > >http://groups.google.com/group/phonegap?hl=en?hl=en
>
> > > > > > For more info on PhoneGap or to download the code go
> > > > towww.phonegap.com
>
> > > > --
> > > > You received this message because you are subscribed to the Google
> > > > Groups "phonegap" group.
> > > > To post to this group, send email to phon...@googlegroups.com
> > > > To unsubscribe from this group, send email to
> > > > phonegap+u...@googlegroups.com<phonegap%2Bunsubscr...@googlegroups.c om>

Nick McCloud

unread,
Nov 2, 2010, 11:31:21 AM11/2/10
to phonegap
That's my understanding of the how to do it - all the PhoneGap source
shows function calls. It had never occurred to me to try a direct
statement.

Shaz started the theme with 'yippikiyay' - I don't think the expanded
version would go down too well in such refined circles as these.

Andrew Lunny

unread,
Nov 2, 2010, 1:45:07 PM11/2/10
to Nick McCloud, phonegap
The alert function is a blocking call, and
stringByEvaluatingJavaScriptFromString blocks until it returns. In
this case, alert is called before the webview is visible, so the user
can't dismiss the alert.

One thing that's worked for me is to wrap the call (an alert, or a
different function call) in a simple setTimeout - this ensures that
the stringByEvaluatingJavaScriptFromString does not lock up
(setTimeout returns immediately, and the callback code isn't called
until the event loop is ready to handle it) and your code executes as
desired.

Sample here:
http://gist.github.com/491468

--
Andrew Lunny
Software Developer, Nitobi
604 685 9287
blogs.nitobi.com/andrew

Jesse

unread,
Nov 2, 2010, 2:16:18 PM11/2/10
to Andrew Lunny, Nick McCloud, phonegap
That's it, forgot about that. Thanks Andrew.

Sent from my iPhone

youngpip

unread,
Nov 2, 2010, 2:23:48 PM11/2/10
to phonegap
Hi Andrew,

Thanks for the info. I tried the code as per your listing and the good
news is that it doesn't crash the app as before. The bad news is that
it doesn't seem to work either! I copied it as follows, and for the
sake of testing created an "appActive" function in my app's .js file
which did a simple alert, but nothing happened:


- (void)applicationDidBecomeActive:(UIApplication *)application {
NSString *fireActiveEvent = @"window.setTimeout(function() { \n"
"var appActive = document.createEvent('Events'); \n"
"appActive.initEvent('appActive'); \n"
"document.dispatchEvent(appActive); \n"
"}, 1);";
[webView stringByEvaluatingJavaScriptFromString:fireActiveEvent];
}

...any thoughts? I must be missing something obvious!

Thanks,
Phil

Andrew Lunny

unread,
Nov 2, 2010, 3:07:37 PM11/2/10
to youngpip, phonegap
Using that code, you would need to subscribe some code on the JS side
to the appActive event, like so:

document.addEventListener('appActive', function () {
alert('app is now active');
}, false);

Either that or replace the call within setTimeout on the Objective-C
side to call your appActive function directly:

- (void)applicationDidBecomeActive:(UIApplication *)application {
NSString *fireActiveEvent = @"window.setTimeout(function() { \n"

"appActive(); \n"


"}, 1);";
[webView stringByEvaluatingJavaScriptFromString:fireActiveEvent];
}

youngpip

unread,
Nov 2, 2010, 3:27:45 PM11/2/10
to phonegap
Andrew you are a star, it works a treat!
> ...
>
> read more »
Reply all
Reply to author
Forward
0 new messages